2015-12-02 90 views
0

是Spring Framework的新功能。我试图使用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 

一些静态文件,当我type url

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"> 

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

不起作用。

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

回答

0

首先,你必须把你静态内容WEB-INF文件夹 和您映射资源

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

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

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

它是由

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