我正在尝试解决一个常见的问题,即尝试从我的 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>
spring-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>