来自 mvc:resource 的处理程序映射覆盖使用注释定义的其他映射
我是 spring mvc3 的新手,我正在尝试创建一个简单的项目来接近 spring mvc3。
现在,当我尝试服务器一些静态资源文件时遇到一些问题。
因为我在 web.xml 中使用了 url-pattern (/):
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
所以,当我输入: http://locaohost:8080/spring/res/css/main.css。我会收到 404 错误。
从 spring 文档中,我尝试使用
但如果我在 spring 中添加此标签-servlet.xml,我发现,我现在可以获取资源文件,但无法访问其他页面。
也就是说,我有一个控制器:
@Controller
@RequestMapping("/example")
public class HelloController {
@RequestMapping("hello")
public String hello(Model model){
model.addAttribute("name", "John");
return "spring.example.hello";
}
}
当我访问: http://locaohost:8080/spring/例如/你好,我现在会得到 404。
但是如果我删除标签:
我可以访问 http://locaohost:8080/spring/example/hello,但是 我无法获取 .css/.js 文件。
通过eclipse中的调试器,我发现当spring在“DispatchServlet”的“initHanderMapping”方法中初始化handerMapping时,它创建了两个映射实例: BeanNameUrlHandlerMapping 和 SimpleUrlHandlerMapping。
BeanNameUrlHandlerMapping 的 handelrMap 属性始终为空,而 SimpleUrlHandlerMapping 始终包含 url 匹配映射。
当我添加标签时,它的handerMapping属性是: {/res/**=org.springframework.web.servlet.resource.ResourceHttpRequestHandler@1120eed}
当我删除标签时,handelrMapping是:<代码>{/example/hello=com.spring.controller.HelloController@1b5438d, /example/hello.*=com.spring.controller.HelloController@1b5438d,/example/hello/=com.spring.controller.HelloController@1b5438d}。
看来,{/res/**=xxxx}
覆盖了其他映射{/example/helloxxxxx}
这是spring-servlet.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- <mvc:resources location="/res/" mapping="/res/**"></mvc:resources>-->
<context:component-scan base-package="com.spring.controller" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.tiles2.TilesViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView" />
</bean>
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/jsp/tile_def.xml</value>
</list>
</property>
</bean>
</beans>
如何修复它?
I am new in spring mvc3,and I am trying to create a simple project to get close to spring mvc3.
Now I meet some problem when I try to server some static resources files.
Because I use the url-pattern (/) in the web.xml:
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
So,when I enter: http://locaohost:8080/spring/res/css/main.css. I will get the 404 error.
From the spring document,I try to use the <mvc:resource location="/res/" mapping="/res/**" />
But if I add this tag in the spring-servlet.xml,I found that,I can get the resources file now,but I can not access other page.
That's to say,I have a controller:
@Controller
@RequestMapping("/example")
public class HelloController {
@RequestMapping("hello")
public String hello(Model model){
model.addAttribute("name", "John");
return "spring.example.hello";
}
}
When I visit: http://locaohost:8080/spring/example/hello,I will get 404 now.
But If I remove the tag: <mvc:resource xxx/>
I can access http://locaohost:8080/spring/example/hello,but I can not get the .css/.js file.
Through debugger in eclipse,I found that when spring init the handerMapping in the method "initHanderMapping" of "DispatchServlet",it created two mapping instance:
BeanNameUrlHandlerMapping and SimpleUrlHandlerMapping.
The handelrMap property of the BeanNameUrlHandlerMapping is always empty while the SimpleUrlHandlerMapping always contain the url-matching mappings.
When I add the tag ,its handerMapping property is: {/res/**=org.springframework.web.servlet.resource.ResourceHttpRequestHandler@1120eed}
When I remove the tag,the handelrMapping is :{/example/hello=com.spring.controller.HelloController@1b5438d, /example/hello.*=com.spring.controller.HelloController@1b5438d, /example/hello/=com.spring.controller.HelloController@1b5438d}
.
It seems that ,the {/res/**=xxxx}
override other mappings {/example/helloxxxxx}
This is the spring-servlet.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- <mvc:resources location="/res/" mapping="/res/**"></mvc:resources>-->
<context:component-scan base-package="com.spring.controller" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.tiles2.TilesViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView" />
</bean>
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/jsp/tile_def.xml</value>
</list>
</property>
</bean>
</beans>
How to fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将
添加到您的上下文中。
覆盖 spring mvc 的默认行为。如果将
添加到 spring-servlet.xml,它应该强制注册所有必需的处理程序。Try adding
<mvc:annotation-driven />
to your context.<mvc:resource...>
overrides the default behavior of spring mvc. If you add<mvc:annotation-driven />
to your spring-servlet.xml, it should forcibly register all required handlers.更好的解决方案:
这将指定资源的优先顺序。
Better solution:
This will specify a precedence order for resources.