当我想访问具有此模式的网址并忽略任何扩展名时,我的问题就开始了
localhost:8084/project/foo/1
我得到了这个 web.xml 配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/foo</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
实际上,要访问该网址,我需要使用 /foo/1.html
并且我想使用 /foo/1
,因为我将使用该数字作为 id
来查找对象,并且我不知道是否遗漏了某些内容,但此配置不起作用。
Controller 代码
@Controller
public class FooController {
@RequestMapping(value = "/foo/{id}")
public ModelAndView telefono(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, @PathVariable String id) {
Map<String, Object> model = new HashMap<String, Object>();
// Code to search foo
return new ModelAndView("foo", model);
}
}
最佳答案
你试过吗
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/foo/*</url-pattern>
</servlet-mapping>
检查this out
尝试一下。
@RequestMapping(value = "/{id}")
当/foo 重定向到 servlet 时,make/foo/1 将尝试查找/foo/foo/1
关于Java - 多个 url 模式且无扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21632996/