I have a Spring Controller with several RequestMappings for different URIs. My servlet is "ui". The servlet's base URI only works with a trailing slash. I would like my users to not have to enter the trailing slash.
This URI works:
http://localhost/myapp/ui/
This one does not:
http://localhost/myapp/ui
It gives me a HTTP Status 404 message.
The servlet and mapping from my web.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", model);
}
}
If your web application exists in the web server's webapps directory, for example
webapps/myapp/
then the root of this application context can be accessed athttp://localhost:8080/myapp/
assuming the default Tomcat port. This should work with or without the trailing slash, I think by default - certainly that is the case in Jetty v8.1.5Once you hit
/myapp
the Spring DispatcherServlet takes over, routing requests to the<servlet-name>
as configured in yourweb.xml
, which in your case is/ui/*
.The DispatcherServlet then routes all requests from
http://localhost/myapp/ui/
to the@Controller
s.In the Controller itself you can use
@RequestMapping(value = "/*")
for the mainPage() method, which will result in bothhttp://localhost/myapp/ui/
andhttp://localhost/myapp/ui
being routed to mainPage().Note: you should also be using Spring >= v3.0.3 due to SPR-7064
For completeness, here are the files I tested this with:
src/main/java/controllers/UIRootController.java
WEB-INF/web.xml
WEB-INF/ui-servlet.xml
And also 2 JSP files at
WEB-INF/views/index.jsp
andWEB-INF/views/other.jsp
.Result:
http://localhost/myapp/
-> directory listinghttp://localhost/myapp/ui
andhttp://localhost/myapp/ui/
-> index.jsphttp://localhost/myapp/ui/other
andhttp://localhost/myapp/ui/other/
-> other.jspHope this helps!
I eventually added a new RequestMapping to redirect the /ui requests to /ui/. Also removed the empty string mapping from the mainPage's RequestMapping. No edit required to web.xml.
Ended up with something like this in my controller:
Now the URL
http://myhost/myapp/ui
redirects tohttp://myhost/myapp/ui/
and then my controller displays the introductory page.try adding
@RequestMapping(method = RequestMethod.GET) public String list() { return "redirect:/strategy/list"; }
the result:
** credits:
Advanced @RequestMapping tricks – Controller root and URI Template
For Java-based configuration
For XML-based configuration
Cheers!
Using Springboot, my app could reply both with and without trailing slash by setting @RequestMapping's "value" option to the empty string:
Another solution I found is to not give the request mapping for mainPage() a value: