Java Spring MVC 资源映射
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27697878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring MVC resource Mapping
提问by Loa
I have been testing some request mapping in Spring MVC, and I came across a strange situation in my application. I decided to create a simple cenario so that you can understand my problem. I will first show you the details of my project (the source), and then I'll get to my question.
我一直在测试Spring MVC中的一些请求映射,在我的应用程序中遇到了一个奇怪的情况。我决定创建一个简单的场景,以便您了解我的问题。我将首先向您展示我的项目(来源)的详细信息,然后我将回答我的问题。
I have the following directory structure in my project:
我的项目中有以下目录结构:
+webapp
+WEB-INF
+recursos
+estilos
test.css
+spring
fronte-beans.xml
+views
+testes
page1.jsp
page2.jsp
web.xml
My Tomcat deployment descriptor:
我的 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>
My application context for DispatcherServlet:
我的 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>
My controller class for page1.jsp:
我的 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";
}
}
My page1.jsp:
我的page1.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>
I can access page1.jsp and page2.jsp smoothly, but the CSS file of page2.jsp ends up not being found. The following text is printed on my console:
我可以顺利访问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'
For what reason "/path/to" is being included in the resulting path? If I try to add different combinations of mapping, both class or method-level, the same thing happens. However, if I map the link to a query string (URL) as follows, the file is found without problems...
由于什么原因“/path/to”被包含在结果路径中?如果我尝试添加不同的映射组合,无论是类级别还是方法级别,都会发生同样的事情。但是,如果我将链接映射到查询字符串(URL),如下所示,找到该文件没有问题......
page1.jsp:
page1.jsp:
<a href="${pageContext.request.contextPath}/?cd=page2">CLICK TO PAGE 2</a>
Controller:
控制器:
@Controller
@RequestMapping(value="/")
public class TestController
...
@RequestMapping(params="cd=page2")
public String getPage()
What's happening? How can I set a path I want so that my pages use and find the necessary resources? I'm trying to separate pages in different paths so that I can apply the security features from Tomcat (security constraints).
发生了什么?如何设置我想要的路径,以便我的页面使用并找到必要的资源?我正在尝试将不同路径中的页面分开,以便我可以应用 Tomcat 的安全功能(安全约束)。
NOTE: I've tried using contextPath to help me in setting the CSS file path, but nothing worked. In fact, the situation worsened because Page1.jsp also turned out not having stylization:
注意:我尝试使用 contextPath 来帮助我设置 CSS 文件路径,但没有任何效果。事实上,情况变得更糟,因为 Page1.jsp 也没有风格化:
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}recursos/estilos/test.css" media="all" />
As always, thank you for your attention and time.
一如既往,感谢您的关注和时间。
采纳答案by Loa
I just found the answer to my problems. I will leave here the solution, but unfortunately I did not understand how it solves the seen scenario.
我刚刚找到了我的问题的答案。我将在这里留下解决方案,但不幸的是我不明白它是如何解决所看到的场景的。
I have found that this can be solved with JSTL. I read the JSTL documentation, and all I found was this description:
我发现这可以用JSTL解决。我阅读了 JSTL 文档,我发现的只是这个描述:
Creates a URL with optional query parameters.
创建带有可选查询参数的 URL。
If reference to the CSS file is changed by the following sentence, the problem will be solved:
如果通过下面这句话改变对CSS文件的引用,问题就解决了:
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<link rel="stylesheet" type="text/css" href="<c:url value="/recursos/estilos/test.css" />" media="all" />
If any moderator see this and know the explanation of how this is resolved, I kindly ask you to expose it here. Edit my answer, or comment it out, please. I'm sure other people can have the same doubt as me in the future.
如果任何版主看到这个并知道如何解决这个问题的解释,我恳请你在这里公开它。请编辑我的答案,或将其注释掉。我相信未来其他人也会和我有同样的疑问。
Thank you all for the attention and time.
感谢大家的关注和时间。
回答by Don Bottstein
Take a look at this blog entry. The relevant passage is:
看看这个博客条目。相关段落是:
If I want to apply main.css to url: www.mysite.com/index.html, I need to use following construction in HTML code:
< link href="resources/css/main.css" rel="stylesheet" type="text/css"/ >
If I want to apply main.css to url: www.mysite.com/some/location.html, I need to use following construction in HTML code:
< link href="../resources/css/main.css" rel="stylesheet" type="text/css"/ >
如果我想将 main.css 应用到 url: www.mysite.com/index.html,我需要在 HTML 代码中使用以下结构:
< link href="resources/css/main.css" rel="stylesheet" type="text/css"/ >
如果我想将 main.css 应用到 url: www.mysite.com/some/location.html,我需要在 HTML 代码中使用以下结构:
< link href="../resources/css/main.css" rel="stylesheet" type="text/css"/ >
As a possible workaround, perhaps this might work:
作为一种可能的解决方法,也许这可能有效:
<mvc:resources mapping="/**/recursos/**" location="/WEB-INF/recursos/" />
回答by kamoor
You can access resource as given below.
您可以访问如下所示的资源。
<link rel="stylesheet" type="text/css" href="/recursos/estilos/test.css" media="all" />
you are missing / at the beginning
你失踪了/在开始
回答by Paco Lagunas
Same was happening to me, and I found a solution. I hope this can help someone else:
我也遇到了同样的情况,找到了解决办法。我希望这可以帮助别人:
As you see in the error message, paths you use for resources /testeSpringMvc/path/to/recursos/estilos/test.css
are trying to be resolved by Spring's DispatchServlet.
正如您在错误消息中看到的,您用于资源的路径/testeSpringMvc/path/to/recursos/estilos/test.css
正试图由 Spring 的 DispatchServlet 解析。
This happens because in your web.xml file you have the following:
发生这种情况是因为在您的 web.xml 文件中有以下内容:
<servlet-mapping>
<servlet-name>fronte</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Your fronte
servlet will try to resolve anything that starts with /
.
您的fronte
servlet 将尝试解析任何以/
.
Just add a simple extension to you <url-pattern>
. Something like <url-pattern>/*.html</url-pattern>
should do the job.
只需为您添加一个简单的扩展<url-pattern>
。像<url-pattern>/*.html</url-pattern>
应该做的工作。