【发布时间】:2020-03-27 00:18:30
【问题描述】:
我正在运行简单的 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-pattern 更改为特定的变量名时,例如<url-pattern>/endpoint/something</url-pattern>,页面开始使用这样的 url。为什么通配符与我在 url 中使用的“whatever”不匹配?这是 RequestMapping 值的问题吗?我正在使用 Tomcat 服务器 v9.0。
web.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>
【问题讨论】:
标签: spring spring-mvc web.xml request-mapping