Issue
I have the following Spring project in eclipse:
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>
Solution
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";
}
}
Answered By - Sanjay Rawat
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.