问题
I would like to set three common attributes on my model for each @RequestMapping within the many controllers in a Spring Boot application. I have read about @ModelAttribute but it needs to be placed within each Controller. I have more than 20 controllers in my application and each having more than 10 @RequestMapping.
Is there a way to set such model attributes in one place which gets initialized at the start of the application?
回答1:
If you want to execute some code on Spring Boot startup, consider this:
Spring boot startup listener
But I guess that you really want Controller related behavior, I would recommend using a Global Interceptor.
With global interceptor, you can interfere with the request-response lifecycle in Spring.
It lets you add functionality to request-response life cycle in 3 different points:
- before a controller handles a request
- after a handler finished performing its functionality
- when the view is about to render to the end user.
Just create a class which extends from HandlerInterceptorAdapter and override one of three methods, with your desired functionality.
For Example:
public class MyInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
request.setAttribute("myFirstAttribute", "MyFirstValueHere");
return super.preHandle(request, response, handler);
}
}
Here's an example on how to do it with Spring Boot:
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
@Autowired
MyInterceptor myInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(...)
...
registry.addInterceptor(myInterceptor);
}
}
回答2:
I provide another method by SpringMVC,using the HandlerInterceptor,when you implements it,it provides 3 methods,each of them contains HttpServletRequest,you can set attribute using request.setAttribute("xx","yy"),below are the code:
public class RequestInterceptor implements HandlerInterceptor {
public void afterCompletion(HttpServletRequest arg0,
HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
}
public void postHandle(HttpServletRequest request, HttpServletResponse arg1,
Object arg2, ModelAndView arg3) throws Exception {
//you can set attributes here like request.setAttribute("xx","yy")
}
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object arg2) throws Exception {
//you can set attributes here like request.setAttribute("xx","yy")
return false;
}
}
then,you need to add the custom interceptor to your spring mvc application like below:
<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
<property name="interceptors">
<list>
<!--class of your custom interceptor-->
<bean class="com.xx.RequestInterceptor"/>
</list>
</property>
</bean>
来源:https://stackoverflow.com/questions/44368087/setting-model-attributes-for-every-action-within-each-controller