Struts 2 requiredstring validation example


The requiredstring validator indicates that string can't be invalid or blank.

It trims the string bydefault then checks if its length is more prominent that 0.

Parameters of requiredstring validator

There are two parameters characterized for requiredstring validator.

ParameterDescription
fieldNamespecifies the field name that will be approved. It is required in Plain-Validator only.
trimtrims the field esteems. It is bydefault genuine methods it is empowered bydefault.

Example of requiredstring validator

  1. <validators>  
  2.     <!- -  Plain-Validator Syntax - - >  
  3.     <validator type="requiredstring">  
  4.         <param name="fieldName">username</param>  
  5.         <param name="trim">true</param>  
  6.         <message>username is required</message>  
  7.     </validator>  
  8.       
  9. </validators>  
  1. <validators>  
  2.     <!- -  Field-Validator Syntax - - >  
  3.     <field name="username">  
  4.           <field-validator type="requiredstring">  
  5.             <param name="trim">true</param>  
  6.             <message>username is required</message>  
  7.        </field-validator>  
  8.     </field>  
  9.   
  10. </validators>  

Full case of requiredstring validator

1) Create index.jsp for information

This jsp page makes a structure utilizing Javacodegeeks.Net UI labels. It gets name, secret word and email id from the client.

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="username" label="Username"></s:textfield>  
  12. <s:password name="userpass" label="Password"></s:password>  
  13. <s:submit value="register"></s:submit>  
  14. </s:form>  
  15.   
  16. </body>  
  17. </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 username,userpass;  
  7.   
  8. public String getUsername() {  
  9.     return username;  
  10. }  
  11.   
  12. public void setUsername(String username) {  
  13.     this.username = username;  
  14. }  
  15.   
  16. public String getUserpass() {  
  17.     return userpass;  
  18. }  
  19.   
  20. public void setUserpass(String userpass) {  
  21.     this.userpass = userpass;  
  22. }  
  23.   
  24. public String execute(){  
  25.     return "success";  
  26. }  
  27.   
  28. }  

3) Create the approval file

Here, we are utilizing packaged validators to play out 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="username">  
  10.         <field-validator type="requiredstring">  
  11.         <message>Name can't be blank</message>  
  12.         </field-validator>  
  13.         </field>  
  14.           
  15.           
  16.         </validators>  

4) Create struts.xml

This xml record characterizes an additional outcome 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/Javacodegeeks.Net 2.1.dtd">  
  3. <struts>  
  4.   
  5. <package name="default" extends="struts-default">  
  6. <action name="register" class="com.javatpoint.Register">  
  7. <result name="input">index.jsp</result>  
  8. <result>welcome.jsp</result>  
  9. </action>  
  10.   
  11. </package>  
  12. </struts>      
  13.    

5) Create see component

It is the basic jsp record showing the data of the user.

welcome.jsp
  1. <%@ taglib uri="/Javacodegeeks.Net tags" prefix="s" %>  
  2.   
  3. Welcome,<s:property value="username"/>  





javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition