Spring MVC 3.1提供静态资源

时间:2012-06-04 09:57:54

标签: eclipse spring-mvc tomcat7

我正在尝试解决从Java Web App尝试提供静态资源(如图像,样式表和脚本)的常见问题。我尝试了很多在其他线程中提供的解决方案,但是无处可去,我得到的只是资源调用上的404错误。

我唯一能想到的是我在Tomcat 7上运行,但是如果我尝试使用6,我会得到一个“服务器不支持J2EE Web模块规范3.0版”。错误,所以看起来像是不行。有没有人有任何想法我可能会出错?

我的项目结构如下:

WebContent

    META-INF

    Resources

        CSS

        Images

            close.jpg

        Scripts 

    WEB-INF

        Lib

        Views

            index.jsp

Web.xml中

<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>/</url-pattern>
</servlet-mapping>

弹簧的servlet

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

<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:p="http://www.springframework.org/schema/p"
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="uk.ac.ncl.controllers" />

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

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />

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

我的控制器

@RequestMapping(value="/", method = RequestMethod.GET)
public String index()
{
    return "index";
}

我的观点

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>
    <head>
        <title>Spring 3.0 MVC Series: Index - ViralPatel.net</title>
    </head>
    <body>
        <h1>Page with image</h1>
        <!-- use c:url to get the correct absolute path -->
        <img src="<c:url value="/resources/images/close.png" />" />
        <img src="http://localhost:8080${pageContext.request.contextPath}/resources/images/close.png" />
    <a href="map">View Map</a>
</body>

1 个答案:

答案 0 :(得分:0)

可能是区分大小写的问题吗?您的html说/resources/image,但您的树看起来像/Resources/Images