【问题标题】:Cannot load static resources when using web fragments and spring使用web片段和spring时无法加载静态资源
【发布时间】:2026-01-21 15:15:02
【问题描述】:

我有一个主要的 spring hello webapp,它包含在 web.xml 中以包含一个 web 片段。我似乎无法访问 jar 中的静态资源,我使用的是 tomcat 7、servlet 3、Spring 3.2.3.RELEASE

而我的jar结构是test.jar->META-INF->test.css

但是当我加载应用程序时,网址:

http://localhost:8686/spring-fragment-test/test.css

好像没用,继续404

我的配置如下:

主应用中的 Spring.xml

<!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <context:component-scan base-package="com.fragments" />

与 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" version="3.0">

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

  <absolute-ordering>   
    <name>SampleFragment</name>
    </absolute-ordering>    

</web-app>

我的其他 fagemt jar 在 meta-inf 中有以下 xml

<?xml version="1.0" encoding="UTF-8"?>
<web-fragment id="WebFragment_ID" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" 
                             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-fragment_3_0.xsd">

    <display-name>SampleFragment</display-name> 
    <name>SampleFragment</name> 

</web-fragment>

【问题讨论】:

    标签: spring-mvc spring-3 servlet-3.0 web-fragment


    【解决方案1】:

    由于您将位置“/resources/”放在 xml 中

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

    你必须把你的css文件放在这个部分

    网页内容/资源/(css文件)

    您现在可以通过写入来访问该文件

    http://loclahost:8080/spring/resources/main.css

    【讨论】:

    • 告诉我你将 css 放在哪里的确切结构。
    • 我已经改变了那个答案。试试看
    • 是的,我试过了,但我想在 Jar 文件中提取 css,我似乎做不到......
    • 好吧,我进一步添加了以下 但后来我没有需要 web-fragment.xml 吗?!?
    • 不知道...我不知道它是关于什么的?虽然我可以说这个程序没有它也可以工作。