Java – how do I map spring MVC controllers to URIs with and without trailing slashes?
I have a spring controller with several requestmappings with different URIs My servlet is "UI" The servlet's base URI applies only to trailing slashes I hope my users don't have to enter trailing slashes
This URI is available:
http://localhost/myapp/ui/
This is not:
http://localhost/myapp/ui
It gives me an HTTP status 404 message
My web Servlets and mappings in XML are:
<servlet> <servlet-name>ui</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>ui</servlet-name> <url-pattern>/ui/*</url-pattern> </servlet-mapping>
My controller
@Controller public class UiRootController { @RequestMapping(value={"","/"}) public ModelAndView mainPage() { DataModel model = initModel(); model.setView("intro"); return new ModelAndView("main","model",model); } @RequestMapping(value={"/other"}) public ModelAndView otherPage() { DataModel model = initModel(); model.setView("otherPage"); return new ModelAndView("other",model); } }
Solution
If your web application exists in the webapps directory of the web server, such as webapps / myapp /, you can use http: / / localhost: 8080 / myapp / to access the root directory of this application context, assuming the default Tomcat port It should be possible to use or not use trailing slashes, which I think is the default - jetty V8, of course 1.5 situation
Once you call / myapp, the spring dispatcher servlet takes over and routes the request to < servlet name > on your web XML, in your case / UI / *
The dispatcher servlet then routes all requests from http: / / localhost / myapp / UI / to @ controller
In the controller itself, you can use @ requestmapping (value = "/ *") as the mainpage () method, which will cause http: / / localhost / myapp / UI / and http: / / localhost / myapp / UI to route to mainpage ()
Note: since spr-7064, you should also use spring > = v3 zero point three
For completeness, here are the files I tested:
Src / Master / Java / controller / uirootcontroller java
package controllers; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller public class UiRootController { @RequestMapping(value = "/*") public ModelAndView mainPage() { return new ModelAndView("index"); } @RequestMapping(value={"/other"}) public ModelAndView otherPage() { return new ModelAndView("other"); } }
WEB-INF / web. XML file
<?xml version="1.0" encoding="UTF-8"?> <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_3_0.xsd" version="3.0" Metadata-complete="false"> <servlet> <servlet-name>ui</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> <!-- spring automatically discovers /WEB-INF/<servlet-name>-servlet.xml --> </servlet> <servlet-mapping> <servlet-name>ui</servlet-name> <url-pattern>/ui/*</url-pattern> </servlet-mapping> </web-app>
WEB-INF / UI-servlet. In 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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" 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"> <context:component-scan base-package="controllers" /> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:order="2" p:viewClass="org.springframework.web.servlet.view.JstlView" p:prefix="/WEB-INF/views/" p:suffix=".jsp"/> </beans>
There are also two JSP files in WEB-INF / views / index JSP and WEB-INF / views / other jsp.
result:
>Index of http: / / localhost / myapp / – > directory list > http: / / localhost / myapp / UI and http: / / localhost / myapp / UI / – > JSP > http: / / localhost / myapp / UI / other and http: / / localhost / myapp / UI / other / – > other jsp
I hope this can help!