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.
Field validator- It works on single field of form. It define validator per field base.ADS_TO_REPLACE_1
<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"ADS_TO_REPLACE_2 pageEncoding="ISO-8859-1"%> <%@taglib uri="/struts-tags" prefix="s" %> <html>ADS_TO_REPLACE_3 <head><title>Email_Validator_Example</title><s:head/></head> <body>Email_Validator_Example..... <s:form action="emailvalidation.action">ADS_TO_REPLACE_4 <s:textfield name="email" label="Email-Id :"></s:textfield> <s:submit></s:submit></s:form> </body>ADS_TO_REPLACE_5 </html> |
EmailValidationAction.java (Action )
package roseindia; import com.opensymphony.xwork2.ActionSupport; public class EmailValidationAction extends ActionSupport {ADS_TO_REPLACE_6 private String email; public String getEmail() { return email; }ADS_TO_REPLACE_7 public void setEmail(String email) { this.email = email; } @OverrideADS_TO_REPLACE_8 public String execute() throws Exception { // TODO Auto-generated method stub return SUCCESS; }ADS_TO_REPLACE_9 } |
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">ADS_TO_REPLACE_10 <validators> <field name="email"> <field-validator type="requiredstring">ADS_TO_REPLACE_11 <message>Email id is required</message> </field-validator> <field-validator type="email">ADS_TO_REPLACE_12 <message>Please enter valid email id.</message> </field-validator> </field>ADS_TO_REPLACE_13 </validators> |
struts.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLICADS_TO_REPLACE_14 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts>ADS_TO_REPLACE_15 <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="false" /> <package name="roseindia" namespace="/" extends="struts-default">ADS_TO_REPLACE_16 <action name="emailvalidation" class="roseindia.EmailValidationAction"> <result name="input">index.jsp</result> <result name="error">index.jsp</result>ADS_TO_REPLACE_17 <result>successJsp.jsp</result> </action></package> </struts> |
success.jspADS_TO_REPLACE_18
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>ADS_TO_REPLACE_19 <%@taglib uri="/struts-tags" prefix="s" %> <html> <head><title>Insert title here</title></head>ADS_TO_REPLACE_20 <body>Email-Id.... <s:property value="email"/></body> </html> |
OutputADS_TO_REPLACE_21
ADS_TO_REPLACE_22
Download Source CodeADS_TO_REPLACE_23
Advertisements
Ads
Ads