Setting model attributes for every action within e

2019-09-14 19:24发布

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?

2条回答
SAY GOODBYE
2楼-- · 2019-09-14 19:39

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>
查看更多
等我变得足够好
3楼-- · 2019-09-14 19:58

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:

  1. before a controller handles a request
  2. after a handler finished performing its functionality
  3. 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);
  }
}
查看更多
登录 后发表回答