Nearby lessons

19 of 35

Spring - MultiActionController

📌 What You Will Learn
  • Understand Spring - MultiActionController
  • See working code examples
  • Learn from common mistakes and Q&A

Learn Spring - MultiActionController step by step — simple explanations, complete programs with their output, common beginner mistakes, and exam-style MCQs.

MultiActionController — web.xml

Example01
JCode Cell
1 
2<?xml version="1.0" encoding="UTF-8"?>
3<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
4<servlet>
5 <servlet-name>ds</servlet-name>
6 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
7 <load-on-startup>1</load-on-startup>
8</servlet>
9<servlet-mapping>
10 <servlet-name>ds</servlet-name>
11<url-pattern>/</url-pattern>
12</servlet-mapping>
13</web-app>
14

Internal Flow

handleRequestInternal() method will be executed, it will return "viewName" property value as ModelAndView object , that is, logical name of the View, where ViewResolver will resolve the View Page and generate the respective webpage as response.

EX:

public class ParameterizableViewController extends Abstractcontroller{

public ModelAndView handleRequestInternal(HttpServletRequest request, HttpservletResponse response)throws Exception{

return new ModelAndView(getViewName()); } }

MultiActionController

In general, in Spring WEB MVC applications, we will for each and every form or an URI we will prepare a separate Controller class depending on our requirement.

In the above approach, if we want to provide a particular Entity related actions like save, update, search and delete then we have to prepare seperate Controller classes like SaveController, UpdateController, SearchController, DeleteController... it will increase no of controller classes unnecessarily. In the above context, to reduce no of controller classes, SPring Framework has provided an alternative in the fomr of

"org.springframework.web.servlet.mvc.multiaction.MultiActionController".

The main intention of MultiActionController class is to group related actions into a single controller class.

If we want to use MultiAction controller class in Spring WEB MVC applications then we have to declare an user defined class as sub class to "MultiActionController" and we have to define action methods with the same incoming URI names.

public (ModelAndView | Map | String | void) actionName(HttpServletRequest request, HttpServletResponse response);

Example03
JCode Cell
1 
2public class StudentController extends MultiActionController{
3public ModelAndView save(HttpServletRequest req, HttpServletResponse res)throws Exception{
4----
5}
6public ModelAndView search(HttpServletRequest req, HttpServletResponse res)throwsException{
7----
8}
9public ModelAndView update(HttpServletRequest req, HttpServletResponse res)throwsException{
10----
11}
12public ModelAndView delete(HttpServletRequest req, HttpServletResponse res)throwsException{
13----
14}
15}
16 
17To trap all request to the MultiActionController class and to execute action methods we have to use '/' as "name" attribute value in "bean" configuration in Spring Configuration File.
18 
19<bean name="/" class="com.durgasoft.controller.StudentController"/>
20 
21In the above context, if we submit "http://lh:1010/app/save" then save() method in StudentAction class will be executed, similarily, search(), update(), delete(),.. methods will be executed as per the URI values from clients like search, update, delete...
22

MultiActionController — index.jsp

Example:

Example04
JCode Cell
1 
2<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
3pageEncoding="ISO-8859-1"%>
4<!DOCTYPE html>
5<html>
6<head>
7<meta charset="ISO-8859-1">
8<title>Insert title here</title>
9</head>
10<body>
11<jsp:forward page="home"/>
12</body>
13</html>
14

MultiActionController — home.jsp

Example05
JCode Cell
1 
2<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
3pageEncoding="ISO-8859-1"%>
4<!DOCTYPE html>
5<html>
6<head>
7<meta charset="ISO-8859-1">
8<title>Insert title here</title>
9</head>
10<body>
11<br><br><br>
12<center>
13<a href="addpage">Add Form</a><br><br>
14<a href="searchpage">Search Form</a><br><br>
15<a href="updatepage">Update Form</a><br><br>
16<a href="deletepage">Delete Form</a>
17</center>
18</body>
19</html>
20

MultiActionController — addform.jsp

Example06
JCode Cell
1 
2<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
3pageEncoding="ISO-8859-1"%>
4<!DOCTYPE html>
5<html>
6<head>
7<meta charset="ISO-8859-1">
8<title>Insert title here</title>
9</head>
10<body>
11<br><br><br>
12<center>
13<h2 style="color: red">Durga Software Solutions</h2>
14<h3 style="color: blue">Student Add Form</h3>
15<form action="add" method="POST">
16 
17<table>
18<tr>
19<td>Student Id</td>
20<td><input type="text" name="sid"/></td>
21</tr>
22<tr>
23<td>Student Name</td>
24<td><input type="text" name="sname"/></td>
25</tr>
26<tr>
27<td>Student Address</td>
28<td><input type="text" name="saddr"/></td>
29</tr>
30<tr>
31<td><input type="submit" value="ADD"/></td>
32</tr>
33</table>
34 
35</form>
36</center>
37</body>
38</html>
39

MultiActionController — searchform.jsp

Example07
JCode Cell
1 
2<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
3pageEncoding="ISO-8859-1"%>
4<!DOCTYPE html>
5<html>
6<head>
7<meta charset="ISO-8859-1">
8<title>Insert title here</title>
9</head>
10<body>
11<center>
12<br><br><br>
13<h2 style="color: red">Durga Software Solutions</h2>
14<h3 style="color: blue">Student Search Form</h3>
15<form action="search" method="POST">
16<table>
17<tr>
18<td>Student Id</td>
19<td><input type="text" name="sid"/></td>
20</tr>
21<tr>
22<td><input type="submit" value="SEARCH"/></td>
23</tr>
24</table>
25</form>
26</center>
27</body>
28</html>
29

MultiActionController — updateform.jsp

Example08
JCode Cell
1 
2<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
3pageEncoding="ISO-8859-1"%>
4<!DOCTYPE html>
5<html>
6<head>
7<meta charset="ISO-8859-1">
8<title>Insert title here</title>
9</head>
10<body>
11<br><br><br>
12<center>
13<h2 style="color: red">Durga Software Solutions</h2>
14<h3 style="color: blue">Student Update Form</h3>
15<form action="editform" method="POST">
16<table>
17<tr>
18<td>Student Id</td>
19<td><input type="text" name="sid"/></td>
20</tr>
21<tr>
22<td><input type="submit" value="GetEditForm"/></td>
23</tr>
24</table>
25</form>
26</center>
27</body>
28</html>
29

MultiActionController — student_edit_form.jsp

Example09
JCode Cell
1 
2<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
3pageEncoding="ISO-8859-1"%>
4<%@page isELIgnored="false" %>
5<!DOCTYPE html>
6<html>
7<head>
8<meta charset="ISO-8859-1">
9<title>Insert title here</title>
10</head>
11<body>
12<br><br><br>
13<center>
14<h2 style="color: red">Durga Software Solutions</h2>
15<h3 style="color: blue">Student Edit Form</h3>
16<form method="POST" action="update">
17<table>
18<tr>
19<td>Student Id</td>
20<td>${sto.sid} <input type="hidden" name="sid" value='${sto.sid}'/></td>
21</tr>
22<tr>
23<td>Student Name</td>
24<td><input type='text' name='sname' value='${sto.sname}'/></td>
25</tr>
26<tr>
27<td>Student Address</td>
28<td><input type='text' name='saddr' value='${sto.saddr}'/></td>
29</tr>
30<tr>
31<td><input type="submit" value="UPDATE"></td>
32</tr>
33</table>
34 
35</form>
36</center>
37</body>
38 
39</html>
40

MultiActionController — deleteform.jsp

Example10
JCode Cell
1 
2<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
3pageEncoding="ISO-8859-1"%>
4<!DOCTYPE html>
5<html>
6<head>
7<meta charset="ISO-8859-1">
8<title>Insert title here</title>
9</head>
10<body>
11<br><br><br>
12<center>
13<h2 style="color: red" align="center">Durga Software Solutions</h2>
14<h3 style="color: blue" align="center">Student Update Form</h3>
15<form action="delete" method="POST">
16<table >
17<tr>
18<td>Student Id</td>
19<td><input type="text" name="sid"/></td>
20</tr>
21<tr>
22<td><input type="submit" value="DELETE"/></td>
23</tr>
24</table>
25</form>
26</center>
27</body>
28</html>
29

MultiActionController — student.jsp

Example11
JCode Cell
1 
2<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
3pageEncoding="ISO-8859-1"%>
4<%@page isELIgnored="false" %>
5<!DOCTYPE html>
6<html>
7<head>
8<meta charset="ISO-8859-1">
9<title>Insert title here</title>
10</head>
11<body>
12<br><br><br>
13<center>
14<h2 style="color: red">Durga Software Solutions</h2>
15<h3 style="color: blue">Student Details</h3>
16<table border='1'>
17<tr>
18<td>Student Id</td>
19<td>${sto.sid}</td>
20</tr>
21<tr>
22<td>Student Name</td>
23<td>${sto.sname}</td>
24</tr>
25<tr>
26<td>Student Address</td>
27<td>${sto.saddr}</td>
28</tr>
29</table>
30<h3>
31<a href="home">Home Page</a>
32</h3>
33</center>
34</body>
35</html>
36

MultiActionController — status.jsp

Example12
JCode Cell
1 
2<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
3pageEncoding="ISO-8859-1"%>
4<%@page isELIgnored="false" %>
5<!DOCTYPE html>
6<html>
7<head>
8<meta charset="ISO-8859-1">
9<title>Insert title here</title>
10</head>
11<body>
12<br><br><br>
13<h2 style="color: red" align="center">
14${message}
15</h2>
16<h3 align="center">
17<a href="home">Home Page</a>
18</h3>
19</body>
20</html>
21

MultiActionController — HomeController.java

Example13
JCode Cell
1 
2package com.durgasoft.controller;
3 
4import javax.servlet.http.HttpServletRequest;
5import javax.servlet.http.HttpServletResponse;
6 
7import org.springframework.web.servlet.ModelAndView;
8import org.springframework.web.servlet.mvc.AbstractController;
9 
10public class HomeController extends AbstractController {
11 
12@Override
13protected ModelAndView handleRequestInternal(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
14 
15return new ModelAndView("home");
16}
17}
18

MultiActionController — StudentAction.java

Example14
JCode Cell
1 
2package com.durgasoft.controller;
3 
4import javax.servlet.http.HttpServletRequest;
5import javax.servlet.http.HttpServletResponse;
6 
7import org.springframework.web.servlet.ModelAndView;
8import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
9 
10import com.durgasoft.dao.StudentDao;
11import com.durgasoft.dto.StudentTo;
12 
13public class StudentAction extends MultiActionController {
14StudentDao studentDao;
15String status = "";
16String message = "";
17 
18public void setStudentDao(StudentDao studentDao) {
19this.studentDao = studentDao;
20}
21 
22public ModelAndView add(HttpServletRequest request, HttpServletResponse response) throws Exception{
23StudentTo sto = new StudentTo();
24sto.setSid(request.getParameter("sid"));
25sto.setSname(request.getParameter("sname"));
26sto.setSaddr(request.getParameter("saddr"));
27status = studentDao.add(sto);
28if(status.equals("success")) {
29 
30 message = "Student Added Successfully";
31}
32if(status.equals("failure")) {
33 message = "Student Insertion Failure";
34}
35if(status.equals("existed")) {
36 message = "Student Existed Already";
37}
38return new ModelAndView("status", "message", message);
39}
40 
41public ModelAndView search(HttpServletRequest request, HttpServletResponse response)throws Exception{
42String sid = request.getParameter("sid");
43StudentTo sto = studentDao.search(sid);
44ModelAndView mav = null;
45if(sto == null) {
46 mav = new ModelAndView("status", "message", "Student Not Existed");
47}else {
48 mav = new ModelAndView("student", "sto", sto);
49}
50return mav;
51}
52 
53public ModelAndView editform(HttpServletRequest request, HttpServletResponse response)throws Exception{
54String sid = request.getParameter("sid");
55StudentTo sto = studentDao.search(sid);
56ModelAndView mav = null;
57if(sto == null) {
58 mav = new ModelAndView("status", "message", "Student Not Existed");
59}else {
60 mav = new ModelAndView("student_edit_form", "sto", sto);
61}
62return mav;
63}
64 
65public ModelAndView update(HttpServletRequest request, HttpServletResponse response)throws Exception{
66StudentTo sto = new StudentTo();
67sto.setSid(request.getParameter("sid"));
68sto.setSname(request.getParameter("sname"));
69sto.setSaddr(request.getParameter("saddr"));
70 
71String status = studentDao.update(sto);
72ModelAndView mav = null;
73if(status.equals("success")) {
74 mav = new ModelAndView("status", "message", "Student Updation Success");
75}else {
76 mav = new ModelAndView("status", "message", "Student Updation Failure");
77}
78return mav;
79}
80 
81public ModelAndView delete(HttpServletRequest request, HttpServletResponse response)throws Exception{
82ModelAndView mav = null;
83String sid = request.getParameter("sid");
84status = studentDao.delete(sid);
85if(status.equals("success")) {
86 mav = new ModelAndView("status", "message", "Student Deleted Successfully");
87}
88if(status.equals("notexisted")) {
89 mav = new ModelAndView("status", "message", "Student Not Existed");
90}
91if(status.equals("failure")) {
92 mav = new ModelAndView("status", "message", "Student Deletion Failure");
93}
94return mav;
95}
96}
97

MultiActionController — StudentDao.java

Example15
JCode Cell
1 
2package com.durgasoft.dao;
3 
4import com.durgasoft.dto.StudentTo;
5 
6public interface StudentDao {
7public String add(StudentTo sto);
8public StudentTo search(String sid);
9public String update(StudentTo sto);
10public String delete(String sid);
11}
12 
13StudentDaoImpl.java
14--------------------
15package com.durgasoft.dao;
16 
17import java.util.List;
18 
19import org.springframework.jdbc.core.JdbcTemplate;
20 
21import com.durgasoft.dto.StudentTo;
22 
23public class StudentDaoImpl implements StudentDao {
24private JdbcTemplate jdbcTemplate;
25String status = "";
26public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
27this.jdbcTemplate = jdbcTemplate;
28}
29 
30@Override
31public String add(StudentTo sto) {
32String query1 = "select * from student where SID = '"+sto.getSid()+"'";
33List<StudentTo> std_List = jdbcTemplate.query(query1, (resultSet, i) -> {
34 StudentTo std_Entity = new StudentTo();
35 std_Entity.setSid(resultSet.getString("SID"));
36 std_Entity.setSname(resultSet.getString("SNAME"));
37 std_Entity.setSaddr(resultSet.getString("SADDR"));
38 return std_Entity;
39});
40if(std_List.isEmpty()) {
41 String query2 = "insert into student values('"+sto.getSid()+"','"+sto.getSname()+"','"+sto.getSaddr()+"')";
42 int rowCount = jdbcTemplate.update(query2);
43 if(rowCount == 1) {
44 status = "success";
45 }else {
46 status = "failure";
47 }
48}else {
49 status = "existed";
50}
51 
52return status;
53}
54@Override
55public StudentTo search(String sid) {
56StudentTo sto = null;
57String query = "select * from student where SID = '"+sid+"'";
58List<StudentTo> std_List = jdbcTemplate.query(query, (resultSet, i) ->{
59 StudentTo std_Entity = new StudentTo();
60 std_Entity.setSid(resultSet.getString("SID"));
61 std_Entity.setSname(resultSet.getString("SNAME"));
62 std_Entity.setSaddr(resultSet.getString("SADDR"));
63 return std_Entity;
64});
65if(std_List.isEmpty()) {
66 sto = null;
67}else {
68 sto = std_List.get(0);
69}
70return sto;
71}
72 
73@Override
74public String update(StudentTo sto) {
75String query = "update student set SNAME = '"+sto.getSname()+"', SADDR = '"+sto.getSaddr()+"' where SID = '"+sto.getSid()+"'";
76int rowCount = jdbcTemplate.update(query);
77if(rowCount == 1) {
78 status = "success";
79}else {
80 status = "failure";
81}
82return status;
83}
84 
85@Override
86public String delete(String sid) {
87String query1 = "select * from student where SID = '"+sid+"'";
88List<StudentTo> std_List = jdbcTemplate.query(query1, (resultSet, i) ->{
89 StudentTo std_Entity = new StudentTo();
90 std_Entity.setSid(resultSet.getString("SID"));
91 std_Entity.setSname(resultSet.getString("SNAME"));
92 std_Entity.setSaddr(resultSet.getString("SADDR"));
93 return std_Entity;
94});
95if(std_List.isEmpty()) {
96 status = "notexisted";
97}else {
98 String query2 = "delete from student where SID = '"+sid+"'";
99 int rowCount = jdbcTemplate.update(query2);
100 if(rowCount == 1) {
101 status = "success";
102 }else {
103 status = "failure";
104 }
105 }
106 
107 return status;
108 }
109}
110

MultiActionController — StudentTo.java

Example16
JCode Cell
1 
2package com.durgasoft.dto;
3 
4public class StudentTo {
5private String sid;
6private String sname;
7private String saddr;
8 
9public String getSid() {
10 return sid;
11}
12public void setSid(String sid) {
13this.sid = sid;
14}
15public String getSname() {
16return sname;
17}
18public void setSname(String sname) {
19this.sname = sname;
20}
21public String getSaddr() {
22return saddr;
23}
24public void setSaddr(String saddr) {
25this.saddr = saddr;
26}
27 
28 
29}
30

MultiActionController — ds-servlet.xml

Example17
JCode Cell
1 
2<?xml version="1.0" encoding="UTF-8"?>
3<beans xmlns="http://www.springframework.org/schema/beans"
4xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5xmlns:p="http://www.springframework.org/schema/p"
6xmlns:context="http://www.springframework.org/schema/context"
7xsi:schemaLocation="
8 http://www.springframework.org/schema/beans
9 http://www.springframework.org/schema/beans/spring-beans.xsd
10 http://www.springframework.org/schema/context
11http://www.springframework.org/schema/context/spring-context.xsd">
12 
13<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
14<property name="driverClassName" value="oracle.jdbc.OracleDriver" />
15<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" />
16<property name="username" value="system" />
17<property name="password" value="durga" />
18</bean>
19 
20<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
21<property name="dataSource" ref="dataSource"/>
22</bean>
23 
24<bean id="studentDao" class="com.durgasoft.dao.StudentDaoImpl">
25<property name="jdbcTemplate" ref="jdbcTemplate"/>
26</bean>
27 
28 
29 
30 
31<bean name="/home" class="com.durgasoft.controller.HomeController"/>
32<bean name="/addpage" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
33<property name="viewName" value="addform"/>
34</bean>
35<bean name="/searchpage" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
36<property name="viewName" value="searchform"/>
37</bean>
38<bean name="/updatepage" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
39<property name="viewName" value="updateform"/>
40</bean>
41<bean name="/deletepage" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
42<property name="viewName" value="deleteform"/>
43</bean>
44 
45<bean name="/*" class="com.durgasoft.controller.StudentAction">
46<property name="studentDao" ref="studentDao"/>
47</bean>
48 
49<bean name="handlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
50 
51<bean name="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
52<property name="prefix" value="/WEB-INF/"/>
53<property name="suffix" value=".jsp"/>
54</bean>
55</beans>
56

MultiActionController — web.xml

Example18
JCode Cell
1 
2<?xml version="1.0" encoding="UTF-8"?>
3<beans xmlns="http://www.springframework.org/schema/beans"
4xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5xmlns:p="http://www.springframework.org/schema/p"
6xmlns:context="http://www.springframework.org/schema/context"
7xsi:schemaLocation="
8 http://www.springframework.org/schema/beans
9 http://www.springframework.org/schema/beans/spring-beans.xsd
10 http://www.springframework.org/schema/context
11http://www.springframework.org/schema/context/spring-context.xsd">
12 
13<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
14<property name="driverClassName" value="oracle.jdbc.OracleDriver" />
15<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" />
16<property name="username" value="system" />
17<property name="password" value="durga" />
18</bean>
19 
20<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
21<property name="dataSource" ref="dataSource"/>
22</bean>
23 
24<bean id="studentDao" class="com.durgasoft.dao.StudentDaoImpl">
25<property name="jdbcTemplate" ref="jdbcTemplate"/>
26</bean>
27 
28 
29 
30 
31<bean name="/home" class="com.durgasoft.controller.HomeController"/>
32<bean name="/addpage" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
33<property name="viewName" value="addform"/>
34</bean>
35<bean name="/searchpage" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
36<property name="viewName" value="searchform"/>
37</bean>
38<bean name="/updatepage" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
39<property name="viewName" value="updateform"/>
40</bean>
41<bean name="/deletepage" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
42<property name="viewName" value="deleteform"/>
43</bean>
44 
45<bean name="/*" class="com.durgasoft.controller.StudentAction">
46<property name="studentDao" ref="studentDao"/>
47</bean>
48 
49<bean name="handlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
50 
51<bean name="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
52<property name="prefix" value="/WEB-INF/"/>
53<property name="suffix" value=".jsp"/>
54</bean>
55</beans>
56

MultiActionController

To run the above application we need the following jar files in web application lib folder. o commons-logging-1.2.jar o ojdbc6.jar o spring-aop-4.3.9.RELEASE.jar o spring-aspects-4.3.9.RELEASE.jar o spring-beans-4.3.9.RELEASE.jar o spring-context-4.3.9.RELEASE.jar o spring-context-support-4.3.9.RELEASE.jar o spring-core-4.3.9.RELEASE.jar

📝 Key Takeaways
  • Key ideas of Spring - MultiActionController explained simply
  • Ready-to-use code examples
  • Exam-style questions at the end