在实施我的项目时,我发现在使用<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
答案 0 :(得分:0)
使用value = "/**"
,您说ApplicationController
将处理绝对所有请求的网址。
只需指定一个更具限制性的模式,您的静态内容将无法匹配,并且它会像您希望的那样工作;示例:
value = "/dynamic/**"
value = "/**/*.html"
value = "/pages/**"
选择取决于你,但是现在“RESTful”风格非常热,所以我会遵循style guide。
答案 1 :(得分:0)
我有同样的要求。
我在Spring Boot应用程序中捆绑了一个React应用程序,但我希望所有静态资源请求都具有最高优先级,然后回退到带注释的控制器。
这样,所有不是实际上是静态文件的URL都将解析为我的React应用程序的索引/根页面。
例如-
/admin/css/style.css
应该解析为静态文件。
但是React路由应该解析根索引页面/视图。
/admin/sub/path
应该解析为视图。
这可以通过在带注释的控制器中映射所有路径来实现-
@Controller
public class DefaultMvcController {
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
@GetMapping({ "", "/"})
public String index() throws IOException {
return "index";
}
@RequestMapping(path = {"/admin", "/admin/**"}, method = RequestMethod.GET)
public String admin() throws IOException {
return "admin/index";
}
}
并将静态文件处理程序映射的顺序更改为小于已注释的控制器映射优先级的 1
@Configuration
@ComponentScan(basePackages = "net.savantly.sprout.controllers")
@RequiredArgsConstructor
public class SproutWebMvcConfigurer extends WebMvcConfigurationSupport {
@Bean
@Override
public HandlerMapping resourceHandlerMapping(UrlPathHelper urlPathHelper, PathMatcher pathMatcher,
ContentNegotiationManager contentNegotiationManager, FormattingConversionService conversionService,
ResourceUrlProvider resourceUrlProvider) {
HandlerMapping mapping = super.resourceHandlerMapping(urlPathHelper, pathMatcher, contentNegotiationManager, conversionService,
resourceUrlProvider);
((AbstractHandlerMapping)mapping).setOrder(-1);
return mapping;
}
}
此处是完整的源代码-https://github.com/savantly-net/sprout-platform/tree/development/spring/sprout-spring-boot-starter