Map / (root URL) in Spring MVC
Solution 1
Instead of mapping to /
you can declare a welcome page in your web.xml
file:
<welcome-file-list>
<welcome-file>welcome.htm</welcome-file>
</welcome-file-list>
so your /
path will be processed as /welcome.htm
and then if your controller is correctly mapped to /welcome.htm
it will process /
as if it was a /welcome.htm
request, without making changes to other configuration.
Solution 2
I'd recommend getting rid of the SimpleUrlHandlerMapping and just doing the following:
@Controller
@RequestMapping("/")
public class RootController
{
@RequestMapping(method=RequestMethod.GET)
public ModelAndView display(...)
{
...
}
}
This should get the result you want. Also, add <mvc:annotation-driven/>
to your servlet context with a <context:component-scan base-package="some.package.path.to.controller" />
to have Spring wire up that controller.
Otherwise, you can probably map the URL with the SimpleUrlHandlerMapping as so:
<property name="mappings">
<value>
/*=rootController
</value>
<property>
If done this way, you can keep the bean defined for rootController.
Admin
Updated on June 04, 2022Comments
-
Admin almost 2 years
This is something that I think should be very easy, but so far I have not been able to get it to work.
What I want to do is map my root path to a Spring MVC Controller. With a normal
Servlet
, I would just add a mapping for "/
" in myweb.xml
, and it would pick it up quite well. But with Spring MVC, not so much.I have tried many combinations, but none seem to work. I think the following one should work.
In
web.xml
:<servlet-mapping> <servlet-name>myDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
In my
contextConfigLocation
file:<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="alwaysUseFullPath" value="true"/> <property name="mappings"> <util:map> <entry key="/" value-ref="rootController"/> </util:map> </property> </bean> <bean id="rootController" class="my.package.RootController">
Then obviously, there is the case of the controller itself. I have no clue how to map the method to the actual root path. My attempt is something like this:
public class RootController extends MultiActionController { @RequestMapping("/") public ModelAndView display(HttpServletRequest request, HttpServletResponse response) throws Exception { final Map<String, Object> model = new HashMap<String, Object>(); model.put("someVariable", "Hello, MVC world."); return new ModelAndView("rootPage", model); } }
So let's say my application runs on
http://localhost:8080/app/
, I want that exact URL to execute the methoddisplay
. I do not want to type anything after/app/
. In fact, some things after/app/
are mapped to other controllers, and that all works fine (and they have to keep working).What am I missing here? Why is this not simply working? If I use the same
url-pattern
to map to a plainServlet
instance, it works fine and I reach thedoGet
method, but with Spring MVC I seem to missing some particular black magic to get this to work.