You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Stéphane Zuckerman <st...@gmail.com> on 2005/04/07 11:05:57 UTC

LazyLists and dynamic forms

Hello,

I've read carefully the Struts' wiki page regarding indexed properties 
and how to deal with them... but I failed to make their examples work 
with LazyDynaBeans ...

Could someone put me back on track please ? I've managed to use 
LazyLists in classical ActionForms, and if there is no other choice I 
will continue that way, but since I've got all my other ActionForms in a 
  dynamic way, I'd rather stay coherent with my project...

Thanks

-- 
Stéphane Zuckerman

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: LazyLists and dynamic forms

Posted by Stéphane Zuckerman <st...@gmail.com>.
Hello, and sorry for the late answer...

Niall Pemberton a écrit :
> What you have looks OK to me - except in the Action the reference to
> DynaValidatorForm - looks like your referencing the wrong class to me - 

You're right, I just mistyped it, but it is a BeanValidatorForm.

> the
> actual ActionForm flavour should be a BeanValidatorForm - but IMO its better
> to just cast it to a DynaBean, then your not fixed on an implementation.
> 
> Something like...
> 
> public final class DynaLazyAction extends Action {
> 
>     public ActionForward execute(...) {
> 
>         DynaBean dynaForm = (DynaBean) form;
>         // here is the line that makes it all crumble...
>         Person[] persons = (Person[]) dynaForm.get("persons");
> 
>         request.setAttribute("persons",persons);
>         return mapping.findForward("ok");
>     }
> }
> 
> Also when you say "here is the line that makes it all crumble" - what does
> that mean? Presumably your getting an Exception thrown - if so, what is it?


The exception is a ClassCastException ... Which is really strange, since 
the bean is supposed to store exactly the object I'm trying to retrieve ...

Thanks for your answers ! :-)

-- 
Stéphane Zuckerman

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: LazyLists and dynamic forms

Posted by Niall Pemberton <ni...@blueyonder.co.uk>.
What you have looks OK to me - except in the Action the reference to
DynaValidatorForm - looks like your referencing the wrong class to me - the
actual ActionForm flavour should be a BeanValidatorForm - but IMO its better
to just cast it to a DynaBean, then your not fixed on an implementation.

Something like...

public final class DynaLazyAction extends Action {

    public ActionForward execute(...) {

        DynaBean dynaForm = (DynaBean) form;
        // here is the line that makes it all crumble...
        Person[] persons = (Person[]) dynaForm.get("persons");

        request.setAttribute("persons",persons);
        return mapping.findForward("ok");
    }
}

Also when you say "here is the line that makes it all crumble" - what does
that mean? Presumably your getting an Exception thrown - if so, what is it?

Niall

----- Original Message ----- 
From: "Stéphane Zuckerman" <st...@gmail.com>
Sent: Thursday, April 07, 2005 5:35 PM

>
> ... And my Action class is as simple as the JSP :
>
> public final class DynaLazyAction extends Action {
> public ActionForward execute(...) {
> DynaValidatorForm dynaForm = (DynaValidatorForm) form;
> // here is the line that makes it all crumble...
> Person[] persons = (Person[]) dynaForm.get("persons");
>
> request.setAttribute("persons",persons);
> return mapping.findForward("ok");
> }
> }
>
> As I said before, when it is about "manual" forms, everything's fine, I
> use LazyLists, and it works (thank you so much, Rick; you made my
> morning wonderful :-) ).
>
> I hope I wasn't too messy in my explanations ...
>
> Thanks for any clue you might give me :-)



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: LazyLists and dynamic forms

Posted by Stéphane Zuckerman <st...@gmail.com>.
Niall Pemberton a écrit :
> Its hard to help when all you say is "failed to make their examples work".
> Posting snippets of relevant bits of your struts-config.xml, jsp etc would
> help along with what actually happened.

Sorry for the lack of details, I know I was really too vague.

I am using Struts 1.2.4 .

So, here is what I want to do :
I have a list of text fields which number is unknown when the user gets 
to the JSP. Fields are added dynamically through JavaScript.

Now, I know there is Struts 1.2.6 on its way, but I can't wait for it to 
get a "stable" label.

So here is the example I was trying to follow (extracted from 
http://wiki.apache.org/struts/StrutsCatalogLazyList) :

[quote]
In fact using Arrays (rather than Lists) a LazyDynaBean can be used 
directly, in the following way:

   <form-beans>

      <form-bean name="skillForm"
		type="org.apache.commons.beanutils.LazyDynaBean">
         <form-property name="skills" type="myPackage.SkillBean[]"/>
      </form-bean>

   </form-beans>
[/quote]

Supposedly, the LazyDynaBean object should be wrapped within a 
BeanValidatorForm one.

Here is what I am using in my struts-config.xml file :

       <form-bean name="dynaLazyForm"
		 type="org.apache.commons.beanutils.LazyDynaBean">
         <form-property name="nb" type="java.lang.String" />
         <form-property  name="persons" 	
			type="net.beuarh.lasher.Person[]"/>
       </form-bean>

The class Person is really just a bean, with deux properties : name and 
fname.

Here is the JavaScript code I use to generate new text fields :

function foo() {
	var nb = document.getElementById('nb').value;
	var f = document.getElementById('f');
	f.innerHTML = null;
	f.innerHTML = "nb: "+
		"<input type='text' id='nb' value='"+nb+"' /><br />";
	for (var i = 0; i < nb; i++) {
		f.innerHTML += "name: <input type='text' " +
		      "name='persons["+i+"].name' id='name' /><br />";
		f.innerHTML += "first name:<input type='text' " +
		      "name='persons["+i+"].fname' id='fname' /><br />";
	}
	f.innerHTML += "<input type='submit' value='submit' />";
}

The JSP is really basic (since this is a test) :

<html>
   <head>
     <html:base/>
     <title>JSP for lazyForm form</title>
     <script src="js.js" language="javascript"
             type="text/javascript"></script>
   </head>
   <body>
     <html:errors/>
     <html:form action="/dynaLazy" styleId="f">
       nb : <html:text property="nb" onblur="foo()" styleId="nb"/>
       <br />
       <html:submit/>
     </html:form>
   </body>
</html>

... And my Action class is as simple as the JSP :

public final class DynaLazyAction extends Action {
	public ActionForward execute(...) {
		DynaValidatorForm dynaForm = (DynaValidatorForm) form;
		// here is the line that makes it all crumble...
		Person[] persons = (Person[]) dynaForm.get("persons");

		request.setAttribute("persons",persons);
		return mapping.findForward("ok");
	}
}

As I said before, when it is about "manual" forms, everything's fine, I 
use LazyLists, and it works (thank you so much, Rick; you made my 
morning wonderful :-) ).

I hope I wasn't too messy in my explanations ...

Thanks for any clue you might give me :-)

-- 
Stéphane Zuckerman

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: LazyLists and dynamic forms

Posted by Rick Reumann <st...@reumann.net>.
Stéphane, you got Niall's attention and he is THE MAN, when it comes to 
this. So don't blow it! he he. Yes, give some specific examples of what 
you are trying to accomplish (also give some explanation of the business 
requirement - often times we assume things need to be done a certain way 
when there is often a better way to handle things.)

Niall Pemberton wrote the following on 4/7/2005 9:07 AM:
> Its hard to help when all you say is "failed to make their examples work".
> Posting snippets of relevant bits of your struts-config.xml, jsp etc would
> help along with what actually happened.
> 
> Niall
> 
> ----- Original Message ----- 
> From: "Stéphane Zuckerman" <st...@gmail.com>
> Sent: Thursday, April 07, 2005 10:05 AM
> 
> 
> 
>>Hello,
>>
>>I've read carefully the Struts' wiki page regarding indexed properties
>>and how to deal with them... but I failed to make their examples work
>>with LazyDynaBeans ...
>>
>>Could someone put me back on track please ? I've managed to use
>>LazyLists in classical ActionForms, and if there is no other choice I
>>will continue that way, but since I've got all my other ActionForms in a
>>  dynamic way, I'd rather stay coherent with my project...
> 
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 


-- 
Rick

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: LazyLists and dynamic forms

Posted by Niall Pemberton <ni...@blueyonder.co.uk>.
Its hard to help when all you say is "failed to make their examples work".
Posting snippets of relevant bits of your struts-config.xml, jsp etc would
help along with what actually happened.

Niall

----- Original Message ----- 
From: "Stéphane Zuckerman" <st...@gmail.com>
Sent: Thursday, April 07, 2005 10:05 AM


> Hello,
>
> I've read carefully the Struts' wiki page regarding indexed properties
> and how to deal with them... but I failed to make their examples work
> with LazyDynaBeans ...
>
> Could someone put me back on track please ? I've managed to use
> LazyLists in classical ActionForms, and if there is no other choice I
> will continue that way, but since I've got all my other ActionForms in a
>   dynamic way, I'd rather stay coherent with my project...



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org