Reputation: 2294
I am new to spring and was working on the Hello world application in MVC The URL mapping is searched as a corresponding view when i return the ModelAndView object from the controller..I have included all proper jars.This is the code.. /web-inf/sample-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<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"> (http://www.springframework.org/schema/context/spring-context-3.0.xsd%27%3E)
<context:component-scan base-package="com.tcs.laks.sample.controller" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean> </beans>
This is the web.xml---
<?xml version="1.0" encoding="UTF-8"?>
<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"> (http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd%27%3E)
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>sample</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>sample</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
The HelloWorldController is..
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.portlet.ModelAndView;
@Controller
@RequestMapping("/welcome")
public class HelloWorldController {
@RequestMapping(method = RequestMethod.GET)
public ModelAndView helloWorld(){
String message = "Hello";
return new ModelAndView("hello", "message", message);
}
}
The url was given as //localhost:8080/sample/welcome it gave 404 as it was trying to find welcome.jsp instead of hello.jsp
HTTP Status 404 - /sample/WEB-INF/jsp/welcome.jsp
type Status report
message /sample/WEB-INF/jsp/welcome.jsp
description The requested resource (/sample/WEB-INF/jsp/welcome.jsp) is not available.
Upvotes: 0
Views: 477
Reputation: 2817
You are using wrong ModelAndView class
import org.springframework.web.portlet.ModelAndView;
it should have been
import org.springframework.web.servlet.ModelAndView;
everything else is fine.
Upvotes: 1
Reputation: 94469
Specify the contextConfigLocation
init-param for the dispatcher servlet within your web.xml file. The current configuration is even being read/used by the dispatcher
<servlet>
<servlet-name>sample</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/sample-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>sample</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Also it looks like you have imported the portlet version of ModelAndView, make sure this is what you desire. If not import, org.springframework.web.servlet.ModelAndView
Upvotes: 1