2013-03-25 77 views
0

我试图从jsp文件(WEB-INF/jsp/index.jsp)访问css & js文件时出现问题。我阅读了Stack Overflow上其他人遇到的类似问题。访问动态Web项目中的css,js文件的问题

enter image description here

我加入这行代码到我的SpringMVCProj-servlet.xml中,这让事情变得更复杂了。

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

SpringMVCProj-servlet.xml中

<context:component-scan base-package="com.myapp" /> 
<mvc:resources mapping="/WebContent/**" location="/WebContent/" /> 

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

的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_2_5.xsd" id="WebApp_ID" version="2.5"> 
    <display-name>Spring MVC Application</display-name> 

    <welcome-file-list> 
<!--  <welcome-file>/WEB-INF/jsp/index.jsp</welcome-file> 
-->  
<welcome-file>/WEB-INF/jsp/index.jsp</welcome-file> 
    </welcome-file-list> 

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

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

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/SpringMVCProj-servlet.xml</param-value> 
</context-param> 

<listener> 
    <listener-class> 
     org.springframework.web.context.ContextLoaderListener 
    </listener-class> 
</listener> 


</web-app> 

这是我之前添加了代码行。

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

enter image description here

这之后...

enter image description here

+0

WebContent是源目录。它不存在于生成的已部署Web应用程序中。 – 2013-03-25 16:40:11

回答

0

它的工作后,我改变了我的SpringMVCProj-servlet.xml中了这一点。

<?xml version="1.0" encoding="UTF-8"?> 

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
     http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 
     http://www.springframework.org/schema/aop 
     http://www.springframework.org/schema/aop/spring-aop.xsd"> 

    <mvc:annotation-driven /> 
    <mvc:resources mapping="/**" location="/" /> 

    <context:component-scan base-package="com.myapp" /> 
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> 
     <property name="prefix" value="/WEB-INF/jsp/" /> 
     <property name="suffix" value=".jsp" /> 
    </bean> 


</beans>