wood burning stoves 2.0*
The moose likes Struts and the fly likes DynaValidatorActionForm not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Frameworks » Struts
Bookmark "DynaValidatorActionForm not working" Watch "DynaValidatorActionForm not working" New topic
Author

DynaValidatorActionForm not working

sushmi shrestha
Greenhorn

Joined: Jul 06, 2008
Posts: 7
I am new to Struts and using Struts 1.3
I have done few things with DynaValidatorActionForm, it worked quite perfectly but when I got with this my usecase of "EDIT" DynaValidatorActionForm is not working as I guessed.

My Usecase for "Edit" goes like this:
1. The Click on Edit link populates the value on DynaValidatorActionForm and then values are displayed on JSP Page.
2. Then submitting the form with changed values will update to database.

But when the edit action is submitted after making changes on the values, the values get updated to the database but the validation that should be doing by DynaValidatorActionForm is not working at this point. Email, null field, numeric field and any other validation is not going on. I am exhausted trying all my ideas leaving no guesses behind.

Please help me out; as during addition, the same DynaValidatorActionForm is working well and doing all the validations properly but during edit, the validation is not working.

I am new to Forums, I apologize if I failed to post my problem at correct category.

Thanking for appreciations.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
The best course of action in this case is to study the differences between the working and non-working code. If you're still stuck, post the relevant portions of your struts-config.xml file, JSP, and Action classes and we'll help you debug it.


Merrill
Consultant, Sima Solutions
Ilari Moilanen
Ranch Hand

Joined: Apr 15, 2008
Posts: 198
Is the edit action different action than the normal "save" action? If it is then are you sure they both use the same form name? Or if they are supposed to use different forms then are you sure that you have configured the xml validations to the edit form too (or just for the "save" form).

I have not coded Struts 1 applications in a long time (use Struts 2 nowadays) but I remember having those kind of problems when the "save" and "edit" validations gave different results.
sushmi shrestha
Greenhorn

Joined: Jul 06, 2008
Posts: 7
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" >
<htmlption value=""></htmlption>
<c:forEach var="i" begin="1" end="31" step="1">
<htmlption value="${i}">${i}</htmlption>
</c:forEach>
</html:select>
<html:select property="dobMonth" >
<c:set var="count" value="0"/>
<htmlption value=""></htmlption>
<c:forEach var="mon" items="Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec">
<htmlption value="${count}"><cut value="${mon}"/></htmlption>
<c:set var="count" value="${count+1}"/>
</c:forEach>
</html:select>
<html:select property="dobYear" >
<htmlption value=""></htmlption>
<c:forEach var="i" begin="<%=currentYear-50 %>" end="<%=currentYear-10 %>" >
<htmlption value="${i}">${i}</htmlption>
</c:forEach>
</html:select>
</td>
</tr>
<tr>
<td>Hire Date: </td>
<td><html:select property="hireDay" >
<htmlption value=""></htmlption>
<c:forEach var="i" begin="1" end="31" step="1">
<htmlption value="${i}">${i}</htmlption>
</c:forEach>
</html:select>
<html:select property="hireMonth" >
<c:set var="count" value="0"/>
<htmlption value="" ></htmlption>
<c:forEach var="mon" items="Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec">
<htmlption value="${count}"><cut value="${mon}"/></htmlption>
<c:set var="count" value="${count+1}"/>
</c:forEach>
</html:select>
<html:select property="hireYear" >
<htmlption value=""></htmlption>
<c:forEach var="i" begin="<%=currentYear-50 %>" end="<%=currentYear %>">
<htmlption value="${i}">${i}</htmlption>
</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">
<htmlption value=""></htmlption>
<htmlption value="1">Admin</htmlption>
<htmlption value="2">Employee</htmlption>
</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 ]
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
First of all, a couple of suggestions: When I asked for "relevant portions" of your code, I meant only those pieces that apply to the current problem, not the whole thing. Also, in the future, please surround your actual code with [ c o d e ] and [ / c o d e ] tags (only without the extra spaces). This will allow the posted code to keep its original indentations and make it much easier to read.

Please show us the portion of the validator.xml file that pertains to the employeeEditForm.
[ July 08, 2008: Message edited by: Merrill Higginson ]
sushmi shrestha
Greenhorn

Joined: Jul 06, 2008
Posts: 7
I will heartly follow your suggestions

Here is my validation.xml portion:


Thanking you for the effort.
[ July 08, 2008: Message edited by: sushmi shrestha ]
atla praveenkumarreddy
Greenhorn

Joined: Jul 08, 2008
Posts: 3
Originally posted by sushmi shrestha:
I will heartly follow your suggestions

Here is my validation.xml portion:


Thanking you for the effort.

[ July 08, 2008: Message edited by: sushmi shrestha ]
atla praveenkumarreddy
Greenhorn

Joined: Jul 08, 2008
Posts: 3
DynaValidatorActionForm validations will be binded with action tag path attribute value.

in jsp you have given <html:form action="/searchedEmployeeEdit">

in struts-config you have specified the action path as path="/searchedEmployeeEdit".

upto this no problem.

but in validation.xml you have to specified the form name has <form name="/employeeEdit"> but

you should specify that as <form name="/searchedEmployeeEdit">

for your kind request try this by replacing the form name with action tag path attribute value as specified by me.
atla praveenkumarreddy
Greenhorn

Joined: Jul 08, 2008
Posts: 3
Originally posted by sushmi shrestha:
I will heartly follow your suggestions

Here is my validation.xml portion:


Thanking you for the effort.

[ July 08, 2008: Message edited by: sushmi shrestha ]
sushmi shrestha
Greenhorn

Joined: Jul 06, 2008
Posts: 7
Thankyou for the hint.

I tried with your suggestion but could not resolve the problem. The change could not populate the value to the JSP form.

I am using the same form for edit and save.

The same action mapping for both edit and save would solve the problem but during the error display, the whole thing gets wrong as the both page forward errors to the same page. Is it possible to pass the dynamic value to the input parmeter of action tag in struts-config.xml? As this will solve my problem.

If "form" can be specified during submission, it would solve the problem I think but I could find no way to specify the form.

Thankyou once again.
sushmi shrestha
Greenhorn

Joined: Jul 06, 2008
Posts: 7
Sorry for closing the topic so lately.
Thanks to atla praveenkumarreddy for pointing where i got wrong actually.
All the confusion and mess was due the inability to understand the DynaValidatorActionForm.

I resolved my problem.

Once again thanks to you both.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61315
    
  66

"sushmi", please check your private messages for an important administrative matter.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
GeeCON Prague 2014
 
subject: DynaValidatorActionForm not working