gpt4 book ai didi

java - spring servlet url-pattern 映射不适用于星号

转载 作者:太空宇宙 更新时间:2023-11-04 06:14:59 25 4
gpt4 key购买 nike

web.xml 中,如果我使用如下完整路径指定了 servlet 映射:

<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/view/admin</url-pattern>
</servlet-mapping>

它工作正常,但如果我根据the specification使用星号(*) (第 12.2 章):

In the Web application deployment descriptor, the following syntax is used to define mappings:

  • A string beginning with a ‘ / ’ character and ending with a ‘ /* ’ suffix is used for path mapping.
  • A string beginning with a ‘ *. ’ prefix is used as an extension mapping. The empty string ("") is a special URL pattern that exactly maps to the application's context root, i.e., requests of the form http://host:port//. In this case the path info is ’ / ’ and the servlet path and context path is empty string (““).
  • A string containing only the ’ / ’ character indicates the "default" servlet of the application. In this case the servlet path is the request URI minus the context path and the path info is null.
  • All other strings are used for exact matches only.

像这样:

<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/view/*</url-pattern>
</servlet-mapping>

map 规则不起作用,glassfish 给出的日志如下:

[glassfish 4.0] [WARNING] [] [org.springframework.web.servlet.PageNotFound] [tid: _ThreadID=19 _ThreadName=http-listener-1(2)] [timeMillis: 1422503740955] [levelValue: 900] [[No mapping found for HTTP request with URI [/view/admin] in DispatcherServlet with name 'spring']]

应该是哪里出了问题?(我已经找到了导致此问题的原因,请继续阅读)

<小时/>

有关 Spring MVC 配置和 Controller 类的其他信息

以下是评论所需的更多附加信息。

完整的web.xml是:

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/view/*</url-pattern>
</servlet-mapping>
</web-app>

这是spring-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd ">

<mvc:annotation-driven></mvc:annotation-driven>

<context:component-scan base-package="com.ksider.service.searchserver.controller"></context:component-scan>


<bean id="freemarkerSettings" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/view/" />
<property name="freemarkerSettings">
<props>

<prop key="template_update_delay">0</prop>
<prop key="default_encoding">UTF-8</prop>
<prop key="locale">zh_CN</prop>
<prop key="url_escaping_charset">UTF-8</prop>
<prop key="whitespace_stripping">true</prop>
<prop key="date_format">yyyy-MM-dd</prop>
<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
<prop key="number_format">0.##</prop>
<prop key="classic_compatible">true</prop>
</props>
</property>
<property name="freemarkerVariables">
<map>
<entry key="xml_escape" value-ref="fmXmlEscape" />
</map>
</property>
</bean>

<bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape"/>


<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"/>

<!-- <property name="viewNames" value="*.ftl"/> -->
<property name="contentType" value="text/html; charset=utf-8"/>
<property name="cache" value="true" />
<property name="prefix" value="" />
<property name="suffix" value=".ftl" />
<property name="order" value="1"/>
<property name="exposeSpringMacroHelpers" value="true"/>
<property name="requestContextAttribute" value="rc"/>
</bean>
</beans>

这是 Controller :

package com.ksider.service.searchserver.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class AdminController {
@RequestMapping(value = "/view/admin", method = RequestMethod.GET)
public String getAdminPage(ModelMap modelMap){
modelMap.put("Msg","hello");
return "admin/index";
}
}
<小时/>

进度更新:

如果我使用的话,我很喜欢是什么原因造成的

 <servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/view/*</url-pattern>
</servlet-mapping>

而不是

 <servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/view/admin</url-pattern>
</servlet-mapping>

我应该如何为每个请求添加前缀 /view ,例如如果我想查询 http://localhost:8080/view/admin 我应该查询 http://localhost:8080/view/view/admin

我认为这是一个非常令人困惑的问题:-(

我只是检查了 servlet 映射引用,但无法找到为什么会发生这种情况的线索。也许应该归咎于一些Spring mvc技巧? enter image description here

所以,问题变成了:

  • 这是什么原因造成的?我应该怎样做才能避免为每个查询添加前缀?

最佳答案

有两个不同的问题。

首先是 servlet 容器如何映射到 servlet。您已经正确地解释了:如果 servlet 映射到 /view/admin/view/* 它同样会接收请求。

接下来是spring DispatcherServlet如何解码和处理请求。 Servlet 规范 3.0(3.5 请求路径元素)说:

  • 当 servlet 映射到 /view/admin 时,servlet 路径为 /view/admin 并且路径信息为 null
  • 当 servlet 映射到 /view/* 时,servlet 路径为 /view 且路径信息 ID /admin

当 DispatcherServlet 找到空路径信息时,它会在其映射中查找 servlet 路径(并找到您的 Controller ),但如果路径信息不为空,则假定 servlet 路径是不在映射中的前缀。

这就是为什么您在 /view/view/admin 下找到 Controller 的原因:第一个 /view 被 servlet 路径占用,路径信息 /view/admin 是 Controller 的映射。

当您将 DispatcherServlet 映射到 /view/* 时, Controller 映射不得包含 servlet 路径 => 您的 Controller 映射应为 /admin

关于java - spring servlet url-pattern 映射不适用于星号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28207018/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com