spring mvc:resources标签和404错误

时间:2013-08-22 06:29:10

标签: java spring

我正在尝试使用代码<mvc:resources>

这是我的项目结构

enter image description here

我想要的是访问js文件夹中的javascript和样式文件夹中的css
但是,每当我将它添加到我的dispatcher-servlet.xml

<mvc:resources mapping="/resources/**" location="/"/>

我在运行项目时遇到错误404(项目将重定向到login.htm,这是login.jsp页面)

这是我的dispatcher-servlet.xml代码

<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错误。我认为这与当前项目存在冲突。

3 个答案:

答案 0 :(得分:14)

答案在这里:

Using <mvc:resources .../> in spring 3 causes all other views to stop working

引用作者:“<mvc:resources>需要<mvc:annotation-driven>(或显式声明处理程序映射等)。”

这对我有用。

答案 1 :(得分:1)

您需要在location属性中设置完整路径,如下所示:

<mvc:resources mapping="/resources/js/**" location="/js/" />

一个单独的注意事项,我建议您将资源全部放在一个文件夹中,例如“资产”,因为在您当前的设置中,恶意用户可以访问WEB-INF下的任何内容/这显然不是您的想。

让我知道这是否有效。

艾曼

答案 2 :(得分:-1)

由于资源文件是折叠js和样式,因此您需要将映射更改为:

<mvc:resources mapping="/js/**" location="/js/" />
<mvc:resources mapping="/styles/**" location="/styles/" />

并将它们加载到jsp:

<script type="text/javascript" src="/js/file.js"></script>
<link type="text/css" href="/styles/file.css" />
相关问题