【问题标题】:Spring MVC resource MappingSpring MVC 资源映射
【发布时间】:2015-02-26 04:06:26
【问题描述】:

我在 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";
    }
}

我的 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>

我可以顺利访问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" />

一如既往,感谢您的关注和时间。

【问题讨论】:

    标签: java spring jsp spring-mvc tomcat


    【解决方案1】:

    同样的事情发生在我身上,我找到了解决方案。我希望这可以帮助其他人:

    正如您在错误消息中看到的,您用于资源 /testeSpringMvc/path/to/recursos/estilos/test.css 的路径正试图由 Spring 的 DispatchServlet 解析。

    这是因为在您的 web.xml 文件中有以下内容:

    <servlet-mapping>
        <servlet-name>fronte</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    

    您的fronte servlet 将尝试解析以/ 开头的任何内容。

    只需为您添加一个简单的扩展名&lt;url-pattern&gt;&lt;url-pattern&gt;/*.html&lt;/url-pattern&gt; 之类的东西应该可以完成这项工作。

    【讨论】:

    • 感谢您分享此内容。当然,我不是唯一一个经历这件事的人。我非常感谢您的关注和可用性。我相信其他人也会看到它,因为他们可能正在经历同样的情况。谢谢! :)
    【解决方案2】:

    我刚刚找到了问题的答案。我将把解决方案留在这里,但不幸的是我不明白它是如何解决所见场景的。

    我发现这可以通过JSTL 解决。我阅读了 JSTL 文档,发现的只是这样的描述:

    创建一个带有可选查询参数的 URL。

    如果对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" />
    

    如果有版主看到这个并知道如何解决这个问题的解释,我恳请你在这里公开它。请编辑我的答案,或将其注释掉。我敢肯定,以后其他人也会和我有同样的疑问。

    感谢大家的关注和时间。

    【讨论】:

      【解决方案3】:

      看看这个blog entry。相关段落是:

      如果我想将 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"/ >
      

      作为一种可能的解决方法,也许这可能有效:

      <mvc:resources mapping="/**/recursos/**" location="/WEB-INF/recursos/" />
      

      【讨论】:

      • 感谢您的回答。我尝试更改我的应用程序上下文文件,然后还尝试更改 page2.jsp,如上所述。不幸的是我没有得到结果。你还有什么建议吗?不管怎样,谢谢。
      • 我很清楚,您尝试将 page2.jsp 中的 css 链接更改为:&lt;link rel="stylesheet" type="text/css" href="../../recursos/estilos/test.css" media="all" /&gt;?
      • 其实我没做过。我之前尝试过别的东西。阅读您的评论后,我尝试了您的建议,不幸的是它没有奏效......我对此感到非常沮丧,我知道我做错了什么。我确信有办法以这种方式映射保持页面的样式。你知道一些可以向我表明我看起来的来源吗?我将永远感激不尽。以后如果找到答案,我会在这里与大家分享。还是谢谢你。
      【解决方案4】:

      您可以访问以下资源。

           <link rel="stylesheet" type="text/css" href="/recursos/estilos/test.css" media="all" />
      

      你在开头缺少 /

      【讨论】:

      • 感谢您的回复,但没有成功。现在 page1.jsp 也没有 CSS 样式了。
      • 当你直接点击css url时。返回的是什么?
      • 你能打开war文件看看test.css存放在哪里吗?
      • 查看页面源码时,浏览器提示找不到资源。我还没有在 WAR 中分发我的应用程序,因为我只是在测试它。但是 test.css 位于“styles”文件夹中,该文件夹位于“resources”中,而“resources”又位于“WEB-INF”和“webapp”中(目录结构在我的问题范围内)。
      • 你想在github上分享项目吗?
      猜你喜欢
      • 2014-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-13
      • 1970-01-01
      相关资源
      最近更新 更多