java - Spring MVC 资源映射

标签 java spring jsp spring-mvc tomcat

我一直在测试 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 Controller 类:

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 :

@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" />

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

最佳答案

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

正如您在错误消息中看到的,您用于资源的路径 /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 将尝试解析任何以 / 开头的内容.

加个简单的扩展就给你了<url-pattern> .类似于 <url-pattern>/*.html</url-pattern>应该完成这项工作。

关于java - Spring MVC 资源映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27697878/

相关文章:

spring - 请求的 bean 当前正在创建中 : Is there an unresolvable circular reference?

java - org.springframework.beans.factory.UnsatisfiedDependencyException : Error creating bean with name userController : Unsatisfied dependency

java - 如何将 map 存储在Spring项目使用的.properties文件中?

java - 为什么需要更多的类加载器?

java - 如何将dict的值分成两部分

java - 读取输入流并根据分隔符拆分

java - 不支持身份验证方法 : GET

java - 哪个配置文件告诉 Tomcat 安装 JRE 的位置?

JQuery 与 JavaEE 集成

java - 如何在不使用模式匹配的情况下使用 java api 查询 “like” 的 mongodb?