我一直在测试Spring MVC中的请求映射,并在我的应用程序中遇到了一个奇怪的情况。我决定创建一个简单的场景,以便您能够理解我的问题。首先,我将向您展示我的项目(源代码)的详细信息,然后再提出我的问题。
我的项目具有以下目录结构:
+webapp
+WEB-INF
+recursos
+estilos
test.css
+spring
fronte-beans.xml
+views
+testes
page1.jsp
page2.jsp
web.xml
我的Tomcat部署描述符:
<?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 http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.4">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>fronte</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/spring/fronte-beans.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>fronte</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
我的DispatcherServlet的应用程序上下文:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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 ">
<mvc:annotation-driven />
<mvc:resources mapping="/recursos/**" location="/WEB-INF/recursos/" />
<context:component-scan base-package="com.regra7.minhaapp.contro" />
<bean id="handlerMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
我的控制器类用于page1.jsp页面:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping(value="/path")
public class TestController
{
@RequestMapping(value="/to/something")
public String getPage()
{
return "testes/page2";
}
}
我的页面1.jsp:
<!DOCTYPE html>
<%@ page
language="java"
contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html lang="pt-BR">
<!-- ############################################### -->
<!-- HEADER -->
<!-- ############################################### -->
<head>
<title>Test Page</title>
<meta name="author" content="RDS" />
<meta name="description" content="? ? ?" />
<meta charset="UTF-8" />
<link rel="stylesheet" type="text/css" href="recursos/estilos/test.css" media="all" />
</head>
<!-- ############################################### -->
<!-- BODY -->
<!-- ############################################### -->
<body>
<h1>PAGE 1</h1>
<p>This is a test, p1.</p>
<p>This is a test, p2.</p>
<p>This is a test, p3.</p>
<a href="${pageContext.request.contextPath}/path/to/something">CLICK TO PAGE 2</a>
</body>
</html>
我可以顺利访问page1.jsp和page2.jsp页面,但是page2.jsp的CSS文件无法找到。我的控制台输出以下文本:
dez 29, 2014 8:16:22 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/testeSpringMvc/path/to/recursos/estilos/test.css] in DispatcherServlet with name 'fronte'
为什么"/path/to"会出现在最终路径中?如果我尝试添加不同的映射组合,无论是类级别还是方法级别,都会发生同样的事情。但是,如果我将链接映射到查询字符串(URL),则可以轻松找到文件,如下所示...
page1.jsp:
<a href="${pageContext.request.contextPath}/?cd=page2">CLICK TO PAGE 2</a>
控制器:
@Controller
@RequestMapping(value="/")
public class TestController
...
@RequestMapping(params="cd=page2")
public String getPage()
发生了什么?我该如何设置所需路径,使页面可以使用和找到必要的资源?我尝试将不同路径的页面分开,以便可以应用来自Tomcat的安全特性(安全约束)。
注意:我尝试使用contextPath来帮助我设置CSS文件路径,但没有任何作用。事实上,情况变得更糟糕了,因为Page1.jsp也没有样式:
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}recursos/estilos/test.css" media="all" />
一如既往,感谢您的关注和时间。
技术相关内容:无法翻译,需要提供具体内容。