Spring MVC资源映射 [英] Spring MVC resource Mapping

查看:60
本文介绍了Spring MVC资源映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Spring MVC中测试了一些请求映射,但是在我的应用程序中遇到了一种奇怪的情况.我决定创建一个简单的方案,以便您可以了解我的问题.首先,我将向您展示我的项目的详细信息(源),然后再回答我的问题.

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.

我的项目中具有以下目录结构:

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

我的Tomcat部署描述符:

My Tomcat deployment descriptor:

<?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应用程序上下文:

My application context for 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控制器类:

My controller class for 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:

My 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文件.以下文本显示在我的控制台上:

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:

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),则发现文件没有问题...

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...

page1.jsp:

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的安全功能(安全约束).

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).

注意:我尝试使用contextPath帮助我设置CSS文件路径,但是没有任何效果.实际上,这种情况恶化了,因为Page1.jsp也没有样式:

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:

<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.

推荐答案

我刚刚找到了解决问题的答案.我将把解决方案留在这里,但是不幸的是,我不明白该解决方案是如何解决所看到的情况的.

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.

我发现可以使用 JSTL .我阅读了JSTL文档,发现的只是这个描述:

I have found that this can be solved with JSTL. I read the JSTL documentation, and all I found was this description:

使用可选的查询参数创建URL.

Creates a URL with optional query parameters.

如果通过以下语句更改对CSS文件的引用,则将解决该问题:

If reference to the CSS file is changed by the following sentence, the problem will be solved:

<%@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.

这篇关于Spring MVC资源映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆