MultiActionController example in

MultiActionController class of Spring MVC. We will also show you the example to use this class. You can download MultiActionController example and run in Eclipse IDE or reconfigure the code in IDE of your choice.

Ads

Tutorials   
Spring 4 Introduction and example Features of Spring Framework 5 How to make Spring web Login form? How to make Simple form in Spring web? Spring, Hibernate login and registration application Spring Framework Tutorial for beginners with examples Spring Framework for Apache Hadoop 2.3.0 GA released Spring Framework 4.1 GA is released with major features Why to use Spring Framework? Spring Framework 4.1 - First Release candidate available Spring IO Platform 1.0.0 Released Spring 4: Login Form using Spring MVC and Hibernate Example Spring 4 MVC Login form Example with source code Spring 4 MVC Hello World Example: Spring 4 MVC Tutorial will full source code Spring Web MVC Application Error:ClassNotFoundException: DispatcherServlet on deploying Features of Spring 4 Spring Framework 4.0 released Spring Framework 4: Spring Framework 4 Tutorials and Example Spring Integration 3.0 Release is released and available for download Spring Tutorial for Beginners Java Springs Framework Tutorial Spring Architecture Spring Framework Tutorials database spring registration form Spring Login Example Roseindia Spring Tutorial Spring Tutorial Spring 3.2 MVC insert and retrieve blob from the database The hidden tag The errors tag net.roseindia.dao net.roseindia.service net.roseindia.model net.roseindia.controller Spring 3.2 MVC Hibernate Example Spring 3.2 MVC, Upload File in a specific folder Spring 3.2 MVC Form Handling The textarea tag The options tag The option tag The select tag The radiobuttons tag The radiobutton tag The checkboxes tag mvc:default-servlet-handler Static Resources Configuration View Controllers Configuration View resolvers Spring 3.2 MVC Hello World Example Content Negotiation Configuration
Ads

MultiActionController example in

     

MultiActionController example in Spring 2.5 Web MVC

In this section we will learn about the MultiActionController class of Spring MVC. We will also show you the example to use this class. You can download MultiActionController example and run in Eclipse IDE or reconfigure the code in IDE of your choice.

Example of MultiActionController Class

MultiActionController class provides us a functionality that allow to bind the multiple request-handling methods in a single controller. The MultiActionController used MethodNameResolver or ParameterMethodNameResolver to find which method to be call when handling an incoming request. In this example we will discuss about how to use MultiActionController.

Step 1:

Now we will create a index.jsp that will have four hyperlinks to generates difference types of requests. The code of the index.jsp is:

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

<html>

<head>

<title>Multi Action Controller Example</title>

</head>

<body>

<h4>Multi Action Controller Example</h4>

<a href="add.html" >Add</a> <br/>

<a href="update.html" >Update</a><br/>

<a href="edit.html" >Edit</a> <br/>

<a href="remove.html" >Remove</a>

</body>

</html>

Step 2:

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

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

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http:
//java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

version="2.5">

<servlet>

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

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

</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 inside the /WEB-INF/  folder. The dispatcher-servlet.xml have the viewResolver that's provide a way to address views which enable you to render models in a browser without tying you to a specific view technology. The dispatcher-servlet.xml will have bean setting for provide the controller of these requests. The code for configure the MultiActionController in the dispatcher-servlet.xml is:

<bean name="/*.html" class="net.roseindia.web.MultiActionControllerExample" />

The full code of the dispatcher-servlet.xml 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 name="/*.html" class="net.roseindia.web.MultiActionControllerExample" />

</beans>

Step 4:

Now we will create a showmessage.jsp in the /WEB-INF/jsp/ folder. It will be display the response(that's return by the controller class) for the user. The code of the showmessage.jsp is:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

pageEncoding="ISO-8859-1"%>

<html>

<head>

<title>Success Page</title>

</head>

<body>

${message}

</body>

</html>

Step 5:

Now we will create a MultiActionControllerExample.java class inside the project src folder that extends the MultiActionController class. We will import "org.springframework.web.servlet.mvc.multiaction.MultiActionController" for extends MultiActionController class in MultiActionControllerExample.java. The MultiActionControllerExample.java class will have four methods for handle the user requests. The code of the MultiActionControllerExample.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.multiaction.MultiActionController;

public class MultiActionControllerExample extends MultiActionController {
  
  public ModelAndView add(HttpServletRequest request,
  HttpServletResponse responsethrows Exception {
  return new ModelAndView("showmessage""message""Add method called");
  }  
  public ModelAndView update(HttpServletRequest request,
  HttpServletResponse responsethrows Exception {
  return new ModelAndView("showmessage""message""Update method called");
  }
  public ModelAndView edit(HttpServletRequest request,
  HttpServletResponse responsethrows Exception {
  return new ModelAndView("showmessage""message""Edit method called");
  }
  public ModelAndView remove(HttpServletRequest request,
  HttpServletResponse responsethrows Exception {
  return new ModelAndView("showmessage""message""Remove method called");
  }
}

Step 6:

Now we will run this project and see the output like:

User can see four hyperlink in this page. User click on these links and see output like:

Click on Add link:

Click on Update link:

Click on Edit link:

Click on Remove link:

Download Code

Download this example code

Advertisements

Ads
Share on Google+Share on Google+

MultiActionController example in

Posted on: October 6, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: MultiActionController example in  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:4
John
August 19, 2011
Your code

Is so hard to read because of the huge ads on the right. I have 2 inches wide to view your code, and 5 inches for ads. Please increase your page width or something! :)
Duke Rathore
June 4, 2013
java

this example is best for beginners.!!!! thanxx a lot
Nagappa L M
September 26, 2012
index page is not loading

Dear ones, I downloaded the above project and imported,but index is not loading,i am getting underlined mark in web.xml on *.html.
Harsh Singh
April 7, 2013
Thanks your downloadable code is best

Thanks your downloadable code is best

Ads

 

Ads