Spring资源处理

时间:2014-06-26 11:03:23

标签: spring spring-mvc

我是spring的新手。我正在使用mvc资源来处理资源。但是我仍然遇到异常 警告:在DispatcherServlet中找不到带有URI [/GameApp/static/js/jquery.colorbox.js]的HTTP请求的映射,其名称为' GameApp'

这是我的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
   <display-name>GameApp</display-name>
   <welcome-file-list>
   <welcome-file>
   index.jsp
   </welcome-file>
   </welcome-file-list>
   <servlet>
            <servlet-name>GameApp</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
              <param-name>contextConfigLocation</param-name>
              <param-value></param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>

        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                 <!-- Spring application context declaration -->
                  classpath:/config/spring-config.xml
            </param-value>
        </context-param>
        <servlet-mapping>
            <servlet-name>GameApp</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>

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



        </web-app>

这是我的spring-config.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/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
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

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

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


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

    <mvc:annotation-driven/>



</beans>

我已将我的静态资源放在webapp / static中......请解释可能导致问题的原因

1 个答案:

答案 0 :(得分:0)

您的classpath:/config/spring-config.xml需要加入DispatcherServlet contextConfigLocation

<servlet>
   <servlet-name>GameApp</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   <init-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>classpath:/config/spring-config.xml</param-value>
   </init-param>
   <load-on-startup>1</load-on-startup>
</servlet>

您可以将根应用程序上下文contextConfigLocation留空:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value></param-value>
</context-param>