【问题标题】:spring mvc:resources tag and 404 errorspring mvc:资源标签和 404 错误
【发布时间】:2013-08-22 06:29:09
【问题描述】:

我正在尝试使用标签<mvc:resources>

这是我的项目结构

我想要的是访问 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>

我不知道为什么我删除&lt;mvc:resources...这行项目运行正常,但是当我将这行添加到项目中时,我立即收到404错误。我认为这与当前的项目有些冲突。

【问题讨论】:

  • 你要精确地得到一个有效的答案。您写了“运行项目时出现错误 404”——您请求哪个 URL?!您期望哪个文件(位置)?
  • 您在浏览器中点击了什么 url,我们将需要您的 bean 配置文件
  • 对不起,在运行项目时,它会重定向到 login.htm,即 login.jsp 页面。

标签: java spring


【解决方案1】:

答案就在这里:

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

引用作者的话:“&lt;mvc:resources&gt; 需要&lt;mvc:annotation-driven&gt;(或显式声明的处理程序映射等)。”

这对我有用。

【讨论】:

    【解决方案2】:

    你需要在location属性中设置完整路径,像这样:

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

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

    让我知道这是否有效。

    艾曼

    【讨论】:

    • WEB-INF文件夹的注释有误!因为SpringsResourceHttpRequestHandler如果请求的路径包含WEB-INFMETA-INF或有效路径以“. ."! (在ResourceHttpRequestHandler.isValidPath(String)中实现)
    【解决方案3】:

    由于资源文件在折叠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" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-01
      • 2017-04-07
      • 2015-08-20
      • 2012-10-16
      • 1970-01-01
      • 2019-04-17
      • 1970-01-01
      • 2012-08-03
      相关资源
      最近更新 更多