- here's the field definition in the JSP:
<html:form action="/login.do" focus="username">
<logic:messagesPresent>
<font color="red">
<ul>
<html:messages id="message">
<li><bean:write name="message"/></li>
</html:messages>
</ul>
</font>
</logic:messagesPresent>
<table>
<tr>
<th align="right">Username:</th>
<td align="left"><html:text property="username" maxlength="12"/></td>
</tr>
</table>
<html:submit/>
</html:form>
- here's the relevant portion of the struts-config.xml file:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation/DTD
Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<form-beans>
<form-bean name="loginForm"
type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="username"
type="java.lang.String"/>
</form-bean>
</form-beans>
<global-forwards>
<forward name="Login" path="/login.jsp"/>
</global-forwards>
<action-mappings>
<action name="loginForm"
path="/login"
type="com.yourcompany.LoginAction"
scope="request"
validate="true"
input="/login.jsp"/>
</action-mappings>
<message-resources parameter="ApplicationResources"
null="false"/>
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validator.xml"/>
</plug-in>
</struts-config>
- copy the validator-rules.xml to your application WEB-INF and cut and
paste the error messages from it into a file called
WEB-INF/classes/ApplicationResources.properties
- now create the validator.xml file in the WEB-INF directory:
<!DOCTYPE form-validation PUBLIC "-//Apache Software Foundation//DTD
Commons Validator Rules Configuration 1.0//EN"
"http://jakarta.apache.org/commons/dtds/validator_1_0.dtd">
<form-validation>
<formset>
<form name="loginForm">
<field property="username"
depends="required,minlength">
<var>
<var-name>minLength</var-name>
<var-value>2</var-value>
</var>
<arg0 name="required" key="username"
resource="false"/>
<arg0 name="minlength" key="username"
resource="false"/>
<arg1 name="minlength" key="2"
resource="false"/>
</field>
</form>
</formset>
</form-validation>
- remember that validation is loaded at system startup so your web.xml
file should look something like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web
Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app id="TRS">
<servlet>
<servlet-name>TRS</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>TRS</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<taglib>
<taglib-uri>bean</taglib-uri>
<taglib-location>/WEB-INF/lib/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>html</taglib-uri>
<taglib-location>/WEB-INF/lib/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>logic</taglib-uri>
<taglib-location>/WEB-INF/lib/struts-logic.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>tiles</taglib-uri>
<taglib-location>/WEB-INF/lib/struts-tiles.tld</taglib-location>
</taglib>
</web-app>
Some of my observations:
- The field validation contains some redundant information, i.e. I
still have to specify arg0 and arg1 even after having defined the
minLength as 2 previously. Note also the case of the validation:
all lower case in the depends (got caught on that one!) but
mixed-case in the var tag.
- The <html:errors/> tag tries to put everything on one line,
instead of each on a separate line which is why I went to using
<html:messages>
- While testing, you definitely need a separate instance of Tomcat:
you'll probably find yourself restarting it frequently. That's
because every time you make even a slight change in the
validator.xml file you have to restart. HINT: If your changes
don't seem to be taking effect, restart Tomcat!
- Someone was complaining about the handling of the float type,
and it does permit the inclusion of certain alpha characters,
namely f, d, F and D at the end of the string. This is not
aberrant behaviour: check the javadocs for java.lang.Float.
- Finally, I couldn't get the DynaValidatorActionForm to work using
the supplied directions, i.e. using the form property attribute
in the validator.xml file. Turns out it does work if you specify
name="<the contents of the 'path' in the action element>", i.e.
change the above example to [struts-config.xml]:
<form-beans>
<form-bean name="loginForm"
type="org.apache.struts.validator.DynaValidatorActionForm">
and [validator.xml]:
<form name="/login">
But at the end of the day it does actually work. I've only included
two checks in this example but it still demonstrates some of the
"gotcha!"s for you to avoid.
And I got caught out again! It turns out that the minlength in the var
element is in all lowercase, not as described in the book (which I
won't mention). Once you finally get it figured out, however, it's a
pretty capable framework. If you want to display all the errors in a
single block then you can put something like this in your properties
file:
errors.header=<h3>The following errors were detected:</h3><ul>
errors.prefix=<li>
errors.suffix=<br>
errors.footer=</ul>
and use <html:errors/> in your JSP. If you want to associate errors
with the field which caused them then use something like this:
errors.prefix=<font size="-1" color="red">
errors.suffix=</font>
and use <html:errors property="FIELDNAME"/> where FIELDNAME is as
defined in the JSP and mapped in the validate.xml file. If you're
using a table (common for forms) then you can create table elements
either above, below or to the right of the offending fields and
populate appropriately with the <html:errors> tag. I just wish it
didn't take so long to wade through all this stuff. I swear that I
was flipping through that book, back and forth for ages. Tantalizing
hints buried in examples with no explanation. I ended up resorting
to viewing the tlds and DTDs to get the syntax right, which is not
the way it should be.
Anyway, my efforts will not have been in vain if this saves anyone
time in making things work.
Thanks for the tips. I'm also working on this matter and I've got one
comment and one question :
- Use Struts Console. It definilty helps.
http://www.jamesholmes.com/struts/console/index.html
- Do you know the difference between a DynaValidatorForm and a
DynaValidatorActionForm???? The second extends the first, but they've
got exactly the same Javadoc (and I'm reading the current javadoc
straight from CVS)............. So for the moment I'm using the first
one, but maybe I'm missing something??
--
Julien Dubois.
The difference is how they get mapped. Go to your validator.xml file
and
check out the name attribute of the form tag. If you're using
DynaValidatorForm then that name should match the name attribute of
one
of your form-bean tags.
If you're using DynaValidatorActionForm then it should match the path
attribution of an action in the action-mappings section.
Example of using DynaValidatorForm:
[validator.xml]
<!DOCTYPE form-validation PUBLIC "-//Apache Software Foundation//DTD
Commons Validator Rules Configuration 1.0//EN"
"http://jakarta.apache.org/commons/dtds/validator_1_0.dtd">
<form-validation>
<formset>
<form name="loginForm">
.....
[struts-config.xml]
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation/DTD
Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<form-beans>
<form-bean name="loginForm"
type="org.apache.struts.validator.DynaValidatorForm">
.....
When using DynaValidatorActionForm:
[validator.xml]
<!DOCTYPE form-validation PUBLIC "-//Apache Software Foundation//DTD
Commons Validator Rules Configuration 1.0//EN"
"http://jakarta.apache.org/commons/dtds/validator_1_0.dtd">
<form-validation>
<formset>
<form name="/login">
.....
[struts-config.xml]
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation/DTD
Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<form-beans>
<form-bean name="loginForm"
type="org.apache.struts.validator.DynaValidatorActionForm">
<form-property name="username"
type="java.lang.String"/>
</form-bean>
</form-beans>
<action-mappings>
<action name="loginForm"
path="/login"
.....
So in the first case it's looking for a form-bean with a name
attribute
of loginForm. In the second it's looking for an action with a path of
/login. As I read it, the idea was that multiple actions could
conceivably
map to the same form and so you'd have more granularity if you could
validate based on the action rather than the form.
Trying to dig this out of the available documentation, well...
Thanks Sudsy for your advice.
However I keep getting this error when trying to run my struts
application using the validator.
javax.servlet.ServletException: Exception creating bean of class
RegisterForm: {1}
at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:471)
at org.apache.jsp.register$jsp._jspService(register$jsp.java:225)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:107)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:201)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:381)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:473)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain
Is this a configuration problem.
Can you help?
Cheers.
bitbu...@hotmail.com (Sudsy) wrote in message news:<3c836f4e.0301...@posting.google.com>...