Spring和tag mvc资源,无法达到.css

时间:2014-01-17 22:15:08

标签: java spring spring-mvc

当我在“控制台”中的Web浏览器中启动Spring项目时出现以下错误:

GET http://localhost:8080/lbagno/$%7Bresource%7D   [HTTP/1.1 404 Not Found 6ms]

当我看到Tomcat的控制台模式时,我有以下消息:

WARNING: No mapping found for HTTP request with URI [/lbagno/${resource}] in DispatcherServlet with name 'mvc-dispatcher'

我不明白为什么。

你有解决方案吗?

谢谢

'lbagno'项目的架构:

-- src 
   -- main
     -- java
        -- resources
           -- css
              -- normalMode
                   header.css
        -- webapp
           -- WEB-INF
             -- /pages
             -- mvc-dispatcher-serclet.xml
             -- web.xml
-- pom.xml

-- target
   -- apache-tomcat-maven-plugin
   -- classes
   -- dependency
   -- lbagno
   -- maven-archiver
   -- surefire
   -- tomcat
   -- lbagno.war

lbagno.war的结构

- META-INF
  - /maven
  - MANIFEST.MF
- WEB-INF
  - classes
      - com
      - css
          - normalMode
             - header.css
      - images
      - normalMode
          -css
            - header.css
      - mvc-dispatcher-servlet.xml
  - lib
  - pages
  - mvc-dispatcher-servlet.xml
  - web.xml
- index.jsp

MVC-调度-servlet.xml中

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

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

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">

          <property value="org.springframework.web.servlet.view.JstlView"  name="viewClass" />

          <property name="prefix">
                <value>/WEB-INF/pages/</value>
          </property>

          <property name="suffix">
               <value>.jsp</value>
          </property>
    </bean>

    <mvc:annotation-driven/>

    <mvc:resources mapping="/resources/**" location="/src/main/resources/css/normalMode/"/>

</beans>

的web.xml

<!DOCTYPE web-app PUBLIC
  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>

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

  <servlet-mapping>
     <servlet-name>mvc-dispatcher</servlet-name>
     <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

header.jsp中

<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>

<head>
   <spring:url value="/resources/css/header.css" var="resource"/>
   <link media="screen" rel="stylesheet" type="text/css" href="${resource}" />
</head>

2 个答案:

答案 0 :(得分:0)

此行错误

<mvc:resources mapping="/resources/**" location="/src/main/resources/css/normalMode/"/>

位置src/main/resources是源位置!将此行更改为:

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

请查看war存档以获取正确的文件位置。


您的css文件也位于错误的位置:

- WEB-INF
  - classes
      - com
      - css
          - normalMode
             - header.css
      - images
      - normalMode
          -css
            - header.css

请将此文件放入src/main/webapp

答案 1 :(得分:0)

我认为问题出在你的web.xml。您的web.xml应声明为Servlet 2.4或更高版本,以便能够使用JSP Expression Language${resource})。

尝试使用此web.xml

<web-app
    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"
    version="2.4">

  <display-name>Archetype Created Web Application</display-name>

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

  <servlet-mapping>
     <servlet-name>mvc-dispatcher</servlet-name>
     <url-pattern>/</url-pattern>
  </servlet-mapping>

</web-app>