Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为每个控制器内的每个操作设置模型属性_Java_Spring_Spring Mvc_Model View Controller_Spring Boot - Fatal编程技术网

Java 为每个控制器内的每个操作设置模型属性

Java 为每个控制器内的每个操作设置模型属性,java,spring,spring-mvc,model-view-controller,spring-boot,Java,Spring,Spring Mvc,Model View Controller,Spring Boot,我想在我的模型上为Spring Boot应用程序中的多个控制器中的每个@RequestMapping设置三个公共属性。我已经读过关于@modeldattribute的内容,但它需要放在每个控制器中。我的应用程序中有20多个控制器,每个控制器都有10多个@RequestMapping 是否有一种方法可以在一个地方设置这样的模型属性,在应用程序开始时初始化?< p>如果您想在Spring Bug启动上执行一些代码,请考虑如下: 但是我想你真的想要控制器相关的行为,我建议使用全局拦截器 使用全局拦截

我想在我的模型上为Spring Boot应用程序中的多个控制器中的每个
@RequestMapping
设置三个公共属性。我已经读过关于
@modeldattribute
的内容,但它需要放在每个控制器中。我的应用程序中有20多个控制器,每个控制器都有10多个
@RequestMapping


是否有一种方法可以在一个地方设置这样的模型属性,在应用程序开始时初始化?

< p>如果您想在Spring Bug启动上执行一些代码,请考虑如下:

但是我想你真的想要控制器相关的行为我建议使用全局拦截器

使用全局拦截器,您可以在Spring中干扰请求-响应生命周期

它允许您在3个不同点向请求-响应生命周期添加功能:

  • 在控制器处理请求之前
  • 处理程序完成其功能后
  • 当视图即将呈现给最终用户时
  • 只需创建一个从
    HandlerInterceptorAdapter
    扩展的类,并使用所需的功能覆盖三个方法中的一个

    例如:

    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);
        }
    
    }
    
    下面是一个关于如何使用Spring Boot的示例:

    @Configuration
    public class WebMvcConfig extends WebMvcConfigurerAdapter {
    
      @Autowired 
      MyInterceptor myInterceptor;
    
      @Override
      public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(...)
        ...
        registry.addInterceptor(myInterceptor);
      }
    }
    

    如果您想在Spring Bug启动上执行一些代码,请考虑如下:

    但是我想你真的想要控制器相关的行为我建议使用全局拦截器

    使用全局拦截器,您可以在Spring中干扰请求-响应生命周期

    它允许您在3个不同点向请求-响应生命周期添加功能:

  • 在控制器处理请求之前
  • 处理程序完成其功能后
  • 当视图即将呈现给最终用户时
  • 只需创建一个从
    HandlerInterceptorAdapter
    扩展的类,并使用所需的功能覆盖三个方法中的一个

    例如:

    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);
        }
    
    }
    
    下面是一个关于如何使用Spring Boot的示例:

    @Configuration
    public class WebMvcConfig extends WebMvcConfigurerAdapter {
    
      @Autowired 
      MyInterceptor myInterceptor;
    
      @Override
      public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(...)
        ...
        registry.addInterceptor(myInterceptor);
      }
    }
    

    我提供了SpringMVC的另一个方法,使用,当您实现它时,它提供了3个方法,每个方法都包含,您可以使用
    请求设置属性。setAttribute(“xx”,“yy”)
    ,下面是代码:

    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;
        }
    
    }
    
    然后,您需要向spring mvc应用程序添加自定义拦截器,如下所示:

    <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>
    

    我提供了SpringMVC的另一个方法,使用,当您实现它时,它提供了3个方法,每个方法都包含,您可以使用
    请求设置属性。setAttribute(“xx”,“yy”)
    ,下面是代码:

    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;
        }
    
    }
    
    然后,您需要向spring mvc应用程序添加自定义拦截器,如下所示:

    <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>
    
    
    
    引入一个通用的BaseController类,并将
    @ModelAttribute
    放在该类中,让您的所有控制器扩展BaseController,这就是用
    @ControllerAdvice
    注释的类可以为您做的事情。或者使用
    HandlerInterceptor
    在每个请求上添加公共数据。引入公共BaseController类,并将
    @ModelAttribute
    放在类中,让所有控制器扩展BaseController,这就是用
    @ControllerAdvice
    注释的类可以为您做的事情。或者使用
    HandlerInterceptor
    在每个请求上添加公共数据。在这种情况下,属性是在请求上设置的,而不是在模型上设置的。我说的对吗?我相信最终它也会被设定在模型上。使用request参数不需要另一个步骤来在模型上设置它们吗?不需要进一步的步骤,request set参数就能完成任务。我的意思是使用我的原始解决方案+尝试在JSP上读取参数,就像在这种情况下通常使用model属性一样,属性是根据请求而不是模型设置的。我说的对吗?我相信最终它也会被设定在模型上。使用request参数不需要另一个步骤来在模型上设置它们吗?不需要进一步的步骤,request set参数就能完成任务,我的意思是使用我的原始解决方案+尝试在JSP上读取参数,就像通常使用model属性一样