【问题标题】:Spring not loading static contentSpring不加载静态内容
【发布时间】:2015-12-02 15:01:51
【问题描述】:

我是 Spring 框架的新手。我正在尝试使用 spring 创建一个测试应用程序。视图运行良好,但在我的 JSP 上我无法使用静态内容。 即使我尝试直接访问它们,它们也不会打开。 我已经尝试了我在网上找到的所有解决方案。 如果我遗漏了什么,请告诉我。

这是我的 springmvc-servlet.xml

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

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

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">    
<property name="prefix" value="/jsp/" />    
<property name="suffix" value=".jsp" />    
</bean>    
</beans>

以下是我的 web.xml 条目

<servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
       <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
   <servlet-name>springmvc</servlet-name>
   <url-pattern>/</url-pattern>
</servlet-mapping>
<servlet>

我有一些静态文件位于

/src/static/img/apple-touch-icon.png
/src/static/css/bootstrap.css
/src/static/css/bootstrap.min.css
/src/static/css/styles.css
/src/index.html

但是当我输入网址时

http://localhost:8080/springdemo/index.html

http://localhost:8080/springdemo/static/img/apple-touch-icon.png

显示 404。:(

还有

<link rel="apple-touch-icon" href="/springdemo/static/img/apple-touch-icon.png">

&lt;link rel="apple-touch-icon" href="http://localhost:8080/springdemo/static/img/apple-touch-icon.png">

不工作。

请帮助我,因为我刚刚开始学习 Spring。

【问题讨论】:

    标签: java spring spring-mvc tomcat tomcat7


    【解决方案1】:

    首先你必须把你的静态内容放在 WEB-INF 文件夹中 你已经映射了你的资源

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

    这意味着“WEB-INF/resources/”中的任何文件都可用“/”。例如,如果您在资源中有一些图像

     /WEB-INF/resources/static/image/icon.png
    

    它可以被

    访问
    <link href="${pageContext.request.contextPath}/static/icon.png" ....>
    

    【讨论】:

      猜你喜欢
      • 2017-04-05
      • 2017-01-14
      • 2017-07-19
      • 2017-03-25
      • 2019-03-04
      • 2020-01-26
      • 2019-07-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多