我如何更改自动添加的spring mvc处理程序的默认处理程序顺序

问题描述 投票:2回答:2

[实施项目时,我发现在使用<mvc:resources/>@RequestMapping(value = "/**")设置控制器后@RequestMapping(method=RequestMethod.GET)无法正常工作。

似乎带注释的控制器的优先级高于SimpleURLHandler。

有人可以解决这个问题吗?我需要该控制器,无法删除它。

提前感谢!

这是我设置项目的方式以及有关该问题的详细信息:

Web.xml

<servlet>
  <servlet-name>Test</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/webmvc-config.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>Test</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>

webmvc-config.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <!-- The controllers are autodetected POJOs labeled with the @Controller annotation. -->
    <context:component-scan base-package="com.web.test" use-default-filters="false">
        <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
    </context:component-scan>

    <!-- Turns on support for mapping requests to Spring MVC @Controller methods
         Also registers default Formatters and Validators for use across all @Controllers -->
    <mvc:annotation-driven conversion-service="applicationConversionService"/>
    <bean class="com.web.test.web.ApplicationConversionServiceFactoryBean" id="applicationConversionService"/>

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources -->
    <mvc:resources location="classpath:/META-INF/web-resources/, /WEB-INF/views/" mapping="/resources/**" cache-period="0" order="0"/>

    <!-- Allows for mapping the DispatcherServlet to "/" by forwarding static resource requests to the container's default Servlet -->
    <mvc:default-servlet-handler/>

            <!-- selects a static view for rendering without the need for an explicit controller -->
            <mvc:view-controller path="/" view-name="index"/>

            <bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver" id="resourceBundleViewResolver" p:basename="META-INF/view/wicket-views" p:order="1"/>

    <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver" id="tilesViewResolver">
        <property name="order" value="2"/>
        <property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/>
    </bean>
    <bean class="org.springframework.web.servlet.view.tiles2.TilesConfigurer" id="tilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/views/layouts/layouts.xml</value>
                <value>classpath:/META-INF/view/tiles-views.xml</value>
            </list>
        </property>
    </bean>
... ...
</beans>

ApplicationController.java

@Controller
@RequestMapping(method=RequestMethod.GET)
public class ApplicationController {

    @RequestMapping(value = "/**")
    public ModelAndView handleRequest(HttpServletRequest request){


        ModelAndView mav = new ModelAndView("index");   

        return mav;
    }
}

运行日志。 (如您在此处看到的那样,资源请求是由带注释的控制器而不是ResourceHttpRequestHandler处理的。)

2011-11-15 17:21:09,821 [http-8080-2] DEBUG org.springframework.web.servlet.DispatcherServlet - Rendering view [org.springframework.web.servlet.view.tiles2.TilesView: name 'index'; URL [index]] in DispatcherServlet with name 'Test'
2011-11-15 17:21:09,821 [http-8080-2] DEBUG org.apache.tiles.impl.BasicTilesContainer - Render request recieved for definition 'index'
2011-11-15 17:21:09,821 [http-8080-4] DEBUG org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'Test' processing GET request for [/Test/resources/javascripts/jQuery/jquery-ui-1.8.16.custom.min.js]
2011-11-15 17:21:09,823 [http-8080-4] DEBUG org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - Matching patterns for request [/resources/javascripts/jQuery/jquery-ui-1.8.16.custom.min.js] are [/**/, /**]
2011-11-15 17:21:09,823 [http-8080-4] DEBUG org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - URI Template variables for request [/resources/javascripts/jQuery/jquery-ui-1.8.16.custom.min.js] are {}
2011-11-15 17:21:09,823 [http-8080-4] DEBUG org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - Mapping [/resources/javascripts/jQuery/jquery-ui-1.8.16.custom.min.js] to HandlerExecutionChain with handler [com.web.Test.web.ApplicationController@1a57c9e4] and 4 interceptors
2011-11-15 17:21:09,823 [http-8080-4] DEBUG org.springframework.web.servlet.DispatcherServlet - Last-Modified value for [/Test/resources/javascripts/jQuery/jquery-ui-1.8.16.custom.min.js] is: -1
2011-11-15 17:21:09,823 [http-8080-4] DEBUG org.springframework.web.bind.annotation.support.HandlerMethodInvoker - Invoking request handler method: public org.springframework.web.servlet.ModelAndView com.web.Test.web.ApplicationController.handleRequest(javax.servlet.http.HttpServletRequest)

如果删除ApplicationController,则可以访问静态资源。

2011-11-15 17:48:16,784 [http-8080-2] DEBUG org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'Test' processing GET request for [/Test/resources/styles/application-common.css]
2011-11-15 17:48:16,784 [http-8080-2] DEBUG org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Matching patterns for request [/resources/styles/application-common.css] are [/resources/**]
2011-11-15 17:48:16,785 [http-8080-2] DEBUG org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - URI Template variables for request [/resources/styles/application-common.css] are {}
2011-11-15 17:48:16,785 [http-8080-2] DEBUG org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapping [/resources/styles/application-common.css] to HandlerExecutionChain with handler [org.springframework.web.servlet.resource.ResourceHttpRequestHandler@6cc5cbab] and 4 interceptors
    

在实施我的项目时,我发现在使用@RequestMapping(value =“ / **”)和@RequestMapping(method = RequestMethod.GET)设置控制器后,

spring-mvc resources handler static-linking
2个回答
0
投票
绝对所有

0
投票
© www.soinside.com 2019 - 2024. All rights reserved.