Java SpringMVC:资源标记和404错误

Java SpringMVC:资源标记和404错误,java,spring,Java,Spring,我正在尝试使用标签 这是我的项目结构 我想要的是访问js文件夹中的javascript和styles文件夹中的css 但是,每当我将其添加到dispatcher-servlet.xml时 <mvc:resources mapping="/resources/**" location="/"/> 我在运行项目时遇到错误404(项目将重定向到login.htm,即login.jsp页面) 这是我的dispatcher-servlet.xml代码 <beans xmlns

我正在尝试使用标签

这是我的项目结构

我想要的是访问js文件夹中的javascript和styles文件夹中的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>

上下文配置位置
/WEB-INF/applicationContext.xml
org.springframework.web.context.ContextLoaderListener
调度员
org.springframework.web.servlet.DispatcherServlet
2.
调度员
/
30
redirect.jsp

我不知道为什么要删除这行
,因为资源文件是折叠的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" />

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

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

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

让我知道这是否有效

艾曼

答案如下:

引用作者的话:“
需要
(或显式声明的处理程序映射等)。”


这对我来说是个好办法。

你必须准确地回答一个有效的答案。你写了“我在运行项目时遇到了404错误”——你请求哪个URL?!您希望看到哪个文件(位置)?您在浏览器中点击了哪个url,我们需要您的bean配置文件?为此,运行项目时,它将重定向到login.htm,这意味着login.jsp页面。关于WEB-INF文件夹的注释是错误的!,因为如果请求的路径包含
WEB-INF
META-INF
或有效路径以“.”开头,Springs
ResourceHttpRequestHandler
不会返回资源!(在
ResourceHttpRequestHandler.isValidPath(字符串)
中实现)