Spring MVC Get Controller & Method Name

The one thing I really love about my Spring Boot Introductory Course is that my students are always asking me questions. I had this question come in recently and I thought we should take this chance to discuss it.

Dan, Suppose you have header, footer and content templates. The header has the navigation bootstrap code. For example you have multiple tabs in the navigation i.e. “Admin” and “Customers”. How do you make the tab visually active if you are on Customer page or vice versa when using templates? Thanks, AK

I have done a lot of Grails web development and If you know anything about that you will know that it’s built on Spring MVC. Grails makes 2 very important variables available to your views

If these two variables were available to us in Thymeleaf that would make our job really easy right? Well sadly they are not so we need to look at a few alternatives. First we could manually set these variables in every method that sends content to a view.

And we can check the controller name and if it’s that “section” we can add the active class.

While this works it seems like a lot of work to have to set those variables in every single method. The http servlet request is made available in the view template so we can also do something like this.

While this will work some of the times trying to match on the URI isn’t always a good choice and to me this just seems like a hacky solution.

My Solution

There might be other solutions to this puzzle but this is what I came up with and I think it works out pretty well. I like the idea of the controller and action names being available in my view but I don’t want to set them on every request manually. Thankfully we can create an interceptor to do this for us. We are extending the HandlerInterceptorAdapter which gives us the ability to override the post handle method. In this method I am going to set the controller and action(method) name and send it to the view using ModelAndView.

There was one issue I found doing this. At first I was setting the controller using the following code.

This would work 99% of the time except when I was using Spring Security. When I was logged in Spring Security would wrap my controller with a proxy class and the name of my controller would look something like

So I switched it up and got the actual type of the class which gave me what I needed. Now that you have the Base Interceptor created you need to register it.

That is all there was to it. Now in every view I had those two variables available. In my template I was able to do something like this

I hope this helps and if you can think of any reasons NOT to do this or have a more elegant solution please let me know below.

Hi, I’m Dan Vega. I am a Software Engineer living just outside of one of my favorite places in the world, Cleveland, Ohio. I am a self-taught programmer who firmly believes that you can accomplish anything in this life if you put your mind to it, roll up your sleeves and are willing to put in the work.

I have been writing software for over 18+ years now and I use my knowledge, passion, and influence to help other developers achieve their goals. I teach online and currently, have over 30,000 students.

When I am not writing software, I have a number of things that really keep me busy. I love to read books, listen to podcasts and watch online courses. I have a real thirst for knowledge and it consumes me every single day of the week. I also love to run & lift heavy weights which keep me balanced throughout the week.

Please note: I reserve the right to delete comments that are offensive or off-topic.