web.xml url 模式无法解析通配符



我正在运行简单的Spring MVC应用程序。在 web.xml 中,我使用了以下映射配置:

<servlet-mapping>
<servlet-name>spring-web</servlet-name>
<url-pattern></url-pattern> <!-- root request -->
<url-pattern>/endpoint/*</url-pattern> <!-- /endpoint/${name} -->
</servlet-mapping>

我的控制器定义为:

@RequestMapping(value = "/endpoint/{name}", method = RequestMethod.GET)
public ModelAndView render(@PathVariable("name") String name) {
ModelAndView genericRenderStructure = new ModelAndView();
genericRenderStructure.setViewName("WEB-INF/views/index.jsp");
genericRenderStructure.addObject("endpointName", name);
return genericRenderStructure;
}

然而,在访问路径/endpoints/something时不会触发渲染函数。当我将 url 模式更改为特定的变量名称(如<url-pattern>/endpoint/something</url-pattern>(时,页面开始处理此类 url。为什么通配符与我在 url 中使用的"任何"不匹配?这是 RequestMapping 值的问题吗?我正在使用雄猫服务器v9.0。

网络.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>Spring3 MVC Application</display-name>
<servlet>
<servlet-name>spring-web</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-web</servlet-name>
<url-pattern></url-pattern> <!-- root request -->
<url-pattern>/endpoint/*</url-pattern> <!-- /endpoint/${name} -->
</servlet-mapping>

spring-web-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.2.xsd
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="com.api.web.*" ></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/</value>
</property>
<property name="suffix">
<value></value>
</property>
</bean>
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven />
</beans>

提到了几个<url-pattern></url-pattern>,删除其中一个并尝试代替仅使用//*

这可能会奏效。

我不明白需要空的<url-pattern></url-pattern>模式。但似乎在达到实际模式之前,第一个正在匹配。使用此设置:

<servlet-mapping>
<servlet-name>spring-web</servlet-name>
<url-pattern>/endpoint/*</url-pattern> <!-- /endpoint/${name} -->
<url-pattern></url-pattern> <!-- root request -->
</servlet-mapping>

所以问题是<url-pattern>/endpoint/*</url-pattern>被认为是一个基础,应该从 RequestMapping 控制器中留下。当与@RequestMapping(value = "/endpoint/{name}", method = RequestMethod.GET)一起使用时,控制器被触发的url是两者的串联版本-/endpoint/endpoint/{name}。因此,正确的请求映射应@RequestMapping(value = "/{name}", method = RequestMethod.GET)

最新更新