The URL validator in Struts can be used to check whether value of a String field is a valid URL or not. It can be used in either of the following forms:

  • XML: using type=”url” attribute in <validator> or <field-validator> elements.
  • Annotation: using @UrlValidator annotation type to annotate setter method of the field or action method.
NOTES: This validator does not treat empty value as invalid URL, thus it’s recommended to use this validator in conjunction with the required string validator if the field is also mandatory.

 

1. Struts URL Validator XML

Usage:

  • Field-validator syntax:
    <field name="fieldName">
    	<field-validator type="url">
    		<message>validation error message</message>
    	</field-validator>
    </field>
     

  • Plain-validator syntax:
    <validator type="url">
    		<param name="fieldName">myHomePage</param>
    		<message>validation error message</message>
    </validator>
     

Parameters:

Parameter name

Description

fieldName

Name of the field to validate. Required if using plain validator syntax.

Struts URL Validator XML Examples:

  • Field-validator example:
    <field name="myHomePage">
    	<field-validator type="url">
    		<message>You must enter a valid URL</message>
    	</field-validator>
    </field>
     

  • Plain-validator example:
    <validator type="url">
    	<param name="fieldName">myHomePage</param>
    	<message>Please specify a valid URL.</message>
    </validator> 
 

2. Struts @UrlValidator Annotation

Usage: Using the following annotation type before the setter method or action method (in case of using plain-validator):

@UrlValidator(param1 = "param 1 value", param2 = "param 2 value", ...)

Parameters:

Parameter name

Required

Default value

Description

message

Yes

 

validation error message.

key

No

 

i18n key for validation error message.

messageParams

No

 

Additional parameters to customize the message.

fieldName

No

 

Specifies field name in case this validator type is plain-validator.

shortCircuit

No

false

Whether this validator is short circuit.

type

No

ValidatorType.FIELD

type of the validator: field-validator (FIELD) or plain-validator (SIMPLE).



 

Struts @UrlValidator Annotation Examples:

  • Basic field-validator:
    @UrlValidator(message = "Please enter a valid URL for your home page.")
    public void setMyHomePage(String myHomePage) {
    	this.myHomePage = myHomePage;
    }
     

  • Specifying i18n key for the message:
    @UrlValidator(message = "Default message", key = "form.validation.homePage")
    public void setMyHomePage(String myHomePage) {
    	this.myHomePage = myHomePage;
    }
     

  • Plain-validator (annotating the action method):
    @UrlValidator(type = ValidatorType.SIMPLE,
    	message = "Please enter a valid URL for your home page.",
    	fieldName = "myHomePage")
    public String execute() {
    	return SUCCESS;
    }
     

Related Struts Form Validation Tutorials:

 

Other Struts Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Add comment