Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

requiredstring


The requiredstring validator specifies that string can’t be null or blank.

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

Parameters of requiredstring validator

There are two parameters defined for requiredstring validator.

Parameter Description
fieldName specifies the field name that is to be validated. It is required in Plain-Validator only.
trim trims the field values. It is bydefault true means it is enabled 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. </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. </validators>

Full example of requiredstring 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

  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. <s:form action=“register”>
  10. <s:textfield name=“username” label=“Username”></s:textfield>
  11. <s:password name=“userpass” label=“Password”></s:password>
  12. <s:submit value=“register”></s:submit>
  13. </s:form>
  14. </body>
  15. </html>

2) Create the action class

This action class inherits the ActionSupport class and overrides the execute method.

RegisterAction.java

  1. package com.Kreationnext;
  2. import com.opensymphony.xwork2.ActionSupport;
  3. public class Register extends ActionSupport{
  4. private String username,userpass;
  5. public String getUsername() {
  6.     return username;
  7. }
  8. public void setUsername(String username) {
  9.     this.username = username;
  10. }
  11. public String getUserpass() {
  12.     return userpass;
  13. }
  14. public void setUserpass(String userpass) {
  15.     this.userpass = userpass;
  16. }
  17. public String execute(){
  18.     return “success”;
  19. }
  20. }

3) Create the validation file

Here, we are using bundled validators to perform the validation.

Register-validation.xml

  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2.   <!DOCTYPE validators PUBLIC
  3.         “-//OpenSymphony Group//XWork Validator 1.0.2//EN”
  4.         “http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd”>
  5.         <validators>
  6.         <field name=“username”>
  7.         <field-validator type=“requiredstring”>
  8.         <message>Name can’t be blank</message>
  9.         </field-validator>
  10.         </field>
  11.         </validators>

4) Create struts.xml

This xml file defines an extra result 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/struts-2.1.dtd”>
  3. <struts>
  4. <package name=“default” extends=“struts-default”>
  5. <action name=“register” class=“com.Kreationnext.Register”>
  6. <result name=“input”>index.jsp</result>
  7. <result>welcome.jsp</result>
  8. </action>
  9. </package>
  10. </struts>

5) Create view component

It is the simple jsp file displaying the information of the user.

welcome.jsp

  1. <%@ taglib uri=“/struts-tags” prefix=“s” %>
  2. Welcome,<s:property value=“username”/>

Comments

comments