Friday, 10 July 2015

Struts 2 url validation example

The url validator checks that given value is string and a valid url. It can be used in website url, reciprocal links etc.

Parameters of url validator

There is only 1 parameter defined for url validator.
ParameterDescription
fieldNamespecifies the field name that is to be validated. It is required in Plain-Validator only.

Example of url validator

  1. <validators>  
  2.  <!-- Plain Validator Syntax -->  
  3.          <validator type="url">  
  4.              <param name="fieldName">website</param>  
  5.              <message>Invalid website url</message>  
  6.          </validator>  
  7.       
  8. </validators>  
  1. <validators>  
  2.   <!-- Field Validator Syntax -->  
  3.   <field-validator type="url">  
  4.          <field name="website">  
  5.              <message>Invalid website url</message>  
  6.          </field>  
  7. </field-validator>  
  8.   
  9. </validators>  

Full example of url validator

1) Create index.jsp for input

This jsp page creates a form using struts UI tags. It receives name, password and email id from the user.
index.jsp
  1. <%@ taglib uri="/struts-tags" prefix="s" %>  
  2. <html>  
  3. <head>  
  4. <STYLE type="text/css">  
  5. .errorMessage{color:red;}  
  6. </STYLE>  
  7. </head>  
  8. <body>  
  9.   
  10. <s:form action="register">  
  11. <s:textfield name="url" label="Website URL"></s:textfield>  
  12. <s:submit value="register"></s:submit>  
  13. </s:form>  
  14.   
  15. </body>  
  16. </html>  

2) Create the action class

This action class inherits the ActionSupport class and overrides the execute method.
RegisterAction.java
  1. package com.javatpoint;  
  2.   
  3. import com.opensymphony.xwork2.ActionSupport;  
  4.   
  5. public class Register extends ActionSupport{  
  6. private String url;  
  7.   
  8. public String getUrl() {  
  9.     return url;  
  10. }  
  11.   
  12. public void setUrl(String url) {  
  13.     this.url = url;  
  14. }  
  15.   
  16. public String execute(){  
  17.     return "success";  
  18. }  
  19.   
  20. }  

3) Create the validation file

Here, we are using bundled validators to perform the validation.
Register-validation.xml
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3.   <!DOCTYPE validators PUBLIC   
  4.         "-//OpenSymphony Group//XWork Validator 1.0.2//EN"   
  5.         "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">  
  6.   
  7.         <validators>  
  8.           
  9.         <field name="url">  
  10.         <field-validator type="requiredstring">  
  11.         <message>URL can't be blank</message>  
  12.         </field-validator>  
  13.         <field-validator type="url">  
  14.         <message>URL must be correct e.g. http://www.javatpoint.com</message>  
  15.         </field-validator>  
  16.         </field>  
  17.           
  18.           
  19.         </validators>  

4) Create struts.xml

This xml file defines an extra result by the name input, and an interceptor jsonValidatorWorkflowStack.
struts.xml
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">  
  3. <struts>  
  4. <package name="default" extends="struts-default">  
  5. <action name="register" class="com.javatpoint.Register">  
  6. <result name="input">index.jsp</result>  
  7. <result>welcome.jsp</result>  
  8. </action>  
  9. </package>  
  10. </struts>      

5) Create view component

It is the simple jsp file displaying the information of the user.
welcome.jsp
  1. <%@ taglib uri="/struts-tags" prefix="s" %>  
  2.   
  3. URL is:<s:property value="url"/>