Home Struts Struts Struts2.2.1 Example of struts2.2.1 field validator.

Ask Questions?

View Latest Questions


 
 

Example of struts2.2.1 field validator.
Posted on: February 7, 2011 at 12:00 AM
Advertisement
In this section, we will introduce you to about the field validator.

Example of struts2.2.1 field validator.

In this section, you will see the use how to validate form field or user input in struts2. 
Struts2 used a xml file for validation called validation.xml

Struts2 provides two ways for defining validator in XML file.

  1. Field validator
  2. Non-field validator

Field validator- It works on single field of form. It define validator per field base.

<validators>
<field name="username">
<field-validator type="required">
<message>User name required.</message>
</field-validator>
</field>
</validators>

Here, you will see a email validation example.

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

pageEncoding="ISO-8859-1"%>

<%@taglib uri="/struts-tags" prefix="s" %>

<html>

<head><title>Email_Validator_Example</title><s:head/></head>

<body>Email_Validator_Example.....

<s:form action="emailvalidation.action">

<s:textfield name="email" label="Email-Id :"></s:textfield>

<s:submit></s:submit></s:form>

</body>

</html>

EmailValidationAction.java (Action )

package roseindia;

import com.opensymphony.xwork2.ActionSupport;

public class EmailValidationAction extends ActionSupport {

private String email;

public String getEmail() {

return email; }

public void setEmail(String email) {

this.email = email; }

@Override

public String execute() throws Exception {

// TODO Auto-generated method stub

return SUCCESS; }

}

EmailValidationAction-validation.xml 

<!DOCTYPE validators PUBLIC

"-//OpenSymphony Group//XWork Validator 1.0.2//EN"

"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">

<validators>

<field name="email">

<field-validator type="requiredstring">

<message>Email id is required</message>

</field-validator>

<field-validator type="email">

<message>Please enter valid email id.</message>

</field-validator>

</field>

</validators>

struts.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<constant name="struts.enable.DynamicMethodInvocation" value="false" />

<constant name="struts.devMode" value="false" />

<package name="roseindia" namespace="/" extends="struts-default">

<action name="emailvalidation" class="roseindia.EmailValidationAction">

<result name="input">index.jsp</result>

<result name="error">index.jsp</result>

<result>successJsp.jsp</result>

</action></package>

</struts>

success.jsp

<%@ page language="java" 

contentType="text/html; charset=ISO-8859-1"

pageEncoding="ISO-8859-1"%>

<%@taglib uri="/struts-tags" prefix="s" %>

<html>

<head><title>Insert title here</title></head>

<body>Email-Id....

<s:property value="email"/></body>

</html>

Output

Download Source Code


Related Tags for Example of struts2.2.1 field validator.:


DMCA.com