Tech I Enjoy Logo

Custom Search




  Home >> Struts

Hereby I shall be discussing one of my ignorance/mistake whatsoever you may call it,
while putting various pieces/parts of a web application based on Struts 1.x version
including form input validation using Apache Commons Validator framework.

As of doing a very simple example, with a single text field to accept
user input from Browser based UI, I was not getting the output error
message that I defined in the MessageResources.properties for that perticular
text field.

Let me explain various files/parts of this example using Struts 1.x version.

Index JSP file is written using Struts1.x Tag Libraries, as follows:

index.jsp
<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>

<html:html>
<head>
<title><bean:message key="index.title.label"/></title>
<html:base/>
</head>
<body bgcolor="white">
<html:errors/>
<table>
<html:form method="post" action="example">
<tr><td><bean:message key="index.name.label"/></td>
<td><html:text property="name"/></td></tr>
<tr><td><html:submit property="butSubmit" value="Submit"/></td>
<td><html:button  property="butReset" value="Reset"/></td></tr>
</html:form>
</table>
</body>
</html:html>
From this index.jsp file, everything looks okay to me, what do you think? I have put the HTML:ERRORS tag to display any error message that is present in the request/session which ever is defined in the scope for the action definition in the struts-config.xml file. struts-config.xml file has the appropriate mapping for the action "example" as shown below: struts-config.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

<struts-config>
    <form-beans>
    <form-bean name="exampleForm"
               type="action.ExampleForm"/>
    </form-beans>
    <global-exceptions>
    </global-exceptions>
    <global-forwards>
    </global-forwards>
    <action-mappings>
        <action path="/example"
                type="action.ExampleAction"
                name="exampleForm"
                scope="request" validate="true" input="/index.jsp">
		
	  <forward name="success" path="/preview.jsp"/>
    </action>
    </action-mappings>


  <message-resources parameter="MessageResources" />

  <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
    <set-property property="pathnames" 
     value="/WEB-INF/validator-rules.xml,/WEB-INF/classes/action/validation.xml"/>
  </plug-in>

</struts-config>
Isn't this configuration file looks correct for this example, with the action class as action.ExampleAction, and the ActionForm as action.ExampleForm. Action Form "ExampleForm" extending ValidatorForm from org.apache.struts.validator package. So have a look at the ExampleForm as follows: action.ExampleForm
package action;
import org.apache.struts.validator.ValidatorForm;
import java.util.Date;

public class ExampleForm extends ValidatorForm
{
    private String name;
    public ExampleForm() {
	}
	public void setName(String argName) {
		name = argName;
	}
	public String getName() {
		return name;
	}
}
And the Action class ExampleAction extends Action from org.apache.struts.action package, and has the execute method overridden, as shown below: ExampleAction.java
package action;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import  java.io.IOException;
import javax.servlet.ServletException;


public class ExampleAction extends Action
{
        public ActionForward execute(ActionMapping mapping,
                                     ActionForm form,
                                     HttpServletRequest request,
                                     HttpServletResponse response) 
                                     throws Exception {
                //System.out.println("In EampleAction");
                ExampleForm exampleForm = (ExampleForm) form;
                System.out.println("Value of Name:"+exampleForm.getName());
                return mapping.findForward("success");
        }

}
This example action class just getting the ExampleForm and printing the value for the name field, and then forwards the request to success property in action mapping to the preview.jsp page. As you might have observed in struts-config.xml file (as shown above) two attributes for action tag in bold
  • validte="true"
  • input="/index.jsp"
  • So Apache Commons Validator framework validation is enabled/used here. In order to validate user input text field, validation.xml is placed in "/WEB-INF/classes/action/" folder and validation-rules.xml files is placed in /WEB-INF/ folder with an entry in struts-config.xml file for the ValidatorPlugIn from org.apache.struts.validator package. validation.xml
    <?xml version="1.0" encoding="ISO-8859-1" ?>
    
    <!DOCTYPE form-validation PUBLIC
    "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN"
          "http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd">
    
    <form-validation>
        <global>
        </global>
        <formset>
            <form name="exampleForm">
                <field property="name" depends="required">
                        <arg0 key="exampleForm.name.text"/>
                </field>
            </form>
        </formset>
    </form-validation>
    
    From this validation configuration file with the depends="required" means we want mandatory value in the input text field. "arg0" tag with the key as "exampleForm.name.text". Why is this arg0 tag required? This arg0 tag basically is used to form the final error message from resource bundle tht is "MessageResources.properties". In MessageResources.properties file, the key "exampleForm.name.text" is defined with its value as shown below: MessageResources.properties
    index.name.label=Name :
    index.title.label=Struts1 Plugin Example
    exampleForm.name.text=Name
    
    The final page is the preview page and is a very simple with a out.println message as follows: preview.jsp
    <%
    out.println("Preview Page");
    %>
    
    After assembling a simple web application folder structure in Tomcat web server, and to my utmost surprise my example application is validating the blank input text field, but displaying the error message on screen as null. I checked my files for mistakes for a day or two but couldn't get it wright. And then took some break and start exploring documentation for the Validation configuration tags such as arg and got to know that this arg tag value goes to the errors.required={0} is required. as the argument. And then I realized that my MessageResources.properties file doesn't have this standard entry, as i have created this file from scratch, as you can see above. Now I added this key value pair in the MessageResources.properties file as follows: MessageResources.properties
    errors.required={0} is required.
    index.name.label=Name :
    index.title.label=Struts1 Plugin Example
    exampleForm.name.text=Name
    
    After correcting this silly but easy to overlook property, I am really happy and excited to find the final validated error message showing on screen as Name is required. just above the text field when no input is typed for the input text field on screen. Following set of JAR files I needed to make this example work:
  • antlr.jar
  • commons-beanutils.jar
  • commons-digester.jar
  • commons-fileupload.jar
  • commons-logging.jar
  • commons-validator.jar
  • jakarta-oro.jar
  • struts.jar
  • If you like to share your comment/suggestions/feedback relating to this Page, you can do so by droping us an email at usingframeworks @ gmail . com with the subject line mentioning URL for this Page (i.e, /Struts-validation-struggle.php) or use this LINK. As per this website's privacy policy, we never disclose your email id, though we shall post your comments/suggestions/feedback with your name (optional) and date on this Page. If you don't want your comments/suggestions/feedback to be shared in this Page, please mention so in your email to us. Thank you very much..... If anything missed out , please let me know at techienjoy at yahoo . com
    Apache Struts 2 Example Steps :
    Example Steps of using Struts 2 and code explained.
    Apache Struts 2 Radio Tag Example :
    Example Steps of using Struts 2 Radio Tags
    and code explained.
    Apache Struts with DOJO Example Part-4 :
    Example of DOJO Toolkit with Struts and 
    code explained.
    Apache Struts 2 Validation With Expression :
    Validation with Expression using Struts 2
    and Example code explained.
    Apache Struts 2 Validation With Example :
    Validation Example using Struts 2
    and Example code explained.
    Apache Struts 2 Custom Validator :
    Example Steps of using Struts 2 with Custom Validator
    and code explained.
    Apache Struts Validation with Example :
    Example of Validation using Struts and 
    code explained.
    Apache Struts 2 PDF Result :
    Example Steps of using Struts 2 with PDF Result
    and code explained.
    Apache Struts with DOJO Example Part-2 :
    Example of DOJO Toolkit with Struts and 
    code explained.
    Struts 2 Plugin Example :
    Example using Struts 2 Plugin
    Apache Struts 2 Validation Example :
    Validation Example Steps of using Struts 2
    and Example code explained.
    List of Struts Example :
    Examples List using Struts 2 and 
    code explained.
    Struts 2 Tiles and I18N Example :
    Example using Tiles and I18N with Struts 2 and 
    code explained.
    Apache Struts with DOJO Example Part-1 :
    Example of DOJO Toolkit with Struts and 
    code explained.
    Struts 2 Result Chain Example :
    Example using Result Chain with Struts 2 and 
    code explained.
    Struts 2 with JSF Example :
    Example using JSF with Struts 2 and 
    code explained.
    Apache Struts 2 and XSLT With Example :
    XSLT Example using Struts 2 and Example 
    code explained.
    Apache Struts 2 Tags Example :
    Example of Struts 2 Tags and code explained.
    Apache Struts with DOJO Example Part-3 :
    Example of DOJO Toolkit with Struts and 
    code explained.
    Struts Validation Example :
    Example using Validation with Struts 2 and 
    code explained.
    Apache Struts 2 Tiles and I18N Example :
    Example Steps of using Struts 2 With Tiles
    and I18N code explained.
    Apache Struts Date Validation with Example :
    Example of Date Validation using Struts and 
    code explained.
    Apache Struts 2 upload Example :
    Uploading Example Steps of using Struts 2
    and Example code explained.
    Apache Struts 2 Tags Example :
    Example Steps of using Struts 2 Tags
    and code explained.
    Apache Struts 2 Result PlainText Example :
    Example Steps of using Struts 2 PlainText Result
    and code explained.


    References :
    Tags: Struts 1 Struts2 plugin
    Tags: struts jsf integrate
    Tags: struts result chain
    Tags: struts tiles i18n
    Tags: Struts validation struggle
    Tags: Struts
    Tags: struts1 date validation
    Tags: struts1 struts2 validation
    Tags: struts2 dojo 1
    Tags: struts2 dojo 2
    Tags: struts2 dojo 3
    Tags: struts2 dojo 4
    Tags: Struts2 example steps Tag usage
    Tags: Struts2 example steps
    Tags: struts2 own validator
    Tags: struts2 pdf result
    Tags: struts2 radio tag example
    Tags: struts2 result plaintext
    Tags: struts2 tags example
    Tags: struts2 tiles i18n
    Tags: struts2 upload example
    Tags: struts2 validation 1
    Tags: struts2 validation expression
    Tags: struts2 validation
    Tags: struts2 xslt article
    

    
    
    DISCLAIMER :
    The content provided in this page is not warranted and/or guaranteed by techienjoy.com. 
    techienjoy.com is not liable for any negative consequences that may result/arise from 
    implementing directly/indirectly any information covered in these pages/articles/tutorials.
    
    All contents of this site is/are written and provided on an "AS IS" basis,
    without WARRANTIES or conditions of any kind, either express or implied, including, without
    limitation, merchantability, or fitness for a particular purpose. You are solely responsible
    for determining the appropriateness of using or refering this and assume any risks associated
    with this.
    
    In spite of all precautions taken to avoid any typo in these pages, there might be some 
    issues like grammatical mistakes and typos being observed in these pages, techienjoy.com
    extends sincerest apologies to all our visitors for the same.
    
    
    

    Android Examples || Android Examples

    © Copyright 2010-2012, TECHIENJOY, All Rights Reserved.      Privacy Policy     Disclaimer & Terms & Conditions