LookupDispatchAction
:
LookupDispatchAction provides a grouping a set of related functions into a
single action class, thus eliminating the need to create seperate actions for
each functions. In this example we will see how to group a set of user related
actions like
1).add user,
2).update user and
3)delete user into a single action
called UserAction.
The LookupDispatchAction class extends
org.apache.struts.actions.DispatchAction. Our class UserAction class extends
LookupDispacthAction. This class does not provide an implementation of the
execute() method as the normal Action class does. The LookupDispatchAction uses
the execute method to manage delegating the request to the individual methods
based on the incoming request parameter.
Table :
create table
student(sname varchar(20) primary key,spassword varchar(20),address
varchar(20));
Step1: develop form with multiple submit buttons
and each button perform with individual task.
home.jsp
<%@taglib uri="http://struts.apache.org/tags-html"
prefix="html" %>
<%@taglib
uri="http://struts.apache.org/tags-bean" prefix="bean"
%>
<html:form action="s">
<center>
<table>
<tr> <td> User Name:
</td>
<td><html:text property="username"/></td>
</tr>
<tr> <td> Password:
</td>
<td><html:password
property="password"/></td>
</tr>
<tr><td> Address:
</td>
<td><html:text property="address"/></td>
</tr>
<tr>
<td colspan="2"
align="center">
<html:submit property="ashok">
<bean:message
key="button.register"/>
</html:submit>
<html:submit property="ashok">
<bean:message key="button.update"/>
</html:submit>
<html:submit property="ashok">
<bean:message key="button.delete"/>
</html:submit>
</td>
</tr>
</table>
</center></html:form>
Step2: Develop a formbean class for form
package com.dustbin.formbean;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
public class StudentFormBean extends
org.apache.struts.action.ActionForm {
private
String username;
private
String password;
private
String address;
public
String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPassword() {
return password;
}public
void setPassword(String password) {
this.password = password;
}
public
String getUsername() {
return username;
}public
void setUsername(String username) {
this.username = username;
}
}
Step3: develop a LookupDispatchAction class and
here create a methods and create a form bean object for getting the values from
form.
They are..1).register()
2).update() and 3).delete()
methods ..these are performing Database operations.
package com.dustbin.controller;
import com.dustbin.formbean.StudentFormBean;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class StudentLookUpDispatchAction extends
org.apache.struts.actions.LookupDispatchAction {
protected Map getKeyMethodMap(){
Map
map=new HashMap();
map.put("button.register","register");
map.put("button.update","update");
map.put("button.delete","delete");
return map;
}
public ActionForward register(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
{
Connection con=null;
PreparedStatement pst=null;
StudentFormBean sfb=(StudentFormBean)form;
String username=sfb.getUsername();
String password=sfb.getPassword();
String address=sfb.getAddress();
try{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/ashok","root","root");
pst=con.prepareStatement("insert into student values(?,?,?)");
pst.setString(1,username);
pst.setString(2, password);
pst.setString(3, address);
int i=pst.executeUpdate();
if(i>0){
return
mapping.findForward("success");
}
else{
return mapping.findForward("failure");
}
}catch(Exception e){
return mapping.findForward("failure");
}
finally{
try{
con.close();
}catch(Exception ee){
}if(pst!=null){
try{
pst.close();
}catch(Exception e){
}
}
}
}
public
ActionForward update(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
Connection con=null;
PreparedStatement pst=null;
StudentFormBean sfb=(StudentFormBean)form;
String username=sfb.getUsername();
String password=sfb.getPassword();
String address=sfb.getAddress();
try{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/ashok","root","root");
pst=con.prepareStatement("update student set address=? ,spassword=?
where sname=?");
pst.setString(1,address);
pst.setString(2,password);
pst.setString(3, username);
int i=pst.executeUpdate();
if(i>0){
return mapping.findForward("success");
}
else{
return mapping.findForward("failure");
}
}catch(Exception e){
return mapping.findForward("failure");
}
finally{
try{
con.close();
}catch(Exception ee){
}if(pst!=null){
try{
pst.close();
}catch(Exception e){ }
}
}
}
public ActionForward delete(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
Connection con=null;
PreparedStatement pst=null;
StudentFormBean sfb=(StudentFormBean)form;
String username=sfb.getUsername();
try{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/ashok","root","root");
pst=con.prepareStatement("delete from student where sname=?");
pst.setString(1, username);
int i=pst.executeUpdate();
if(i>0){
return
mapping.findForward("success");
}
else{
return mapping.findForward("failure");
}
}catch(Exception e){
return mapping.findForward("failure");
}
finally{
try{
con.close();
}catch(Exception ee){ }
if(pst!=null){
try{
pst.close();
}catch(Exception e){}
}
}}
}
And now,configuring the action class and form bean class.
<?xml version="1.0"
encoding="UTF-8" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration
1.3//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-bean name="StudentFormBean"
type="com.dustbin.formbean.StudentFormBean"/>
</form-beans>
<global-exceptions>
</global-exceptions>
<global-forwards>
<forward name="success" path="/success.jsp"
redirect="false"/>
<forward name="failure" path="/failure.jsp"
redirect="false"/>
<forward name="welcome"
path="/Welcome.do"/>
</global-forwards>
<action-mappings>
<action name="StudentFormBean" parameter="ashok"
path="/s" validate="true" scope="request"
type="com.dustbin.controller.StudentLookUpDispatchAction"/>
<action path="/home"
type="org.apache.struts.actions.ForwardAction"
parameter="/home.jsp"/>
<action path="/Welcome"
forward="/welcomeStruts.jsp"/>
</action-mappings>
<controller
processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>
<message-resources
parameter="com/myapp/struts/ApplicationResource"/>
<!--
========================= Tiles plugin ===============================-->
<plug-in className="org.apache.struts.tiles.TilesPlugin"
>
<set-property property="definitions-config"
value="/WEB-INF/tiles-defs.xml" />
<set-property property="moduleAware" value="true"
/>
</plug-in>
<!--
========================= Validator plugin =================================
-->
<plug-in
className="org.apache.struts.validator.ValidatorPlugIn">
<set-property
property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>
</struts-config>
Step4: develop a success form for all
buttons,when database rows are effected…
This form will be displayed.
<%@taglib
uri="http://struts.apache.org/tags-html" prefix="html"
%>
Your Operation was
success....<br></br>
<html:link
action="/home">HOME</html:link>
Step5: develop a failure form for all
buttons,when rows are not effected…this form will be displayed
<%@taglib uri="http://struts.apache.org/tags-html"
prefix="html" %>
Your Operation was
Failed.....<br></br>
<html:link action="/home">HOME</html:link>
OUTPUT:
For Updating DATA
After Deleting Ashok Record:
I
CLICK HERE FOR WAR FILE DOWNLOAD
CLICK HERE FOR JAR FILE DOWNLOAD
0 comments:
Post a Comment