Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
URL Validator
2.1.
Details of the URL Validator
3.
Example of URL Validator
4.
Full Example of URL Validator
4.1.
Create index.jsp for Input
4.2.
Create the Action Class
4.3.
Create the Validation File
4.4.
Create struts.xml
4.5.
Create View Component
4.6.
OUTPUT
5.
Frequently Asked Questions
5.1.
Which two sorts of validations does the Validator framework support?
5.2.
How can we validate URL in HTML?
5.3.
How does the Struts 2 validation work?
5.4.
What is the validation framework in Struts?
5.5.
What is a validation framework?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Url Validator

Introduction

Link validation pings a URL's final destination to check for problems. This is very helpful for bloggers as it helps to prevent broken and invalid links in your published document.

We'll learn about URL validators in this blog.

Learning CN

URL Validator

The URL validator verifies that the supplied value is a valid URL and a string. It can be used for website URLs, exchanged links, etc.

Details of the URL Validator

For the URL validator, just one parameter has been defined.

Parameters of URL validator

Example of URL Validator

<validators>  
<!-- Plain Validator Syntax -->  
       <validator type="url">  
           <param name="fieldName">website</param>  
           <message>Invalid website url</message>  
       </validator>  
     
</validators>  
<validators>  
 <!-- Field Validator Syntax -->  
 <field-validator type="url">  
       <field name="website">  
           <message>Invalid website url</message>  
       </field>  
</field-validator>  
 
</validators>  

Full Example of URL Validator

Create index.jsp for Input

Utilizing the struts UI tags, this JSP website generates a form. The user's name, password, and email address are obtained.

Struts

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>  

Create the Action Class

The execute method is overridden in this action class, which derives from the ActionSupport class.

RegisterAction.java

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";  
}  
 
}  

Create the Validation File

In this case, the validation is carried out utilizing bundled validators.

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. XYZ.com</message>  
       </field-validator>  
       </field>  
         
         
       </validators>  

 

Create struts.xml

This XML file specifies a workflow stack interceptor and a different outcome with the name ValidatorWorkflowStack.

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>

Create View Component

It is a straightforward JSP file that shows the user's information.

welcome.jsp

<%@ taglib uri="/struts-tags" prefix="s" %>  
 
URL is:<s:property value="url"/>  

OUTPUT

OUTPUT

Enter any username and url and click on the login button.

Frequently Asked Questions

Which two sorts of validations does the Validator framework support?

First, using Custom Validation Here, we must supply the implementation of the validate method and implement the Validatable interface (or extend the ActionSupport class). 

Second, Utilizing Input Validation (built-in validators), Struts 2 has various preset validation features that can be utilized in struts two applications.

How can we validate URL in HTML?

A field for entering a URL is defined using the input type="URL" tag. Before the form can be submitted, the input value is automatically validated.

How does the Struts 2 validation work?

The validation framework, an essential component of Struts, helps the application run the rules for validating before the action method is called. Typically, Javascript is used to implement client-side validation. However, client-side validation should not be the only method used.

What is the validation framework in Struts?

You can specify validation rules using the Validation Framework, and either the client-side or the server-side can implement these rules. Using JBoss Developer Studio's specific editor for the XML files that manage validation in a project makes using the Validation Framework in Struts considerably simpler.

What is a validation framework?

Using Spring's robust expression evaluation engine, the validation framework evaluates the Validator's applicability criteria and validation rules. As a result, any valid validator's test and attributes can accept any good Spring expression.

Conclusion

So that's the end of the article. url validator

After reading about the url validator, Are you interested in reading/exploring more themes on Validator? Don't worry; Coding Ninjas has you covered.

However, if you want to give your work an edge over the competition, you might choose to enroll in one of our premium courses.

With our Coding Ninjas Studio Guided Path, you may learn about Data Structures & Algorithms, Competitive Programming, JavaScript, System Design, and more! If you want to put your coding skills to the test, check out the mock test series on Coding Ninjas Studio and participate in the contests! But if you've only recently started your schooling and are looking for answers to issues presented by digital titans like Amazon, Microsoft, Uber, and others. In this situation, you must consider the obstaclesinterview experiences, and interview package as part of your placement preparations. If you find our blogs valuable and fascinating, please vote them up!

Good luck with your studies!

Live masterclass