web.xml
<?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" id="WebApp_ID" version="2.5">
<display-name>MultiActionController</display-name>
<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>
---------------------------------------------------------------------
dispatcher-servlet.xml
<?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">
<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="lnt.infotech.spring.MultiActionControllerExample" />
</beans>
---------------------------------------------------------------------
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Success Page</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>
In the above code as you can see we have given the url as add.html, update.html, edit.html, remove.html but we have given the common bean for all the four url i.e "/*.html". which passes the control to the "MultiActionControllerExample" controller. In this controller we have extended to MultiActionController class given by spring. Due to this the controller search for the url named method (eg. add, update, edit, remove) and executes it respectively when ever called.
-------------------------------------------------------------------------
MultiActionControllerExample
package lnt.infotech.spring;
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 response) throws Exception {
return new ModelAndView("showmessage", "message", "Add method called");
}
public ModelAndView update(HttpServletRequest request,
HttpServletResponse response) throws Exception {
return new ModelAndView("showmessage", "message", "Update method called");
}
public ModelAndView edit(HttpServletRequest request,
HttpServletResponse response) throws Exception {
return new ModelAndView("showmessage", "message", "Edit method called");
}
public ModelAndView remove(HttpServletRequest request,
HttpServletResponse response) throws Exception {
return new ModelAndView("showmessage", "message", "Remove method called");
}
}
-------------------------------------------------------------------------------
showmessage.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Success Page</title>
</head>
<body>
${message}
</body>
</html>
In the above code ${message} is accessing the message set by the controller and displaying it.
--------------------------------------------------------------------------------
Jars required
----------------
1. commons-logging.jar
2. jstl.jar
3. mail.jar
4. spring-webmvc.jar
5. spring.jar
6. standard.jar
---------------------------------------------------------------------
project layout
No comments:
Post a Comment