|Home |Login |Registration |Struts Step by Step Tutorial |Hibernate Step by Step Tutorial |Spring Step by Step Tutorial |JSP Step by Step Tutorial |JDBC Step by Step Tutorial |Web Services Step by Step Tutorial |EJB fundamentals | ORACLE Step by Step Tutorial | SCJP 5.0 and SCJP 6.0 Study Guide | SCWCD 5.0 Study Guide | SCJP Tips
Java mock test | SCJP mock test | SCJP DUMP | SCBCD mock test |Java online test exam | JSP online test | ORACLE online test | Hibernate online test | Servlet online test | Struts online test | EJB online test | C online test | C++ online test | Aptitude online test

Java interview questions | JSP interview questions | ORACLE interview questions | Hibernate interview questions | Servlet interview questions | Struts interview questions | JDBC interview questions | C/C++ interview questions | Spring interview questions | JMS interview questions | Informatica interview questions | EJB interview questions | OOPS and Design Pattern interview questions
1600 PMP mock questions 1400 CAPM mock questions 800 SCJP 6 mock questions 600 OCAJP 7 mock questions 590 OCPJP 7 mock questions 556 SCWCD 5 mock questions 500 OCEJWCD 6 mock questions pdfDownload (java,struts, hibernet etc) JobsJobs and Walkins

 

Recent Questions

!!! Struts Frequently Asked Questions !!!

What is Struts Validator Framework?

SCJP 1.5/1.6 Exam Kit

!!!Answer!!!- From Technical Expert

Validation Framework provides the functionality to validate the form data. It can be use to validate the data on the client side as well as on the server side.

Struts has a class called ValidatorForm in org.apache.struts.validator package. This is a subclass of ActionForm and implements the validate() method. The validate() method invokes the Commons Validator, executes the rules using the two xml files (validator-rules.xml and validation.xml) and generates ActionErrors using the Message Resources defined in the struts-config.xml.
validator-rules.xml :
The validator-rules.xml file defines the Validator definitions available for a given application.
The validator-rules.xml file acts as a template, defining all of the possible Validators that are available to an application.
validation.xml File :
The validation.xml file is where you couple the individual Validators defined in the validator-rules.xml to components within your application.


Follow the below steps to setup Validation Framework in Struts (server side validation ).

Step 1. Add validator plugin into struts-config.xml

<plug-in className="org.apache.struts.validator.ValidatorPlugIn"> <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,
/WEB-INF/validation.xml"/>
</plug-in>

Step 3. In the JSP page (EmpForm.jsp)- add <html:errors/>

<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html:form>
<html:form action="/submitForm.do" method="post">
<html:errors/> // this is for error message from resource bundle in JSP
<html:text property="firstName" size="20" maxlength="50"/>
<html:text property="lastName" size="20" maxlength="50"/>
<html:submit >Save</html:submit>
</html:form>


Step 4. Add Message Resources location in struts-config.xml

<!-- Message Resources --> <message-resources parameter="application_resource"/>

Step 5. In the the Resource Bundle. application_resource.properties file //Here you can add localization.

label.firstName=First Name
label.lastName=Last Name
errors.required={0} is required.


Step 7. In the EmpForm

package com.techfaq.form;
public class EmpForm extends ValidatorForm {
int empId;
String firstName;
String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
}

Step 6. In the validation.xml - The validation.xml file is where you couple the individual Validators defined in the validator-rules.xml to components within your application

<form-validation>
<formset>
<form name="empForm">
<field property="firstName" depends="required">
<arg0 key="label.firstName"/>
</field>
<field property="lastName" depends="required"> <arg0 key="label.lastName"/>
</field>
</form>
</formset>
</form-validation>


Step 6. In the validator-rules.xml - The validator-rules.xml file defines the Validator definitions available for a given application.

<form-validation>
<global>
<validator
name="required"
classname="org.apache.struts.util.StrutsValidator"
method="validateRequired"
methodparams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionErrors,
javax.servlet.http.HttpServletRequest"
msg="errors.required"/>
</global>
</form-validation>


Step 5. Add Action mapping and form entry into the stuts-confix.xml and validate="true" is for validation framework to validate the form input.

<?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="empForm" type="com.techfaq.form.EmpForm"> </form-bean>
</form-beans>
<action-mappings>
<action path="/submitForm"
type="com.techfaq.action.EmpAction"
name="empForm"
scope="request"
validate="true" // This need for validation framework to validate the form input
input="EmpForm.jsp">
<forward name="success"
path="success.jsp"/>
<forward name="failure" path="failure.jsp" />
</action>
</action-mappings>
</struts-config>


Now in the browser type http://localhost:8080/testApp/EmpForm.jsp

Don't Enter firstName and lastName in the text box and submit the "Save" BUTTON. the RequestProcessor checks for the validateattribute in the ActionMapping.
If the validate is set to true, the RequestProcessor invokes the validate() method of the ValidatorForm instance.
If Validate fail the RequestProcessor looks for the input attribute and return to JSP page mentioned in input tag.
If Validate pass goto Action Class execute() method..
If Validate fail , In the browser (EmpForm.jsp) you can see. In red color.


First Name is required.
Last Name is required.


In the empForm firstName and lastName are the required filed. So in the above configuration you can see we add for both firstName and lastName. You can see depends="required" - "required" property is defind in validator-rules.xml. In the resource bundle : application_resource.propertis file
label.firstName=First Name
label.lastName=Last Name
#Error messages used by the Validator
errors.required={0} is required.
{0} will be filled by (First Name or Last Name) because validation.xml above configuration you have defind
<arg0 key="label.lastName"/>. and <arg0 key="label.lastName"/>.

Answered By : null Replied Date : Mar 30 2012
Answer :


Answered By : null Replied Date : Feb 28 2012
Answer :


Answered By : null Replied Date : Feb 26 2012
Answer :


Answered By : null Replied Date : Feb 24 2012
Answer :


Answered By : null Replied Date : Feb 22 2012
Answer :


Answered By : null Replied Date : Feb 22 2012
Answer :


Answered By : null Replied Date : Feb 22 2012
Answer :


Answered By : null Replied Date : Feb 7 2012
Answer :


Answered By : null Replied Date : Feb 7 2012
Answer :


Answered By : null Replied Date : Feb 4 2012
Answer :


Answered By : null Replied Date : Jan 25 2012
Answer :


Answered By : null Replied Date : Jan 9 2012
Answer :


Answered By : null Replied Date : Jan 8 2012
Answer :


Answered By : null Replied Date : Jan 6 2012
Answer :


Answered By : null Replied Date : Jan 3 2012
Answer :


Answered By : null Replied Date : Dec 22 2011
Answer :


Answered By : null Replied Date : Dec 14 2011
Answer :


Answered By : null Replied Date : Dec 13 2011
Answer :


Answered By : null Replied Date : Dec 2 2011
Answer :


Answered By : null Replied Date : Nov 28 2011
Answer :


Answered By : null Replied Date : Nov 25 2011
Answer :


Answered By : null Replied Date : Nov 10 2011
Answer :


Answered By : null Replied Date : Oct 28 2011
Answer :


Answered By : null Replied Date : Oct 24 2011
Answer :


Answered By : null Replied Date : Oct 16 2011
Answer :


Answered By : null Replied Date : Oct 15 2011
Answer :


Answered By : null Replied Date : Oct 13 2011
Answer :


Answered By : null Replied Date : Oct 7 2011
Answer :


Answered By : null Replied Date : Oct 1 2011
Answer :


Answered By : null Replied Date : Sep 21 2011
Answer :


Answered By : null Replied Date : Sep 21 2011
Answer :


Answered By : null Replied Date : Sep 21 2011
Answer :


Answered By : null Replied Date : Sep 21 2011
Answer :


Answered By : null Replied Date : Sep 5 2011
Answer :


Answered By : null Replied Date : Sep 3 2011
Answer :


Answered By : null Replied Date : Aug 20 2011
Answer :


Answered By : null Replied Date : Aug 20 2011
Answer :


Answered By : null Replied Date : Aug 19 2011
Answer :


Answered By : null Replied Date : Jul 25 2011
Answer :


Answered By : null Replied Date : Jun 22 2011
Answer :


Answered By : null Replied Date : Jun 20 2011
Answer :


Answered By : null Replied Date : Jun 18 2011
Answer :


Answered By : null Replied Date : Jun 8 2011
Answer :


Answered By : null Replied Date : Jun 8 2011
Answer :


Answered By : null Replied Date : May 11 2011
Answer :


Answered By : null Replied Date : May 10 2011
Answer :


Answered By : null Replied Date : May 10 2011
Answer :


Answered By : null Replied Date : Apr 26 2011
Answer :


Answered By : null Replied Date : Apr 22 2011
Answer :


Answered By : null Replied Date : Apr 6 2011
Answer :


Answered By : null Replied Date : Apr 6 2011
Answer :


Answered By : null Replied Date : Apr 4 2011
Answer :


Answered By : null Replied Date : Apr 4 2011
Answer :


Answered By : null Replied Date : Mar 29 2011
Answer :


Answered By : null Replied Date : Mar 20 2011
Answer :


Answered By : null Replied Date : Mar 6 2011
Answer :


Answered By : null Replied Date : Feb 27 2011
Answer :


Answered By : null Replied Date : Feb 27 2011
Answer :


Answered By : null Replied Date : Feb 23 2011
Answer :


Answered By : null Replied Date : Feb 13 2011
Answer :


Answered By : null Replied Date : Feb 9 2011
Answer :


Answered By : null Replied Date : Jan 29 2011
Answer :


Answered By : null Replied Date : Jan 29 2011
Answer :


Answered By : null Replied Date : Jan 15 2011
Answer :


Answered By : null Replied Date : Jan 15 2011
Answer :


Answered By : null Replied Date : Jan 7 2011
Answer :


Answered By : null Replied Date : Dec 22 2010
Answer :


Answered By : null Replied Date : Dec 15 2010
Answer :


Answered By : null Replied Date : Dec 5 2010
Answer :


Answered By : null Replied Date : Dec 4 2010
Answer :


Answered By : null Replied Date : Dec 4 2010
Answer :


Answered By : null Replied Date : Dec 4 2010
Answer :


Answered By : null Replied Date : Dec 3 2010
Answer :


Answered By : null Replied Date : Dec 3 2010
Answer :


Answered By : null Replied Date : Dec 2 2010
Answer :


Answered By : null Replied Date : Dec 1 2010
Answer :


Answered By : null Replied Date : Dec 1 2010
Answer :


Answered By : null Replied Date : Nov 28 2010
Answer :


Answered By : null Replied Date : Nov 26 2010
Answer :


Answered By : null Replied Date : Nov 26 2010
Answer :


Answered By : null Replied Date : Nov 25 2010
Answer :


Answered By : null Replied Date : Nov 23 2010
Answer :


Answered By : null Replied Date : Nov 22 2010
Answer :


Answered By : null Replied Date : Nov 21 2010
Answer :


Answered By : null Replied Date : Nov 16 2010
Answer :


Answered By : null Replied Date : Nov 16 2010
Answer :


Answered By : null Replied Date : Nov 16 2010
Answer :


Answered By : null Replied Date : Nov 15 2010
Answer :


Answered By : null Replied Date : Nov 15 2010
Answer :


Answered By : null Replied Date : Nov 15 2010
Answer :


Answered By : null Replied Date : Nov 15 2010
Answer :


Answered By : null Replied Date : Nov 14 2010
Answer :


Answered By : null Replied Date : Nov 14 2010
Answer :


Answered By : null Replied Date : Nov 14 2010
Answer :


Answered By : null Replied Date : Nov 13 2010
Answer :


Answered By : null Replied Date : Nov 13 2010
Answer :


Answered By : null Replied Date : Nov 12 2010
Answer :


Answered By : null Replied Date : Nov 12 2010
Answer :


Answered By : null Replied Date : Nov 12 2010
Answer :


Answered By : null Replied Date : Nov 12 2010
Answer :


Answered By : null Replied Date : Nov 11 2010
Answer :


Answered By : null Replied Date : Nov 10 2010
Answer :


Answered By : null Replied Date : Nov 10 2010
Answer :


Answered By : null Replied Date : Nov 8 2010
Answer :


Answered By : null Replied Date : Nov 5 2010
Answer :


Answered By : qweq Replied Date : May 4 2009
Answer : qwe


Answered By : null Replied Date : Oct 29 2010
Answer :


Answered By : null Replied Date : Oct 28 2010
Answer :


Answered By : null Replied Date : Oct 27 2010
Answer :


Answered By : null Replied Date : Oct 26 2010
Answer :


Answered By : null Replied Date : Oct 24 2010
Answer :


Answered By : null Replied Date : Oct 24 2010
Answer :


Answered By : null Replied Date : Oct 22 2010
Answer :


Answered By : null Replied Date : Oct 21 2010
Answer :


Answered By : null Replied Date : Oct 19 2010
Answer :


Answered By : null Replied Date : Oct 16 2010
Answer :


Answered By : null Replied Date : Oct 14 2010
Answer :


Answered By : null Replied Date : Oct 13 2010
Answer :


Answered By : null Replied Date : Sep 30 2010
Answer :


Answered By : aaaaaa Replied Date : Apr 27 2010
Answer : aaaaaaaaa


Answered By : null Replied Date : Aug 21 2010
Answer :


Answered By : null Replied Date : May 28 2012
Answer :


Answered By : null Replied Date : Jun 28 2012
Answer :


Answered By : null Replied Date : Jul 29 2012
Answer :


Answered By : null Replied Date : Aug 26 2012
Answer :


Answered By : null Replied Date : Aug 29 2012
Answer :


Answered By : null Replied Date : Sep 5 2012
Answer :


Answered By : null Replied Date : Sep 8 2012
Answer :


Answered By : null Replied Date : Oct 10 2012
Answer :


Answered By : null Replied Date : Nov 27 2012
Answer :


Answered By : null Replied Date : Dec 17 2012
Answer :


Answered By : null Replied Date : Dec 26 2012
Answer :


Answered By : null Replied Date : Dec 29 2012
Answer :


Answered By : null Replied Date : Jan 12 2013
Answer :


Answered By : null Replied Date : Mar 24 2013
Answer :


Answered By : null Replied Date : Apr 15 2013
Answer :


 

You can also contribute to this answer:

Your Name:
Answer:

 
Ask Question and get answer from Expert.
View Answers List from Expert.

The information you are posting should be related to java and ORACLE technology. Not political.