Note that there are some explanatory texts on larger screens.

plurals
  1. POMap / (root URL) in Spring MVC
    text
    copied!<p>This is something that I think should be very easy, but so far I have not been able to get it to work.</p> <p>What I want to do is map my root path to a Spring MVC Controller. With a normal <code>Servlet</code>, I would just add a mapping for "<code>/</code>" in my <code>web.xml</code>, and it would pick it up quite well. But with Spring MVC, not so much.</p> <p>I have tried many combinations, but none seem to work. I think the following one should work.</p> <p>In <code>web.xml</code>:</p> <pre><code>&lt;servlet-mapping&gt; &lt;servlet-name&gt;myDispatcherServlet&lt;/servlet-name&gt; &lt;url-pattern&gt;/&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; </code></pre> <p>In my <code>contextConfigLocation</code> file:</p> <pre><code>&lt;bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"&gt; &lt;property name="alwaysUseFullPath" value="true"/&gt; &lt;property name="mappings"&gt; &lt;util:map&gt; &lt;entry key="/" value-ref="rootController"/&gt; &lt;/util:map&gt; &lt;/property&gt; &lt;/bean&gt; &lt;bean id="rootController" class="my.package.RootController"&gt; </code></pre> <p>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:</p> <pre><code>public class RootController extends MultiActionController { @RequestMapping("/") public ModelAndView display(HttpServletRequest request, HttpServletResponse response) throws Exception { final Map&lt;String, Object&gt; model = new HashMap&lt;String, Object&gt;(); model.put("someVariable", "Hello, MVC world."); return new ModelAndView("rootPage", model); } } </code></pre> <p>So let's say my application runs on <code>http://localhost:8080/app/</code>, I want that exact URL to execute the method <code>display</code>. I do not want to type anything after <code>/app/</code>. In fact, some things after <code>/app/</code> are mapped to other controllers, and that all works fine (and they have to keep working).</p> <p>What am I missing here? Why is this not simply working? If I use the same <code>url-pattern</code> to map to a plain <code>Servlet</code> instance, it works fine and I reach the <code>doGet</code> method, but with Spring MVC I seem to missing some particular black magic to get this to work.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload