Social Icons

Spring 2.0 MVC - MultiActionController

Let's check out a simple example to use Spring 2.0 MultiActionController

Notably, Spring provides a handful number of controllers each of which has their advantages of implementation. Personally, I prefer and use MultiActionController more often, mainly due to the way it actually works!


What you need ?

Eclipse - Download from http://www.eclipse.org
Spring 2.0 Libraries - Download from http://www.springsource.org/download/
JSTL 1.2.1 Libraries - Download from http://jstl.java.net/
Commons Logging Libraries - Download from http://commons.apache.org/logging/download_logging.cgi
Log4j Libraries - Download from http://logging.apache.org/log4j/1.2/download.html

Jars to be present in WEB-INF/lib folder
commons-logging-1.1.1.jar
javax.servlet.jsp.jstl-1.2.1.jar
javax.servlet.jsp.jstl-api-1.2.1.jar
log4j-1.2.14.jar
spring.jar

Now that we have all required libraries ready, let's look at what changes / additions you need to do to a empty web app to make it Spring enabled.

web.xml


Configure the dispatcher servlet in web.xml and servlet-mapping to intercept the requests.

DemoApp-servlet.xml

The SimpleUrlHandlerMapping maps the URLs to each Controller beans.
The ParameterMethodNameResolver defines that method name to be executed must be reolved from the URL parameter, while the defaultMethodName is set to 'loadPage'

AuthenticationController.java

This is the Controller class. Note that loadPage method is automatically executed when request comes to /AuthenticationController.do
If the request contains any param like /AuthenticationController.do?action=doLogout then doLogout will be executed. ModelAndView object is returned, which is mapped by the 'viewresolver' configuration in DemoApp-servlet.xml

index.jsp

Note the form action here.


home.jsp

Note the href tag for logout here.


 Download Eclipse project  Spring2MultiActionController.zip

0 comments:

Post a Comment