AbstarctController Example
We need:
·
Web.xml
·
spring-dispatcher-servlet.xml
·
HelloController.java
·
HelloPage.jsp
URL : http://localhost:8080/FirstSpringMVCProject/welcome.html
---->
(1)
Web.xml
<?xml version="1.0"
encoding="UTF-8"?>
<web-app id="WebApp_ID"
version="3.0" 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_3_0.xsd">
<display-name>FirstSpringMVCProject</display-name>
<servlet>
<servlet-name>spring-dispatcher</servlet-name>----> (3)
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> ----> (2)
</servlet>
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
spring-dispatcher-servlet.xml----> (4)
<beans xmlns="http://www.springframework.org/schema/beans"
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-2.5.xsd">
<bean id="HandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> ----> (5)
<bean name="/welcome.html"
class=" hellocontroller.HelloController" /> ----> (6)
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
>----> (11)
<property name="prefix">
<value>/WEB-INF/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
HelloController ---->
(7)
package hellocontroller;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
public class HelloController extends AbstractController{
@Override
protected ModelAndView
handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView
modelandview = new ModelAndView("HelloPage"); ---->
(9)
modelandview.addObject("welcomeMessage", "Hello Spring
MVC Application");----> (8)
return modelandview; ----> (10)
}
}
HelloPage.jsp ---->
(12)
<html>
<body>
<h1>First Spring MVC Application Demo</h1>
<h2>${welcomeMessage}</h2> ----> (13)
</body>
</html>
Output
First Spring MVC
Application Demo
Hello Spring MVC Application
Flow:
1.By Reading url , it contains
action as /welcome.html. web.xml reads the URL and Trasfrer to the Dispatcher
servlet
2.Dispatcher Servlet acts as a
FrontContoller, it will reads its Object name spring-dispatcher
3.DispatcherServlets searches the
xml which contains the name as spring-dispatcher-servlet.xml
4.spring-dispatcher-servlet.xml
will loads, and Request transfer to
HandlerMapping
5.HandlerMapping maps the request
to controller based on name /welcome.html
6.hellocontroller.HelloController
Object is created
7.hellocontroller.HelloController
will executes it's logic
8.adds the model data
9.adds the view name as
"HelloPage"
10.returns ModelAndView to HandlerMapping
11.HandlerMapping calls view resolver
for result page
12. It will return HelloPage.jsp
based on viewname = "HelloPage" and Suffex= .jsp
13.Output will Generate