首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用web片段和spring时无法加载静态资源

使用web片段和spring时无法加载静态资源
EN

Stack Overflow用户
提问于 2015-06-04 23:32:49
回答 1查看 1.3K关注 0票数 0

我有一个主要的spring hello webapp,它在web.xml中包含了一个网页片段。我似乎无法访问jar中的静态资源,我使用的是tomcat7、servlet3、Spring3.2.3.RELEASE

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

但当我加载应用程序时,url:

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

似乎不起作用,继续得到404

我的配置如下:

主应用程序中的Spring.xml

代码语言:javascript
复制
<!-- 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

代码语言:javascript
复制
<?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>

我的另一个fragemt jar在meta-inf中有以下xml

代码语言:javascript
复制
<?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>
EN

回答 1

Stack Overflow用户

发布于 2015-06-05 00:54:03

因为您将位置"/resources/“放在xml中

代码语言:javascript
复制
<mvc:resources mapping="/resources/**" location="/resources/" />

你必须把你的css文件放在

Web-Content/resources/(css文件)

现在,您可以通过以下方式访问该文件

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30648141

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档