【问题标题】:Use of the mvc:resources causes an 404 error in my Spring application使用 mvc:resources 在我的 Spring 应用程序中导致 404 错误
【发布时间】:2023-03-17 06:45:01
【问题描述】:

在我的 spring 应用程序中,我遵循这篇文章中的提示:

http://www.java-allandsundry.com/2012/10/spring-mvc-static-resource-handling.html

映射我的资源文件(css、js 等)。

问题是当我将此代码放入 spring-servlet.xml 文件时,当我尝试运行应用程序时收到 404 错误(甚至没有显示任何页面):

    <mvc:resources mapping="/bootstrap/**" location="/bootstrap/" /> 
    <mvc:resources mapping="/extras/**" location="/extras/" /> 
    <mvc:resources mapping="/jquery/**" location="/jquery/" />

这是我的 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>HorarioLivre2</display-name>

  <welcome-file-list>
    <welcome-file>acesso/login.html</welcome-file>
  </welcome-file-list>

  <servlet>
    <servlet-name>HorarioLivre2</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>HorarioLivre2</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

仅作记录,如果我从 *-servlet.xml 文件中删除上述 3 行,则只有在替换此行时才能访问我的应用程序:

    <url-pattern>/</url-pattern>

那个人:

    <url-pattern>*.html</url-pattern>

有人知道如何使用 mvc:resources 正确配置资源映射吗?

我的 *-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<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:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

        <context:component-scan base-package="com.horariolivre"/>
        <mvc:annotation-driven></mvc:annotation-driven>
        <mvc:resources location="/bootstrap/" mapping="/bootstrap/**" />
        <mvc:resources location="/extras/" mapping="/extras/**" />
        <mvc:resources location="/jquery/" mapping="/jquery/**" />

            <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
                <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
                <property name="prefix" value="/WEB-INF/jsp/" />
                <property name="suffix" value=".jsp" />
            </bean>

        <context:annotation-config>
            <bean class="com.horariolivre.resources.HibernateConfig">
            </bean>
        </context:annotation-config>

        <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

我如何在我的 JSP 文件中包含资源:

<!-- Bootstrap core CSS -->
<link href="<c:out value="/bootstrap/css/bootstrap.min.css"/>" rel="stylesheet">

<!-- Custom styles for this template -->
<link href="<c:out value="/extras/css/signin.css"/>" rel="stylesheet">

【问题讨论】:

  • 使用** 附加您的位置模式很好(例如/boostrap/**
  • 不工作。对我来说更奇怪的是为什么我什至无法访问我的应用程序;如果仅在此配置出现问题时无法访问资源,这对我来说更自然。

标签: spring spring-mvc


【解决方案1】:

以下内容适用于我的应用程序。

<context:component-scan base-package="controllers, email"></context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>
<mvc:resources location="/resources/" mapping="/static/**" />
    //defined within the dispatcher servlet xml file

    <security:http use-expressions="true">
            <security:intercept-url pattern="/static/**" access="permitAll" />
            <security:intercept-url pattern="/images/**" access="permitAll" />
    //defined within the security-context.xml file

图像的 JSP 显示

<img src="${pageContext.request.contextPath}/static/css/images/spring-logo.jpg"
        alt="" title="" border="0">

你的可能是

<img src="${pageContext.request.contextPath}/extras/image.jpg"
        alt="" title="" border="0">

【讨论】:

  • 这对我不起作用,因为我没有在这个应用程序中使用 spring-security。但我不明白的一件事:你真的将 mvc:resources 放在你的 web.xml 中吗?您将什么选项附加到 标记以使其工作?
  • 抱歉,我的意思是 DispatcherServlet 配置 xml 文件。我已经添加了一点。应用是否在没有资源标签的情况下启动?
  • 好的,根据您的建议,我现在可以从我的应用程序访问页面,但资源仍然无法访问。我更新了我的问题以包括我修改后的 *-servlet.xml 和我的 JSP 文件中包含资源的一些行。
  • 您没有获得应用程序上下文。我已经编辑了我的答案以显示我如何在 JSP 页面中显示图像。
猜你喜欢
  • 1970-01-01
  • 2015-07-09
  • 2013-12-26
  • 1970-01-01
  • 1970-01-01
  • 2018-02-25
  • 2020-02-13
  • 1970-01-01
  • 2018-11-10
相关资源
最近更新 更多