| |
Struts Tutorials:
Struts2 Tag CheckBoxList , Checkbox, Iterator, IF
Struts2 Tag Library Example
Struts2 Tiles Example
Struts2 Tiles I18N Example
Struts2 Questions
Struts Tiles I18N Example
Struts Eclipse MVC
Struts2 Tags
Struts2 Example and Tutorial
Struts MVC
Struts2 Validation
Hibernate Tutorials:
Hibernate Case Study
Class Hierarchy Persist Example
Using Hibernate Interceptor
Hibernate Questions with Answer
Hibernate Many-to-Many Mapping Example
Hibernate one-to-many Mapping Example
Hibernate and ORM tools
Spring Hibernate Example
Hibernate SessionFactory Example
Hibernate Mapping Class Hierarchy
Hibernate Questions
Hibernate SessionFactory Questions
Spring Hibernate Example:
Spring Hibernate Case Study
Written By : Amit
Title :
MVC Struts
Description :
Some facts about MVC Framework and Struts More...
|
Written By : Amit
Title :
Struts2 example tutorial
Description :
My First example using Apache Struts2 More...
|
Written By : ISHTEK
Title :
Struts2
Description :
Tags such as IF Iterator Tags More...
|
Written By : guddu
Title :
Struts Eclipse MVC
Description :
Step by step demonstration More...
|
Written By : Girish
Title :
Struts Tiles I18N Example
Description :
Code with Sample Action More...
|
| Tags/Keywords : Struts, Struts2 Tutorial, Struts 2 Example, Struts2 Author : Amit Date (Year/Month/Date): 2009-12-01
Struts 1.x Validation example with some case study discussed | |
Please be informed that NONE of the design/code from this
page is claiming to be some sort of best practices and we DO NOT expect
any of our visitor/reader of this page to assume this as some sort of
best practice for any context and should not be using this
as it is without appropriate evaluation to their, so to say,
specific programming context.
This page intends only to provide bit and piece of known ways for
doing some sort of example and may not be fit for any other purpose.
My mistake 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 aaction 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 , nd 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
 | Author of this article/writeup has expressed his/her willingness |
to help or guide users with any technical difficulties he/she faces
while working with the example code environment setting up, running
and resolving any such exception raised during compile or at runtime.
You may ask for any technical doubt or seek technical help related to
this article by using following form to reach for technical help
from the Author for FREE.
This article's Author shall be reading your request and responding
within reasonable time (no resolution timeframe defined as such).
|
| Are you interested in solving a very interesting Technology Stack while Playing this Game 
|
|
| Home >>> Struts Struts 2 Example Tutorial >>> Struts Validation >>> ResourceBundle Entry Corrected |
|
|
Visitor/User referred related external URL:
(Visible upon review and approved by this site Administrator)
|
|
|
|
|
<- requires login | Log in or Register | |
Copyright © 2008-2009, Interview-Questions-Tips-Forum, All Rights Reserved. | CONTACT PRIVACY POLICY DISCLAIMER |
 |
Terms of Use and Disclaimer :
This web site provides some of the information about various technologies, example
code, tips, tutorials etc. Like any printed materials, content of these pages may
become out of date over a period of time. Therefore all visitor/users of this web
site are requested/advised to refer to the originating parties/sources for the
latest changes and happenings for detailed information. This information is not
intended to be a substitute for the original reference provided by the originating
parties/sources.
By accessing and using this website in any ways, including, without
limitation, browsing the website pages, using any information, using any content and/or
downloading any materials, you agree to and are bound by the terms of use
described in this page and Usage Terms and Conditions.
If you do not agree to all of
the terms and conditions contained in the terms of use described in this
page and Usage Terms and Conditions, do not use this
website in any manner. If you are using the website on behalf of your
employer, you represent that you are authorized to accept these Terms of Use
on your employer's behalf.
All Trademarks are property of their respective owner. Appropriate measure is being
taken for providing accurate and up-to-date information but like any printed materials,
these blog(s)/contents may eventually be outdated one day, so if you are using any
of these information, please refer original content/documentation from respective sources.
And under no circumstances shall the Author of these contents and/or this web site
be liable for any loss, damage, expense incurred or suffered which is claimed to have
occurred because of usage of the contents of this web site.
If you have any questions/queries/feedback/suggestions then please write to this web
site owner at contact.
|  |
|
|
|
|
|