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.
Parameter | Description |
fieldName | specifies the field name that is to be validated. It is required in Plain-Validator only. |
Example of url validator
- <validators>
-
- <validator type="url">
- <param name="fieldName">website</param>
- <message>Invalid website url</message>
- </validator>
-
- </validators>
- <validators>
-
- <field-validator type="url">
- <field name="website">
- <message>Invalid website url</message>
- </field>
- </field-validator>
-
- </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
- <%@ taglib uri="/struts-tags" prefix="s" %>
- <html>
- <head>
- <STYLE type="text/css">
- .errorMessage{color:red;}
- </STYLE>
- </head>
- <body>
-
- <s:form action="register">
- <s:textfield name="url" label="Website URL"></s:textfield>
- <s:submit value="register"></s:submit>
- </s:form>
-
- </body>
- </html>
2) Create the action class
This action class inherits the ActionSupport class and overrides the execute method.
RegisterAction.java
- package com.javatpoint;
-
- import com.opensymphony.xwork2.ActionSupport;
-
- public class Register extends ActionSupport{
- private String url;
-
- public String getUrl() {
- return url;
- }
-
- public void setUrl(String url) {
- this.url = url;
- }
-
- public String execute(){
- return "success";
- }
-
- }
3) Create the validation file
Here, we are using bundled validators to perform the validation.
Register-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="requiredstring">
- <message>URL can't be blank</message>
- </field-validator>
- <field-validator type="url">
- <message>URL must be correct e.g. http://www.javatpoint.com</message>
- </field-validator>
- </field>
-
-
- </validators>
4) Create struts.xml
This xml file defines an extra result by the name input, and an interceptor jsonValidatorWorkflowStack.
struts.xml
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
- <struts>
- <package name="default" extends="struts-default">
- <action name="register" class="com.javatpoint.Register">
- <result name="input">index.jsp</result>
- <result>welcome.jsp</result>
- </action>
- </package>
- </struts>
5) Create view component
It is the simple jsp file displaying the information of the user.
welcome.jsp
- <%@ taglib uri="/struts-tags" prefix="s" %>
-
- URL is:<s:property value="url"/>