July 14, 2011

Struts 2 - Front-end validation with the Validation Framework

In this code example, we replace the validate() method for basic validation method in the example here with the validation framework.

Front-end validation with the Validation framework lets us configure the validations in a XML file (validation meta-data) and re-use the various, default validators that are provided by the framework.

1) Create the FeedbackForm.jsp and FeedbackDone.jsp files in the WebContent folder of the Web Application

FeedbackForm.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<body>
<h4>Enter Feedback</h4>
  <s:form action="PostFeedback">
     <s:textfield name="emailAddress" label="Email Address" />
     <s:textfield name="phoneNumber" label="Phone Number" />
     <s:textarea name="message" label="Message" cols="20" rows="4" />
     <s:submit/>
  </s:form>
</body>
</html>

FeedbackDone.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<body>
<h3>Your feedback is as follows</h3>
   <s:property value="emailAddress"/><br/>
   <s:property value="phoneNumber"/><br/>
   <s:property value="message"/><br/>
</body>
</html>

2) Create the action class - Feedback.java - in the src folder of the Web Application.

package info.icontraining.struts2;

import com.opensymphony.xwork2.*;

public class Feedback extends ActionSupport {

   public String execute() { 
      return Action.SUCCESS;
   }
 
   private String emailAddress;
   private String phoneNumber;
   private String message;
 
   public String getEmailAddress() {
      return emailAddress;
   }
   public void setEmailAddress(String emailAddress) {
      this.emailAddress = emailAddress;
   }
 
   public String getPhoneNumber() {
      return phoneNumber;
   }
   public void setPhoneNumber(String phoneNumber) {
      this.phoneNumber = phoneNumber;
   }
 
   public String getMessage() {
      return message;
   }
   public void setMessage(String message) {
      this.message = message;
   }
}

3) Configure the actions in the struts.xml configuration file,

<action name="Feedback" >
   <result>/FeedbackForm.jsp</result>
</action>
  
<action name="PostFeedback" class="info.icontraining.struts2.Feedback">
   <result>/FeedbackDone.jsp</result>
   <result name="input">/FeedbackForm.jsp</result>
</action>

4) Add a new validation framework XML configuration file for the action. This file should be added inside the package where the action class is present along with the properties file. The properties file will prevent the hard-coding of error messages inside the XML configuration file.

The naming convention for the Validation XML file for Struts 2 is ActionClassName-validation.xml
Therefore, create a file, Feedback-validation.xml and add it to the package in which the action class Feedback.java is present

<!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="emailAddress">
      <field-validator type="requiredstring">
         <message>Please enter email address</message>
      </field-validator>
      <field-validator type="email">
         <message key="email.invalid" />
      </field-validator>
   </field>
   <field name="phoneNumber">
      <field-validator type="requiredstring">
         <message>Please enter phone number</message>
      </field-validator>
      <field-validator type="regex">
         <param name="expression"><![CDATA[^(\d){3}-(\d){3}-(\d){4}$]]></param>
         <message key="phone.invalid" />
      </field-validator>
   </field>
   <field name="message">
      <field-validator type="requiredstring">
         <message>Please enter message</message>
      </field-validator>
   </field>
</validators>

5) Add the Feedback.properties tile to the package in which the action class Feedback.java is present

email.invalid=Please enter valid email
phone.invalid=Please enter valid phone (xxx-xxx-xxxx)

6) Access the action by typing the following URL in the browser,

http://localhost:8080/WebAppName/Feedback.action

No comments:

Post a Comment