Struts 2 Url Validator

The URLValidator of Struts 2 Framework checks whether the String contained within the given field is a valid URL or not. If the entered value is not a valid URL, it generates an error message.

Ads

Struts 2 Url Validator

     

The URLValidator of Struts 2 Framework checks whether the String contained within the given field is a valid URL or not. If the entered value is not a valid URL, it generates an error message. 

The error message is supplied between the <message> </message> tag. The following example demonstrates how to use the URL validator. 

Follow the steps to develop the  URL validator example:

Step 1: Create the struts.xml file and add the following xml snippet in the struts.xml file.

<?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>
  <!-- Rose India Struts Tutorials -->
  <constant name="struts.enable.DynamicMethodInvocation" value="false" />
  <constant name="struts.devMode" value="true" />

  <package name="roseindia" namespace="/roseindia" extends="struts-default">
  
  <!-- Add actions here -->

  <!-- Url validation -->

  <action name="urlValidation">
  <result>/pages/urlInputForm.jsp</result>
  </action>

  <action name="urlValidation1" class="net.roseindia.urlVaLidationAction">
  <result name="input">/pages/urlInputForm.jsp</result>
  <result name="error">/pages/urlInputForm.jsp</result>
  <result>/pages/urlSuccess.jsp</result>
  </action>

 <!-- Add actions here -->
  </package>


  <!-- Add packages here -->

</struts>

Step 2 : Create an input  jsp form.

urlInputForm.jsp

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

<html>

<head>

<title>Url input form</title>
 <link href="<s:url value="/css/main.css"/>" rel="stylesheet"
  type="text/css"/>
  <s:head/>
</head>

<body>
  
  <s:form method="POST" action="urlValidation1">
 </td>
  </tr>
  <s:textfield label="Enter Url" name="url" />
  <s:submit />
  </s:form>

</body>

</html>

Step 3 : Create an Action class.

urlVaLidationAction.java

package net.roseindia;
import com.opensymphony.xwork2.ActionSupport;

public class urlVaLidationAction extends ActionSupport {

  private String url;

  public void setUrl(String url){
  this.url = url;
  }
  public String getUrl(){
  return url;
  }
}

Step 4 : Create an URL validator as shown: 

The validation.xml format is either <ActionClassName>-validation.xml or <ActionClassName>-<ActionAliasName>-validation.xml.

URL validator:  This Field Validator checks whether a given field contains a valid URL  String or not. If the entered text is valid then you get the urlSuccess.jsp page. Otherwise, it displays the message given  in the xml file (like "Please enter a valid URL.").

The URL validator takes the following parameters: 

  • fieldName - It refers to the field name, the URL validator is validating.
    It is required if using Plain-Validator Syntax 

urlVaLidationAction-validation.xml

<?xml version="1.0" encoding="UTF-8"?>
<!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="url">
  <field-validator type="url">
 <message>Please enter a valid url</message>
  </field-validator>
  </field>

</validators>

When the correct URL is entered the user gets the urlSuccess.jsp page displaying the entered URL.

urlSuccess.jsp

<%@page language="java" %>
<html>

<head>
  <title>Correct entry</title>
</head>

<body>
<b>URL =  </b><%=request.getParameter("url"%>!
</body>

</html>

Output:

When this application executes initially the user gets the following:

 If the user enters the wrong URL then he gets:

Again,  the user enters the wrong URL then he gets:

If the user enters the correct URL as

then he gets a success message as an output:

     

Advertisements

Ads
Share on Google+Share on Google+

Struts 2 Url Validator

Posted on: December 18, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Struts 2 Url Validator   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads