Two Laptop Bag*
The moose likes Struts and the fly likes Doubt in Action Form Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Doubt in Action Form" Watch "Doubt in Action Form" New topic
Author

Doubt in Action Form

parthiban thrangaraju
Greenhorn

Joined: Feb 06, 2011
Posts: 25
Hi,

In action form can we use org.apache.struts.validator.validatorForm and validate method. I have attached my code


ActionForm
--------------

package classes.newuser;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class NewUserForm extends org.apache.struts.validator.ValidatorForm {


private static final long serialVersionUID = 1L;
private String firstname=null;
private String lastname=null;
private String gender=null;
private String dateofbirth=null;
private String emailid=null;
private String username=null;
private String password=null;
private String confirmpassword=null;


public String getConfirmpassword() {
return confirmpassword;
}

public String getDateofbirth() {
return dateofbirth;
}

public String getEmailid() {
return emailid;
}

public String getFirstname() {
return firstname;
}

public String getGender() {
return gender;
}

public String getLastname() {
return lastname;
}

public String getPassword() {
return password;
}

public String getUsername() {
return username;
}

public void setConfirmpassword(String confirmpassword) {
this.confirmpassword = confirmpassword;
}

public void setDateofbirth(String dateofbirth) {
this.dateofbirth = dateofbirth;
}

public void setEmailid(String emailid) {
this.emailid = emailid;
}

public void setFirstname(String firstname) {
this.firstname = firstname;
}

public void setGender(String gender) {
this.gender = gender;
}

public void setLastname(String lastname) {
this.lastname = lastname;
}

public void setPassword(String password) {
this.password = password;
}

public void setUsername(String username) {
this.username = username;
}

public void reset(ActionMapping mapping,HttpServletRequest request){

this.firstname=null;
this.lastname=null;
this.gender=null;
this.dateofbirth=null;
this.emailid=null;
this.username=null;
this.password=null;
this.confirmpassword=null;

}

public ActionErrors validate(ActionMapping mapping,HttpServletRequest request){

ActionErrors errors=new ActionErrors();

NewUserAction act= new NewUserAction();
ActionForm form=null;

//Boolean flag= act.

errors.add("emailid", new ActionMessage("NewUserForm.emailid.alreadyexists"));

return errors;
}
}

can any one help me
Raaja Gotluru
Ranch Hand

Joined: Mar 02, 2010
Posts: 108
Hi,

You can write validate method while extending ValidatorForm. But it will become same as that of extending ActionForm.

ValidatorForm class extends ActionForm and provides basic field validation based on an XML file. The key passed into the validator is the action element's 'name' attribute from the struts-config.xml which should match the form element's name attribute in the validation.xml.

ValidatorForm used for declarative validations not for programatic validations.
parthiban thrangaraju
Greenhorn

Joined: Feb 06, 2011
Posts: 25

Hi Raaja,

Thanks for your reply.

I have written the code for checking from database. In this code if the email id already exists in the database, it is not storing once again but the error message is not displayed and also i have tried by using logic tag. Both the scenarios are not working.How to solve the problem.

Jsp code
----------

<!DOCTYPE html>
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<%@ 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" %>
<html lang="en">
<head>
<title><bean:message key="newuser.title1"/></title>
<meta charset="utf-8">
</head>

<body id="page7">

<table id="login-form">
<html:form action="/NewUserForm" styleId="login-form">
<fieldset>
<div class="field">
<tr>
<td>
<bean:message key="newuser.title2"/>
</td>
<td>
<html:text property="firstname"/>
</td>
<td>
<html:errors property="firstname"/>
</td>
</tr>

</div>
<div class="field">
<tr>
<td>
<bean:message key="newuser.title3"/>
</td>
<td>
<html:text property="lastname"/>
</td>
<td>
<html:errors property="lastname"/>
</td>
</tr>
</div>
<div class="field">
<tr>
<td>
<bean:message key="newuser.title4"/>
</td>
<td>
<html:radio property="gender" value="Male">Male</html:radio>
<html:radio property="gender" value="Female">Female</html:radio>
</td>
<td>
<html:errors property="gender"/>
</td>
</tr>
</div>
<div class="field">
<tr>
<td>
<bean:message key="newuser.title5"/>
</td>
<td>
<html:text property="dateofbirth"/>
</td>
<td>
<html:errors property="dateofbirth"/>
</td>
</tr>
</div>
<div class="field">
<tr>
<td>
<bean:message key="newuser.title6"/><sup>*</sup>
</td>
<td>
<html:text property="emailid"/>
</td>
<td>
<html:errors property="emailid"/>
<logic:present name="email">
Email Id already exists.
</logic:present>
</td>
</tr>
</div>
<div>
<tr>
<td>
<bean:message key="common.title1"/>
</td>
<td>
<html:text property="username"/>
</td>
<td>
<html:errors property="username"/>
</td>
</tr>
</div>
<div>
<tr>
<td>
<bean:message key="common.title2"/>
</td>
<td>
<html:password property="password"/>
</td>
<td>
<html:errors property="password"/>
</td>
</tr>

</div>
<div>
<tr>
<td>
<bean:message key="newuser.title7"/>
</td>
<td>
<html:password property="confirmpassword"/>
</td>
<td>
<html:errors property="confirmpassword"/>
</td>
</tr>

</div>

<div class="wrapper">
<tr>
<td colspan="3">
<html:submit value="REGISTER" styleId="submit"/>
</td>
</tr>

</div>
</fieldset>
</html:form>
</table>

</body>
</html>

Action Form
---------------


package classes.newuser;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;




public class NewUserForm extends org.apache.struts.validator.ValidatorForm {

private static final long serialVersionUID = 1L;
private String firstname=null;
private String lastname=null;
private String gender=null;
private String dateofbirth=null;
private String emailid=null;
private String username=null;
private String password=null;
private String confirmpassword=null;

public String getConfirmpassword() {
return confirmpassword;
}

public String getDateofbirth() {
return dateofbirth;
}

public String getEmailid() {
return emailid;
}

public String getFirstname() {
return firstname;
}

public String getGender() {
return gender;
}

public String getLastname() {
return lastname;
}

public String getPassword() {
return password;
}

public String getUsername() {
return username;
}

public void setConfirmpassword(String confirmpassword) {
this.confirmpassword = confirmpassword;
}

public void setDateofbirth(String dateofbirth) {
this.dateofbirth = dateofbirth;
}

public void setEmailid(String emailid) {
this.emailid = emailid;
}

public void setFirstname(String firstname) {
this.firstname = firstname;
}

public void setGender(String gender) {
this.gender = gender;
}

public void setLastname(String lastname) {
this.lastname = lastname;
}

public void setPassword(String password) {
this.password = password;
}

public void setUsername(String username) {
this.username = username;
}

public void reset(ActionMapping mapping,HttpServletRequest request){

this.firstname=null;
this.lastname=null;
this.gender=null;
this.dateofbirth=null;
this.emailid=null;
this.username=null;
this.password=null;
this.confirmpassword=null;

}

/*public ActionErrors validate(ActionMapping mapping,HttpServletRequest request){

ActionErrors errors=new ActionErrors();

NewUserAction act= new NewUserAction();
ActionForm form=null;




errors.add("emailid", new ActionMessage("NewUserForm.emailid.alreadyexists"));

return errors;
}*/
}

Action Class
--------------


package classes.newuser;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionFormBean;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionRedirect;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.validator.FieldChecks;
import org.apache.struts.validator.ValidatorPlugIn;
import org.apache.struts.validator.validwhen.ValidWhen;


public class NewUserAction extends org.apache.struts.action.Action {

private static final String SUCCESS = "success";
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {

NewUserForm nsf=(NewUserForm)form;



String firstname=nsf.getFirstname();
String lastname=nsf.getLastname();
String gender=nsf.getGender();
String dateofbirth=nsf.getDateofbirth();
String emailid=nsf.getEmailid();
String username=nsf.getUsername();
String password=nsf.getPassword();
String confirmpassword=nsf.getConfirmpassword();

try{

String url="jdbc:mysql://localhost:3306/nemogroups";
Class.forName("com.mysql.jdbc.Driver");
Connection con= DriverManager.getConnection(url, "root" ,"root" );
Statement st=con.createStatement();

//String email=request.getParameter("emailid").toString();
System.out.println(emailid);
ResultSet rs=st.executeQuery("select emailid from registration where emailid='"+emailid+"'");
int count=0;
while(rs.next()){

String email=rs.getString("emailid");
System.out.println("email=============="+email);
count++;
}


if(count>0){

request.setAttribute("emailid", "email");

}

else{

String insert="insert into registration(firstname,lastname,gender,dateofbirth,emailid,username,password,confirmpassword) values('"+firstname+"','"+lastname+"','"+gender+"','"+dateofbirth+"','"+emailid+"','"+username+"','"+password+"','"+confirmpassword+"')";
st.executeUpdate(insert);

}

}
catch (Exception e) {
// TODO: handle exception
System.out.println(e);
}
return mapping.findForward(SUCCESS);
}



}


Thanks in advance
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in Action Form
 
Similar Threads
How to display error message
Error setting the expression with value [Ljava.lang.String;@1d05b0a
How to Validate duplicate entries in database using struts1
how can i display my data on jsp page
how to do validation in struts1