为CSS和与设计相关的事情获取HTTP 404错误

时间:2014-04-09 14:05:36

标签: spring spring-mvc

我正在使用spring MVC3.0。 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/j2ee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID" version="2.4">
  <display-name>Spring MVC Form Handling</display-name>

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

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

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  </web-app>

注意:我有两个网址模式,这就是为什么应用程序设计正在破坏而我正在获取 404 css和图像等错误......

弹簧servlet.xml中:

<bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:messages" />
        <property name="defaultEncoding" value="UTF-8" />
    </bean>

    <bean id="localeChangeInterceptor"
        class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="lang" />
    </bean>

    <bean id="localeResolver"
        class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
        <property name="defaultLocale" value="en" />
    </bean>

    <bean id="handlerMapping"
        class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
        <property name="interceptors">
            <ref bean="localeChangeInterceptor" />
        </property>
            </bean> 

所以我希望有问题处理程序bean来解决问题。

2 个答案:

答案 0 :(得分:2)

利用Spring功能,提供有效的静态resorces服务。为此,您应该在下面的条目spring-context。

<!-- Handles HTTP GET requests for /resources/** by efficiently serving 
    up static resources in the ${webappRoot}/resources directory -->
<mvc:resources mapping="/resources/**" location="/resources/" />

您必须将css,js和图像文件放在webappRoot / resources文件夹中。还要删除用于提供静态资源的servlet映射。在你的情况下,保留下面的servlet映射并从web.xml中删除spring1 servlet,因为它没有映射到任何目标servlet

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

在JSP中,请参考静态资源 <link rel="stylesheet" type="text/css" media="screen" href="<s:url value='/resources/css/styles.css'/>" /> <script src="<s:url value='/resources/js/jquery-1.8.3.js'/>" type="text/javascript"> </script>

答案 1 :(得分:0)

检查你的日志,你应该有一个例外:

    java.lang.IllegalArgumentException: Servlet mapping specifies an unknown servlet name spring1...

除非你错过了另一个servlet-name为spring1的servlet,否则从web.xml中删除以下行并检查:

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