spring mvc:resources标签和404错误 [英] spring mvc:resources tag and 404 error
问题描述
我正在尝试使用代码< mvc:resources>
这是我的项目结构< br>
this is my project structure
我想要的是访问js文件夹中的javascript和样式文件夹中的css
但是,每当我将它添加到我的dispatcher-servlet.xml
what I want is to access to javascript in js folder and css in styles folder
however, whenever I add this to my dispatcher-servlet.xml
<mvc:resources mapping="/resources/**" location="/"/>
我在运行项目时遇到错误404(项目将重定向到login.htm,这是登录.jsp页面)
I got error 404, when run the project (the project will redirect to login.htm which is login.jsp page)
这是我的dispatcher-servlet.xml代码
this is my dispatcher-servlet.xml code
<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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<context:component-scan base-package="com.swcommodities.wsmill.controller"/>
<mvc:resources mapping="/resources/**" location="/"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/"
p:suffix=".jsp" />
<tx:annotation-driven/>
.
.
.
</beans>
这是web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
我不知道为什么我删除了行< mvc:resources ...
项目正确运行,但是当将此项添加到项目中时,我立即收到404错误。我认为这个与当前项目存在冲突。
I don't know why I delete the line <mvc:resources...
the project run correctly, but when add this one to the project, I get 404 error immediately. I think this one make some conflict with the current project.
推荐答案
答案在这里:
使用< mvc:resources ... />在春季3导致所有其他观点停止工作
引用作者:< mvc:resources>
需要< mvc:annotation-driven>
(或显式声明的处理程序映射等)。
Quoting the author: "<mvc:resources>
requires <mvc:annotation-driven>
(or explicitly declared handler mappings, etc)."
这对我有用。
这篇关于spring mvc:resources标签和404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!