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