Dynamic Form Beans and Validation in Struts

D

D. Mantelos

1. In my struts-config.xml I use a form bean of type DynaValidatorForm
with some properties.
2. In the validator.xml I defined these fields as required.
3. I DO NOT WANT TO WRITE ANY ACTIONFORM CLASS

Is it possible to define dynamic Form beans in the struts-config.xml
without creating explicitly a Form class and having automatically
validation defined in the validation.xml?

BOTH is required:
1. Avoid creating a Form class (only definition of DynaValidatorForm
form-bean in the struts-config.xml)
AND
2. Autmatic validation via validation.xml

Is this possible?

TIA
 
W

Wendy S

D. Mantelos said:
Is it possible to define dynamic Form beans in the struts-config.xml
without creating explicitly a Form class and having automatically
validation defined in the validation.xml?

You posted the same thing on the 13th. Posting it again isn't going to
change the answer.

You seem to misunderstand the purpose of dynamic form beans. They do NOT
magically make it so you never have to write a form class again. Under
certain conditions, you can get away with only the definition in
struts-config.xml. But in some situations you WILL need a form bean. The
one that comes most immediately to mind is a session-scoped form with a
checkbox, in which case you need to implement the reset method or you'll
never be able to "un-check" the checkbox.

What is your objection to writing Form classes? Maybe there is another way
to solve your problem. If you would state the problem, you might get more
help.
 
D

D. Mantelos

Wendy S said:
You posted the same thing on the 13th. Posting it again isn't going to
change the answer.

You seem to misunderstand the purpose of dynamic form beans. They do NOT
magically make it so you never have to write a form class again. Under
certain conditions, you can get away with only the definition in
struts-config.xml. But in some situations you WILL need a form bean. The
one that comes most immediately to mind is a session-scoped form with a
checkbox, in which case you need to implement the reset method or you'll
never be able to "un-check" the checkbox.

What is your objection to writing Form classes? Maybe there is another way
to solve your problem. If you would state the problem, you might get more
help.

I am interested mainly in getting the validation.xml to be applied for
required fields of Dynamic Form Beans which I have define in the
struts-config.xml. This does not work. But for example, a simple login
page with the two required fields may be a dyn. form bean. How can I
let the validation automatically be done by the validation.xml.
OK: My example:

VALIDATION.XML:
---------------
<form-validation>

<formset>
<form name="/Login">
<field property="userName" depends="required"><arg0
key="errors.parameterRequired"/></field>
<field property="userPassword" depends="required"><arg0
key="errors.parameterRequired"/></field>
</form>
</formset>

</form-validation>



STRUTS-CONFIG.XML:
------------------
....
<form-bean name="userFormBean"
type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="userName" type="java.lang.String"/>
<form-property name="userPassword"
type="java.lang.String"/>
<form-property name="userID" type="java.lang.String"/>
<form-property name="doAction" type="java.lang.String"/>
</form-bean>
....



<action path="/Login"
type="com.eurodyn.medusaTypicalProject.actions.LoginAction"
name="userFormBean"
scope="request" validate="true" input="/login.jsp">
<forward name="success"
path="/WEB-INF/jsp/index.jsp?doAction=home"/>
</action>

LOGIN.JSP:

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=utf-8">
<META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
<title>MEDUSA-B Typical Project - <bean:message
key="login.title"/></title>
</head>
<body>
<bean:message key="login.title"/><br><br>
<html:form action="Login">
<html:hidden property="doAction" value="login"/>
<logic:messagesPresent><html:errors/><br><br></logic:messagesPresent>
<bean:message key="login.username"/>:&nbsp;<html:text
property="userName"/><br>
<bean:message key="login.password"/>:&nbsp;<html:text
property="userPassword"/><br>
<br>
<html:submit><bean:message
key="login.button.login"/></html:submit>
</html:form>
</body>
</html:html>
 
G

Gerald Hubmaier

D. Mantelos said:
VALIDATION.XML:
---------------
<form-validation>

<formset>
<form name="/Login">
^^^^^^^^^
This is wrong, you have to use the name of the bean (userFormBean) here.

HTH, Gerald
 
D

D. Mantelos

Gerald Hubmaier said:
^^^^^^^^^
This is wrong, you have to use the name of the bean (userFormBean) here.

HTH, Gerald

Correct. I replaced it with
<form name="userFormBean">
but it does not work. The error msg is:
8:45:21,296 DEBUG RequestProcessor:787 - Populating bean properties
from this request
18:45:21,296 DEBUG RequestProcessor:919 - Validating input form
properties
18:45:21,296 ERROR DynaValidatorForm:114 -
org.apache.struts.validator.FieldChecks.validateRequired(java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionErrors,
javax.servlet.http.HttpServletRequest)
org.apache.commons.validator.ValidatorException:
org.apache.struts.validator.FieldChecks.validateRequired(java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionErrors,
javax.servlet.http.HttpServletRequest)
at org.apache.commons.validator.ValidatorAction.loadValidationMethod(ValidatorAction.java:627)
at org.apache.commons.validator.ValidatorAction.executeValidationMethod(ValidatorAction.java:557)
at org.apache.commons.validator.Field.validateForRule(Field.java:811)
at org.apache.commons.validator.Field.validate(Field.java:890)
at org.apache.commons.validator.Form.validate(Form.java:174)
at org.apache.commons.validator.Validator.validate(Validator.java:367)
at org.apache.struts.validator.DynaValidatorForm.validate(DynaValidatorForm.java:112)
at org.apache.struts.action.RequestProcessor.processValidate(RequestProcessor.java:921)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:206)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:415)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at com.eurodyn.opendaysbase.httpsessionutils.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:35)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:186)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:362)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:186)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
at java.lang.Thread.run(Thread.java:534)
18:45:21,296 DEBUG RequestProcessor:924 - No errors detected,
accepting input


So the required check is not performed. What can I do?
 
W

Wendy Smoak

D. Mantelos said:
but it does not work. The error msg is:
8:45:21,296 DEBUG RequestProcessor:787 - Populating bean properties
from this request
18:45:21,296 DEBUG RequestProcessor:919 - Validating input form
properties
18:45:21,296 ERROR DynaValidatorForm:114 -
org.apache.struts.validator.FieldChecks.validateRequired(java.lang.Object,
....

What version of Struts are you using?

What does the log look like when you start up your app? You should see
Validator setting up the form and validation rules.

I'm doing the exact same thing-- a dynamic login form with required userID
and password fields, and it works fine for me.

In no particular order:
- Move to the latest version of Struts and see if the problem goes away
- run it in a debugger (I use JSwat) and trace the code.
- Check Bugzilla to see if there is a known problem
- strip your project down to the smallest example that demonstrates the
problem, and post a .zip so that interested parties can try it out.
 
G

Gerald Hubmaier

D. Mantelos said:
So the required check is not performed. What can I do?
Do you include the Validator-Plugin in your struts-config.xml?

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

D. Mantelos

I solve it as follows:
In the validation.xml I replaced the word "ActionErrors" with
"ActionMessages", because the validateRequired method needs an
argument of type ActionMessages and I used ActionErrors, because I
used an old XML file.

Thanks for support, Saki
 
D

D. Mantelos

Gerald Hubmaier said:
^^^^^^^^^
This is wrong, you have to use the name of the bean (userFormBean) here.

HTH, Gerald

Gerard,
search for "dynavalidatoractionform" and you will find that you can
also use the action path mapping instead of the form bean name. In my
case it was wrong. I could correct with replacing DynaValidatorForm
with DynaValidatorActionForm.

Saki
 
Joined
May 3, 2007
Messages
2
Reaction score
0
Refer to this link for the simple & correct solution:
http://spteam-lists.blogspot.com/2007/04/re-struts-12-validation-not-working.html

Details here for reference:
This error occurs when the configuration for a vaidation method
doesn't match up with the actual method signature. So in your case the
FieldChecks's validateRequired() method has a different method
signature from what is configured in the methodParams element for the
required validator in validator-rules.xml. As I said before, typically
this occurs when people ugrade to a newer version of Struts and forget
to upgrade the validation-rules.xml - but whatever the cause the
solution is the same - ensure you have the correct
validation-rules.xml for the version of Struts you're using.

Saying "My IDE is 'MyEclipse' which uses a Struts 1.2 release." is
vague - you need to determine which 1.2 version of Struts that is
(There are four "GA" quality releases of Struts 1.2 - they are 1.2.4,
1.2.7, 1.2.8 and 1.2.9). The MANIFEST.MF file in the struts jar should
tell you what version of struts you're using - when you find that out
download that version of Struts and grab the validator-rules.xml from
it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top