u123
u123

Reputation: 16329

Set start page for Spring MVC web application?

I have the following Spring project in eclipse:

enter image description here

When I go to:

http://[my-host]:8082/webapp-module/hello

The WEB/INF/jsp/hello.jsp page is loaded just fine. But I would also like to define a default start page (WEB/INF/index.jsp) when I go to:

http://[my-host]:8082/webapp-module

Currently that does not work. Do I need to add a separate controller for this?

My web.xml file:

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Spring MVC Application</display-name>

    <context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>/WEB-INF/webapp-module-servlet.xml</param-value>
    </context-param>

    <listener>
       <listener-class>
          org.springframework.web.context.ContextLoaderListener
       </listener-class>
    </listener>    

   <servlet>
      <servlet-name>webapp-module</servlet-name>
      <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <load-on-startup>1</load-on-startup>
   </servlet>

   <servlet-mapping>
      <servlet-name>webapp-module</servlet-name>
      <url-pattern>/</url-pattern>
   </servlet-mapping>

</web-app>

And my webapp-module-servlet.xml file:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   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:annotation-config/>

   <context:component-scan base-package="com.samples" />

   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/jsp/" />
      <property name="suffix" value=".jsp" />
   </bean>

</beans>

Upvotes: 4

Views: 11132

Answers (2)

Du-Lacoste
Du-Lacoste

Reputation: 12807


Briefly your web application will look into welcome page and /redirect is invoked which is captured by controller class and execute the logic you have implemented.

Add this in web.xml

 <welcome-file-list>
        <welcome-file>hello.jsp</welcome-file>
    </welcome-file-list>

In the JSP itself, add below coding:

<c:redirect url="/directMe"/>

Lastly, in the controller:

@RequestMapping(value = "/directMe", method = RequestMethod.GET)
        public void myMethod(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws IOException { 
// add your logic to execute
}

Upvotes: 0

Sanjay Rawat
Sanjay Rawat

Reputation: 2374

Step 1: Move index.jsp inside /WEB-INF/jsp/ folder.

Step 2: In your @Controller class add below method:

@RequestMapping("/") 
public String home(){
    return "index"; 
} 

Your complete Controller class should look like this:

@Controller 
public class LoginController { 

    @RequestMapping("/") 
    public String home(){
        return "index"; 
    }  

    @RequestMapping("/hello") 
    public String showhello(){
        return "hello"; 
    }   
} 

Upvotes: 6

Related Questions