Java – how to map the root directory of URL schema directory in servlet mapping in spring MVC?
<servlet-mapping>
<servlet-mapping> <servlet-name>testServlet</servlet-name> <url-pattern>/test/*</url-pattern> </servlet-mapping>
If I type / test / the page will work However, playing / testing or / testing / will not work I use spring MVC, and my request mapping is as follows:
@RequestMapping(value = {"","/"})
Edit:
I'm using a separate project for validation, but this seems to be the error of spring's urlpathhelper When there is a context and servlet path, the following method returns an incorrect path, and you click the servlet without a trailing slash
public String getPathWithinApplication(HttpServletRequest request) { String contextPath = getContextPath(request); String requestUri = getRequestUri(request); if (StringUtils.startsWithIgnoreCase(requestUri,contextPath)) { // Normal case: URI contains context path. String path = requestUri.substring(contextPath.length()); return (StringUtils.hasText(path) ? path : "/"); } else { // Special case: rather unusual. return requestUri; } }
As an example, I said that I have a "admin" and the following servlet mapping context:
<servlet-mapping> <servlet-name>useRSServlet</servlet-name> <url-pattern>/users/*</url-pattern> </servlet-mapping>
Now I have a request mapping in one of my controllers, as follows:
@RequestMapping(value = {"","/"})
If I click / admin / users, it won't work properly But if I click / admin / users / it will work Now, if I change the request mapping to the following, they all work:
@RequestMapping(value = {"/users","/"})
However, now URL / admin / users / users will also work (this is not what I want)
Solution
Yevgeniy is correct, but if your dispatcher servlet takes over the default servlet, it must be added to your web In XML:
<welcome-file-list> <welcome-file>/</welcome-file> </welcome-file-list>