Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Spring AbstractController example

 
 

Share on Google+Share on Google+

AbstractController example

Advertisement
The example of AbstractController in Spring 2.5 MVC.

AbstractController example in Spring web MVC framework

     

AbstractController example in Spring web MVC framework

In this tutorial we are discussing AbstractContoller. The AbstractController class is extended by the programmer's to create the controller for their application.

Abstract controller provides a basic infrastructure, all of Spring's Controllers are inherited from AbstractController. The AbstractController is a class that offers caching support. You can extend AbstractController class and then implement your own controller from scratch. It should be used for simple use like returning a resource to the client without checking request parameters.

Here we will create a Spring 2.5 MVC web application that will be used AbstractController for creating the controller for the application.

Step 1:

Now we will create index.jsp inside the application WebContent folder. In the index.jsp, we will create a hyperlink for send request and controlled by the AbstractController. If user click on "Show Message" link then a message is appear on the user browser. The code of the index.jsp is:

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<html>

<body>

<a href="abstractcontroller.html">Show Message</a>

</body>

</html>

Step 2:

Now we will configured the web.xml file for DispatcherServlet. The code of the web.xml is:

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5"

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_5.xsd" >

<servlet>

<servlet-name>dispatcher</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>dispatcher</servlet-name>

<url-pattern>*.html</url-pattern>

</servlet-mapping>

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

</web-app>

Step 3:

Now we will create a dispatcher-servlet.xml in the WEB-INF folder. We will be create bean setting for the all requests in this file.

We will be create a viewResolver that resolve all the .html request in to .jsp. The code of the dispatcher-servlet.xml for viewResolver is:

<bean id="viewResolver"

class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="prefix">

<value>/WEB-INF/jsp/</value>

</property>

<property name="suffix">

<value>.jsp</value>

</property>

</bean>

<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/> 

we will create a bean for urlMapping that mapped the request for a particular controller. In this example we will create urlMapping for abstractcontroller.html to abstractcontroller. The code of the urlMapping in the dispatcher-servlet.xml is:

<bean id="urlMapping"

class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

<property name="interceptors">

<list>

<ref local="localeChangeInterceptor"/>

</list>

</property>

<property name="urlMap">

<map>

<entry key="/abstractcontroller.html">

<ref bean="abstractController"/>

</entry>

</map>

</property>

</bean>

<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">

<property name="paramName" value="hl"/>

</bean> 

  Now the full code of the dispatcher-servlet.xml for the AbstractController is:

<?xml version="1.0" encoding="UTF-8"?>

<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"

xmlns:p="http://www.springframework.org/schema/p">

<bean id="viewResolver"

class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="prefix">

<value>/WEB-INF/jsp/</value>

</property>

<property name="suffix">

<value>.jsp</value>

</property>

</bean>

<bean id="urlMapping"

class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

<property name="interceptors">

<list>

<ref local="localeChangeInterceptor"/>

</list>

</property>

<property name="urlMap">

<map>

<entry key="/abstractcontroller.html">

<ref bean="abstractController"/>

</entry>

</map>

</property>

</bean>

<bean id="abstractController" class="net.roseindia.web.ABSController"></bean>

<bean id="localeChangeInterceptor" class=
"org.springframework.web.servlet.i18n.LocaleChangeInterceptor"
>

<property name="paramName" value="hl"/>

</bean>

<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/>

</beans>

Step 4:

Now we will create a abscontroller.jsp inside the WEB-INF/jsp/ folder. This is the success page for display the message send by the ABSController.java. The code of the abscontroller.jsp is:

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

<html>

<body>

<c:out value="${message}"/>

</body>

</html>

Step 5:

Now we will create a ABSController.java class file inside the project src folder that extends AbstractController. This class controlled the request that have mapped for the particular request. This class accept the request and return the ModelAndView for display on the user browser. The code of the ABSController.java is:

package net.roseindia.web;

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 ABSController extends AbstractController 
  
 @Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
 HttpServletResponse response
throws Exception {

  String Mess = "Abstract Controller Test"
  ModelAndView modelAndView = new ModelAndView("abscontroller");
  modelAndView.addObject("message", Mess)
  return modelAndView;
  }
}

  Step 6:

When you run this application it will display output as shown below:

after click on the "Show Message" link that connect  this link with "abstractcontroller.html" and the message like:

Download Code

Download this example code

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: August 27, 2010

Related Tutorials

Discuss: AbstractController example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
roshan
September 14, 2012
regarding code example

the code example could be more less-bulky
DMCA.com