4

I have some navigation in one of my thymeleaf templates.

<li class="active"><a th:href="@{/}">Home</a></li>
<li class=""><a th:href="@{/about}">About</a></li>
<li class=""><a th:href="@{/contact}">Contact</a></li>

What I want to do is set the active class of the current navigation item that we are on. Coming from the grails world we can do this easily because there are 2 very important variables available to us in a GSP.

${controllerName}
${actionName}

Now I know that I can manually setting these variables in my Spring MVC controller and passing them to the view but this does not seem like a good solution.

@Controller
@RequestMapping("/about")
public class AboutController {

    @RequestMapping("/")
    public String home(Model model) {
        model.addAttribute("controllerName", "about");
        model.addAttribute("actionName", "home");        
        return "about/home";        
    }

}

Any other way to do this?

Dan Vega
  • 1,097
  • 1
  • 14
  • 23
  • The only way that i know which is not custom variables based is described here: http://stackoverflow.com/questions/10852131/how-to-get-the-mapped-url-from-controller-name-action-name-in-springmvc . But it's still kind of a manual way. – Rozart Jan 14 '16 at 13:17
  • That is what I thought. I was using something like this but I was just checking to see if there is an easier way. I might try and create a base interceptor and add 2 variables to every request.
  • – Dan Vega Jan 14 '16 at 14:10
  • Well I am afraid that there is no such thing similar to the one in Grails. Additionally i am not so sure if `
  • – Rozart Jan 14 '16 at 14:13
  • https://stackoverflow.com/questions/27623405/thymeleaf-add-parameter-to-current-url – V H Nov 19 '22 at 16:12