First of all thanks to you both.
I am using same for both addition and edit and have used same action class for both action only a parameter passed during the submission differentiate between those two actions.
My struts-config.xml file content:
-----------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<form-beans>
<form-bean name="loginForm" type="com.f1soft.das.struts.form.LoginForm" />
<form-bean name="employeeEditForm" type="org.apache.struts.validator.DynaValidatorActionForm" >
<form-property name="firstName" type="java.lang.String" initial=""></form-property>
<form-property name="lastName" type="java.lang.String" ></form-property>
<form-property name="address" type="java.lang.String" ></form-property>
<form-property name="email" type="java.lang.String" ></form-property>
<form-property name="phone" type="java.lang.String"></form-property>
<form-property name="mobile" type="java.lang.String"></form-property>
<form-property name="dobDay" type="java.lang.String"></form-property>
<form-property name="dobMonth" type="java.lang.String"></form-property>
<form-property name="dobYear" type="java.lang.String"></form-property>
<form-property name="hireDay" type="java.lang.String"></form-property>
<form-property name="hireMonth" type="java.lang.String"></form-property>
<form-property name="hireYear" type="java.lang.String"></form-property>
<form-property name="post" type="java.lang.String"></form-property>
<form-property name="empId" type="java.lang.String"></form-property>
<form-property name="privilege" type="java.lang.String"></form-property>
<form-property name="recordStatus" type="java.lang.String"></form-property>
</form-bean>
</form-beans>
<global-forwards>
<forward
name="welcome"
path="/default.do"
redirect="true" />
<!-- global forward for
test. Need to be removed later -->
<forward name="test"
path="/searchPage.do"
redirect="true" />
</global-forwards>
<action-mappings>
<action path="/searchFirstName"
include="/WEB-INF/jsp/admin/searchFirstName.jsp" />
<action path="/default"
forward="/WEB-INF/jsp/index.jsp" unknown="true"/>
<action path="/searchPage"
forward="/WEB-INF/jsp/admin/search.jsp" />
<action path="/login"
type="com.f1soft.das.struts.action.LoginAction"
name="loginForm"
input="/WEB-INF/jsp/index.jsp"
scope="request"
validate="true">
<forward name="admin" path="/WEB-INF/jsp/admin/navigation.jsp" />
<forward name="employee" path="/WEB-INF/jsp/employee/navigation.jsp" />
<forward name="failure" path="/WEB-INF/jsp/index.jsp?status=failure" />
</action>
<action path="/homePage"
forward="/WEB-INF/jsp/admin/navigation.jsp" />
<action path="/employeeManagementPage"
parameter="/WEB-INF/jsp/admin/employeeManagement.jsp"
type="org.apache.struts.actions.ForwardAction" />
<actionpath="/leaveApprovalPage"
parameter="/WEB-INF/jsp/admin/leaveApproval.jsp"
type="org.apache.struts.actions.ForwardAction" />
<action path="/reportPage"
parameter="/WEB-INF/jsp/admin/report.jsp"
type="org.apache.struts.actions.ForwardAction" />
<action path="/employeeAdditionPage"
parameter="/WEB-INF/jsp/admin/employeeAdd.jsp"
type="org.apache.struts.actions.ForwardAction" />
<!-- <action path="/employeeEditPage"
parameter="/WEB-INF/jsp/admin/employeeEdit.jsp"
type="org.apache.struts.actions.ForwardAction" />-->
<action
attribute="employeeEditForm"
input="/WEB-INF/jsp/admin/employeeAdd.jsp"
name="employeeEditForm"
parameter="do"
path="/employeeEdit"
scope="request"
type="com.f1soft.das.struts.action.ShowEmployeeAction"
cancellable="true"
validate="true">
<forward name="op-error" path="/WEB-INF/jsp/admin/employeeAdd.jsp" />
<forward name="op-success" path="/WEB-INF/jsp/admin/next.jsp" />
<forward name="mainpage" path="/WEB-INF/jsp/admin/employeeManagement.jsp" />
<forward name="invalid" path="/index.jsp"></forward>
</action>
<action
path="/searchedEmployeeEdit"
attribute="employeeEditForm"
input="/WEB-INF/jsp/admin/employeeEdit.jsp"
name="employeeEditForm"
parameter="do"
scope="request"
validate="true"
cancellable="true"
type="com.f1soft.das.struts.action.EmployeeEditAction" >
<!-- forward names for EmployeeEditAction.showEmployee() -->
<forward name="edit-success" path="/WEB-INF/jsp/admin/employeeEdit.jsp" />
<forward name="gotoPage" path="/WEB-INF/jsp/admin/employeeEdit.jsp" />
<forward name="back" path="/WEB-INF/jsp/admin/search.jsp" />
<!-- forward names for EmployeeEditAction.saveEmployee() for flag=edit-->
<forward name="op-error" path="/WEB-INF/jsp/admin/employeeEdit.jsp" />
<forward name="op-success" path="/WEB-INF/jsp/admin/editSuccess.jsp" />
<forward name="mainpage" path="/WEB-INF/jsp/admin/search.jsp" />
<forward name="invalid" path="/index.jsp"></forward>
</action>
</action-mappings>
<message-resources parameter="com.f1soft.das.struts.ApplicationResources" />
<!-- validator plugins configuration -->
<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>
My Action class : EmployeeEditAction.java
------------------------------------------
package com.f1soft.das.struts.action;
import java.util.Calendar;
import java.util.Date;
import java.util.Hashtable;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.util.MessageResources;
import org.apache.struts.validator.DynaValidatorActionForm;
import com.f1soft.das.bl.EmployeeManager;
import com.f1soft.das.bl.LoginValidation;
import com.f1soft.das.misc.DateConvertor;
import com.f1soft.das.test.TestAdditionUpdate;
import com.f1soft.das.util.Employee;
public class EmployeeEditAction extends DispatchAction {
/*EmployeeManager handles the database transactions related to employee*/
EmployeeManager em = new EmployeeManager();
/*
* method to populate the searched information on employeeEdit.jsp
*/
public ActionForward showEmployee(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
DynaValidatorActionForm df= (DynaValidatorActionForm)form;
System.err.println("@EMployeeEditAction.java::REF-006:SearchKey is "+request.getParameter("searchKey"));
System.out.println("@EMployeeEditAction.java::REF-001:Show employee function!!!");
/*if(isCancelled(request)){
System.out.println("@EMployeeEditAction.java::REF-005:Cancel action clicked!!!");
return mapping.findForward("back");
}*/
//search the employee with the id in hashtable "userTable"
String search = request.getParameter("searchKey");
Employee searchedEmp=new LoginValidation().getUserTable().get(search);
//set the values to form
df.set("empId", searchedEmp.getEmpId());
df.set("firstName", searchedEmp.getFirstName());
df.set("lastName", searchedEmp.getLastName());
df.set("address", searchedEmp.getAddress());
df.set("email", searchedEmp.getEmail());
df.set("phone", searchedEmp.getPhone());
df.set("mobile", searchedEmp.getMobile());
df.set("post", searchedEmp.getPost());
df.set("privilege", Integer.toString(searchedEmp.getPrivilege()));
Calendar dob = Calendar.getInstance();
dob.setTime(searchedEmp.getDob());
df.set("dobDay", Integer.toString(dob.get(Calendar.DATE)));
df.set("dobMonth", Integer.toString(dob.get(Calendar.MONTH)));
df.set("dobYear", Integer.toString(dob.get(Calendar.YEAR)));
Calendar hire = Calendar.getInstance();
hire.setTime(searchedEmp.getHireDate());
df.set("hireDay", Integer.toString(hire.get(Calendar.DATE)));
df.set("hireMonth", Integer.toString(hire.get(Calendar.MONTH)));
df.set("hireYear", Integer.toString(hire.get(Calendar.YEAR)));
String rs = searchedEmp.getRecordStatus().toString();
System.out.println("@EmployeeEditAction-EEA-013::record status fetched");
df.set("recordStatus", rs);
System.out.println(" --RecordStatus:::"+searchedEmp.getRecordStatus());
System.out.println(" --RecordStatus-STRING:::"+searchedEmp.getRecordStatus().toString());
return mapping.findForward("gotoPage");
}
/*
* method to save/update the employee information.
*/
public ActionForward saveEmployee(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
DynaValidatorActionForm employeeEditForm = (DynaValidatorActionForm) form;
String forwardString = "";
//attribute to catch the edit action requested.
String actionRequested=request.getParameter("flag");
//to catch the exception when the request parameter flag is other than edit
if(!actionRequested.equalsIgnoreCase("edit") && !actionRequested.equalsIgnoreCase("save")){
return mapping.findForward("invalid");
}
if(employeeEditForm.get("phone").toString().trim().equalsIgnoreCase("")){
System.out.println("@EmployeeEditAction.java REF-014:: PHONE IS NULL");
}
if(isCancelled(request)){
System.out.println("@EMployeeEditAction.java::REF-007:Cancel action clicked!!!");
return mapping.findForward("mainpage");
}
/*
* Addition of New Employee Information
* This part is for addition of new employee information
*/
if (actionRequested.equalsIgnoreCase("save")){
System.out.println("@EMployeeEditAction.java REF-009::\n --NEW EMPLOYEE BEING ADDED");
/***
* this part need to be modified during integration
* this line to be deleted as the userTable variable is static
* and is available under login module integration
*/
Hashtable<String, Employee> users = new LoginValidation().getUserTable();
/**
* check if empId matches with the existing.
*/
if(users.get(employeeEditForm.getString("empId"))!= null){
System.err.println("@EmployeeEditAction.java REF-003::User exists.. Please try with another id");
request.setAttribute("errMsg", "User Exists! Please try with another id");
forwardString="op-error";
}
/*
* check if email matches with the existing
*/
if (em.isEmailExist(employeeEditForm.getString("email"))){
System.err.print("@EmployeeEditAction::Someone has already registered with the email add");
request.setAttribute("errDuplicate", "Someone has already registered with the email add");
forwardString="op-error";
}
/* to capture both of the above errors at a time*/
if(forwardString.equalsIgnoreCase("op-error")){
return mapping.findForward(forwardString);
}
} //end of section Addition of New Employee Information
else if(actionRequested.equals("edit")){
/*check email add if it matches with any other existing email id registered*/
System.out.println("@EMployeeEditAction.java REF-008::\n --EMPLOYEE UPDATE GOING ON");
}
/*
* the default password is obtained from the resource file.
* so messageResources is required.
*/
MessageResources messageResources = getResources(request);
System.out.println("@ EmployeeEditAction:::Employee Name is:: "+employeeEditForm.get("firstName"));
//conversion to date
int dobYear=Integer.parseInt(employeeEditForm.getString("dobYear"));
int dobMonth= Integer.parseInt(employeeEditForm.getString("dobMonth"));
int dobDay=Integer.parseInt(employeeEditForm.getString("dobDay"));
int hireYear = Integer.parseInt(employeeEditForm.getString("hireYear"));
int hireMonth = Integer.parseInt(employeeEditForm.getString("hireMonth"));
int hireDay = Integer.parseInt(employeeEditForm.getString("hireDay"));
Date dob = new DateConvertor().createDate(dobYear, dobMonth, dobDay);
Date hireDate = new DateConvertor().createDate(hireYear, hireMonth, hireDay);
//transfer form data to hibernate dto
Employee employee = new Employee();
employee.setFirstName(employeeEditForm.getString("firstName"));
employee.setLastName(employeeEditForm.getString("lastName"));
employee.setAddress(employeeEditForm.getString("address"));
employee.setPhone(employeeEditForm.getString("phone"));
employee.setEmail(employeeEditForm.getString("email"));
employee.setMobile(employeeEditForm.getString("mobile"));
employee.setEmpId(employeeEditForm.getString("empId"));
employee.setDob(dob);
employee.setHireDate(hireDate);
employee.setPost(employeeEditForm.getString("post"));
employee.setPrivilege(Integer.parseInt(employeeEditForm.getString("privilege")));
employee.setPassword(messageResources.getMessage("app.defaultPassword"));
if(actionRequested.equalsIgnoreCase("save")){
//for new Employee
//employee.setPassword(messageResources.getMessage("app.defaultPassword"));--commented for checking
employee.setRecordStatus(true);
}else if(actionRequested.equalsIgnoreCase("edit")){
//for existing employee during edit
//employee.setPassword(""); --commented for checking
System.out.println("@EmployeeEditAction:::EEA-011::recordStatus="+employeeEditForm.getString("recordStatus"));
System.out.println("@EmployeeEditAction:::EEA-012::recordStatus="+Boolean.parseBoolean(employeeEditForm.getString("recordStatus")));
employee.setRecordStatus(Boolean.parseBoolean(employeeEditForm.getString("recordStatus")));
}
System.out.println("@EmployeeEditAction::REF-010::Employee Data:::"+employee.toString());
//insert into db
em.saveEmployee(employee);
/*Test whether updated or not
* -- need to removed later
* */
new TestAdditionUpdate().showNewUser(employee.getEmpId());
System.out.println("@EmployeeEditAction REF-002 :::Save Successful");
return mapping.findForward("op-success");
}
}
My JSP Page: employeeEdit.jsp
------------------------------
<%@ page language="java" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ include file="header.jsp" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base />
<title>Edit Employee Record</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body><br>
This is employee Edit page
<html:errors/><br/>
<html:form action="/searchedEmployeeEdit">
<table>
<tr>
<td colspan="2">Employee Information:</td>
</tr>
<tr>
<td>First Name</td>
<td><html:text property="firstName" /></td>
</tr>
<tr>
<td>Last Name</td>
<td><html:text property="lastName" /></td>
</tr>
<tr>
<td>Address</td>
<td><html:text property="address" /></td>
</tr>
<%
if(request.getAttribute("errDuplicate")!=null){
%>
<tr>
<td colspan="2"><%=(String)request.getAttribute("errDuplicate") %></td>
</tr>
<%
}
%>
<tr>
<td>Email</td>
<td><html:text property="email" /></td>
</tr>
<tr>
<td>Phone</td>
<td><html:text property="phone" /></td>
</tr>
<tr>
<td>Mobile</td>
<td><html:text property="mobile" /></td>
</tr>
<tr>
<td>Date of Birth</td>
<td>
<html:select property="dobDay" >
<html:option value=""></html:option>
<c:forEach var="i" begin="1" end="31" step="1">
<html:option value="${i}">${i}</html:option>
</c:forEach>
</html:select>
<html:select property="dobMonth" >
<c:set var="count" value="0"/>
<html:option value=""></html:option>
<c:forEach var="mon" items="Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec">
<html:option value="${count}"><c:out value="${mon}"/></html:option>
<c:set var="count" value="${count+1}"/>
</c:forEach>
</html:select>
<html:select property="dobYear" >
<html:option value=""></html:option>
<c:forEach var="i" begin="<%=currentYear-50 %>" end="<%=currentYear-10 %>" >
<html:option value="${i}">${i}</html:option>
</c:forEach>
</html:select>
</td>
</tr>
<tr>
<td>Hire Date: </td>
<td><html:select property="hireDay" >
<html:option value=""></html:option>
<c:forEach var="i" begin="1" end="31" step="1">
<html:option value="${i}">${i}</html:option>
</c:forEach>
</html:select>
<html:select property="hireMonth" >
<c:set var="count" value="0"/>
<html:option value="" ></html:option>
<c:forEach var="mon" items="Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec">
<html:option value="${count}"><c:out value="${mon}"/></html:option>
<c:set var="count" value="${count+1}"/>
</c:forEach>
</html:select>
<html:select property="hireYear" >
<html:option value=""></html:option>
<c:forEach var="i" begin="<%=currentYear-50 %>" end="<%=currentYear %>">
<html:option value="${i}">${i}</html:option>
</c:forEach>
</html:select></td>
</tr>
<tr>
<td>Post:</td>
<td><html:text property="post"/></td>
</tr>
<tr>
<td colspan="2">For Login Purpose:</td>
</tr>
<tr>
<td>Privilege:</td>
<td>
<html:select property="privilege">
<html:option value=""></html:option>
<html:option value="1">Admin</html:option>
<html:option value="2">Employee</html:option>
</html:select>
</td>
</tr>
<tr>
<td>Status:</td>
<td>
<html:radio property="recordStatus" value="true">ACTIVE</html:radio>
<html:radio property="recordStatus" value="false">INACTIVE</html:radio>
</td>
</tr>
</table>
<html:hidden property="empId"/>
<html:hidden property="flag" value="edit"/>
<%-- set the parameter for the dispatch action --%>
<html:hidden property="do" value="saveEmployee" />
<html:submit>Edit</html:submit>
<html:cancel>Cancel</html:cancel>
<html:reset>Refresh</html:reset>
</html:form>
</body>
</html:html>
I hope these three are will help you to debug. The actual edit is called from other page which has "edit" button to call the edit action which actually populates the values to the employeeEdit.jsp page by calling "showEmployee()" method included in EmployeeEditAction class.
I also tried by separating action class and bean form for edit. But in vain, it didn't work.
I hope my logic is correct to implement the DynaValidatorActionForm for the edit task.
Thanking all.
[ July 08, 2008: Message edited by: sushmi shrestha ]
[ July 08, 2008: Message edited by: sushmi shrestha ]
[ July 08, 2008: Message edited by: sushmi shrestha ]