How to get current Controller Action so I can set active class on navbar link

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How to get current Controller Action so I can set active class on navbar link

tom123
How can I set the css class attribute on a html link based on the current controller action?
Essentially, is there a method i can call to find out the current controller action?

<ul th:if="${sec.hasAnyRole('ROLE_USER','ROLE_ADMIN')}" class="nav pull-right">
	<li class="dropdown" th:inline="text">
        	<a class="dropdown-toggle" data-toggle="dropdown" href="#" th:inline="text">
        	[[${sec.principal.username}]]<b class="caret"></b>
           	</a>
            	<ul class="dropdown-menu">
              		<li><a href="#" "${Set css class attribute to 'active' here if current action equals 'settings'}"><i class="icon-wrench icon"></i>&nbsp;&nbsp; Settings</a></li>
              		<li class="divider"></li>
              		<li><a href="#" th:href="@{/logout}"><i class="icon-off icon"></i>&nbsp;&nbsp; Sign out</a></li>
            	</ul>
	</li>
</ul>
Reply | Threaded
Open this post in threaded view
|

Re: How to get current Controller Action so I can set active class on navbar link

Emanuel
Administrator
You can set the class attribute on an HTML element using the th:class attribute and putting a conditional operator in there, eg:

<div th:class="${controller.action} eq 'settings' ? 'active'">

I don't know how to get the current controller action though, without resorting to putting it on the Thymeleaf context like above (context.setVariable("controller", <your controller>), then your controller has an 'action' property/getter that can be queried) - hopefully someone will be able to answer that.
Reply | Threaded
Open this post in threaded view
|

Re: How to get current Controller Action so I can set active class on navbar link

tom123
Thanks Emanuel.

I implemented it as you suggested:

<li th:class="${controllerName} eq 'CardReportController' ? 'active'"><a href="#"
              th:href="@{/cardreport}">My Purchases</a></li>

And I added a class to intercept requests and add the controller name and action to the model.

public class BaseInterceptor extends HandlerInterceptorAdapter
{
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception
	{
		HandlerMethod handlerMethod = (HandlerMethod) handler;

		modelAndView.addObject("controllerName", handlerMethod.getBean()
				.getClass().getSimpleName());
		modelAndView.addObject("controllerAction", handlerMethod.getMethod()
				.getName());
	}
}

@EnableWebMvc
@ComponentScan({ "com.webapp.controller" })
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter
{

	public WebConfig()
	{
		super();
	}

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry)
	{
		registry.addResourceHandler("/css/**").addResourceLocations("/css/");
		registry.addResourceHandler("/js/**").addResourceLocations("/js/");
		registry.addResourceHandler("/img/**").addResourceLocations("/img/");
	}

	@Override
	public void addInterceptors(InterceptorRegistry reg)
	{
		reg.addInterceptor(new BaseInterceptor());
		reg.addInterceptor(new SecurityInterceptor());
		reg.addInterceptor(new AddCardInterceptor());
	}

}