Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC: how to create a default controller for index page?

I'm trying to do one of those standard spring mvc hello world applications but with the twist that I'd like to map the controller to the root. (for example: http://numberformat.wordpress.com/2009/09/02/hello-world-spring-mvc-with-annotations/ ) So the only real difference is that they map it to host\appname\something and I'd like to map it to host\appname.

I placed my index.jsp in src\main\webapp\jsp and mapped it in the web.xml as the welcome file. I tried:

@Controller("loginController") public class LoginController{    @RequestMapping("/")   public String homepage2(ModelMap model, HttpServletRequest request, HttpServletResponse response){     System.out.println("blablabla2");     model.addAttribute("sigh", "lesigh");     return "index";   } 

As my controller but I see nothing appear in the console of my tomcat. Does anyone know where I'm messing up?

My web.xml:

<?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_2_5.xsd"     version="2.5">      <!-- Index -->     <welcome-file-list>         <welcome-file>/jsp/index.jsp</welcome-file>     </welcome-file-list>      <context-param>         <param-name>contextConfigLocation</param-name>         <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>     </context-param>      <servlet>         <servlet-name>springweb</servlet-name>         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>         <load-on-startup>1</load-on-startup>     </servlet>      <servlet-mapping>         <servlet-name>springweb</servlet-name>         <url-pattern>/</url-pattern>     </servlet-mapping>  </web-app> 

The mvc-dispatcher-servlet.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:aop="http://www.springframework.org/schema/aop"     xmlns:context="http://www.springframework.org/schema/context"     xmlns:mvc="http://www.springframework.org/schema/mvc"     xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd         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:annotation-config />     <context:component-scan base-package="de.claude.test.*" />      <bean id="viewResolver"         class="org.springframework.web.servlet.view.UrlBasedViewResolver">         <property name="viewClass"             value="org.springframework.web.servlet.view.JstlView" />         <property name="prefix" value="/jsp/" />         <property name="suffix" value=".jsp" />     </bean>  </beans> 

I'm using Spring 3.0.5.release

Or is this not possible and do I need to put my index.jsp back in the root of the web-inf and put a redirect to somewhere inside my jsp so the controller picks it up?

like image 859
Hugo Avatar asked Mar 09 '11 20:03

Hugo


People also ask

What is default controller in Spring MVC?

The default handler is based on the @Controller and @RequestMapping annotations, offering a wide range of flexible handling methods. With the introduction of Spring 3.0, the @Controller mechanism also allows you to create RESTful Web sites and applications, through the @PathVariable annotation and other features.

Can we have multiple controllers in Spring MVC?

In Spring MVC, we can create multiple controllers at a time. It is required to map each controller class with @Controller annotation.


2 Answers

I had the same problem, even after following Sinhue's setup, but I solved it.

The problem was that that something (Tomcat?) was forwarding from "/" to "/index.jsp" when I had the file index.jsp in my WebContent directory. When I removed that, the request did not get forwarded anymore.

What I did to diagnose the problem was to make a catch-all request handler and printed the servlet path to the console. This showed me that even though the request I was making was for http://localhost/myapp/, the servlet path was being changed to "/index.html". I was expecting it to be "/".

@RequestMapping("*") public String hello(HttpServletRequest request) {     System.out.println(request.getServletPath());     return "hello"; } 

So in summary, the steps you need to follow are:

  1. In your servlet-mapping use <url-pattern>/</url-pattern>
  2. In your controller use RequestMapping("/")
  3. Get rid of welcome-file-list in web.xml
  4. Don't have any files sitting in WebContent that would be considered default pages (index.html, index.jsp, default.html, etc)

Hope this helps.

like image 69
Mike M. Lin Avatar answered Sep 17 '22 13:09

Mike M. Lin


The redirect is one option. One thing you can try is to create a very simple index page that you place at the root of the WAR which does nothing else but redirecting to your controller like

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <c:redirect url="/welcome.html"/> 

Then you map your controller with that URL with something like

@Controller("loginController") @RequestMapping(value = "/welcome.html") public class LoginController{ ... } 

Finally, in web.xml, to have your (new) index JSP accessible, declare

<welcome-file-list>     <welcome-file>index.jsp</welcome-file> </welcome-file-list> 
like image 21
Emmanuel Ballerini Avatar answered Sep 21 '22 13:09

Emmanuel Ballerini