Java spring mvc:资源标签和404错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18373165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:58:05  来源:igfitidea点击:

spring mvc:resources tag and 404 error

javaspring

提问by Kien Dang Ngoc

I'm trying to use tag <mvc:resources>

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

this is my project structure

enter image description here

这是我的项目结构

在此处输入图片说明

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

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

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

I got error 404, when run the project (the project will redirect to login.htm which is login.jsp page)

运行项目时出现错误 404(项目将重定向到 login.htm,即 login.jsp 页面)

this is my dispatcher-servlet.xml code

这是我的 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>

this is web.xml

这是 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>

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...项目运行正确的行,但是当将这一行添加到项目时,我立即收到 404 错误。我认为这与当前的项目有些冲突。

回答by Septem

since resource files are in fold js and styles, you need to change mapping to:

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

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

and load them in jsp:

并在jsp中加载它们:

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

回答by Ayman

You need to set the full path in the location attribute, like this:

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

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

One a separate note, I would recommend you put your resources all in one folder all together, for example "assets", because in your current setup a malicious user can access anything under WEB-INF/ which is obviously not what you want.

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

Let me know if this works.

让我知道这个是否奏效。

Ayman

艾曼

回答by LuizPoleto

The answer is here:

答案在这里:

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

在 spring 3 中使用 <mvc:resources .../> 会导致所有其他视图停止工作

Quoting the author: "<mvc:resources>requires <mvc:annotation-driven>(or explicitly declared handler mappings, etc)."

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

That did the trick for me.

这对我有用。