You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Bart Busschots <ba...@so-4pt.net> on 2006/08/01 20:54:25 UTC

OK ... I'm almost there ..... just not quite yet

OK ... I have a simple two-step for for allowing a user of our system (a 
teacher) to create a group of students in our system. The first step 
asks the teacher to enter some basic information about the group as a 
whole, specifically, the name, the group type (a dropdown), a group 
description (a text area) and the number of students in the group. The 
second step then asks the teacher to enter the names of all the 
students. The obvious thing to note is that the number of text areas on 
the second page is dynamic. So, after some messing around I decided to 
store that information as a list of Strings in the form bean so my form 
looks like this in strutsconfig:

<form-bean name="createStudentGroupForm" 
type="org.apache.struts.validator.DynaValidatorForm">
   <form-property name="groupName" type="java.lang.String" />
   <form-property name="noStudents" type="java.lang.String" />
   <form-property name="groupType" type="java.lang.String" />
   <form-property name="wikiText" type="java.lang.String" />
   *<form-property name="studentNames" type="java.lang.String[]" />*
  </form-bean>

The first form submits to an action that populates the studentNames 
field like so:
 
    //get the dynamic form
    DynaValidatorForm theForm = (DynaValidatorForm)form;

    //get the details from the form
    String groupType = (String)theForm.get("groupType");
    int numStudents = Integer.parseInt(((String)theForm.get("noStudents")));

    //prepare the list of default student names
    String students[] = new String[numStudents];
    for(int i = 0; i < numStudents; i++) {
      students[i] = "Student " + (i + 1);
    }

    //insert into the form
    theForm.set("studentNames", students);

I then try to render the text areas in my JSP like so:


  <html:form action="/mentor/createNamedStudentGroup" 
styleClass="vtie_form" onsubmit="encodeNames()">

  <ul class="form_element_list">
   <jsp:useBean id="createStudentGroupForm" scope="session" 
type="org.apache.struts.validator.DynaValidatorForm" />
   <logic:iterate id="student" name="createStudentGroupForm" 
property="studentNames">
    <li><html:text property="studentNames" indexed="true" /></li>
   </logic:iterate>
  </ul>

I do get out 5 text areas as you would expect but the values are messed 
up. Here is the source generated:

<li><input type="text" name="org.apache.struts.taglib.html.BEAN[0].studentNames" value="[Ljava.lang.String;@1a175b" /></li>
<li><input type="text" name="org.apache.struts.taglib.html.BEAN[1].studentNames" value="[Ljava.lang.String;@1a175b" /></li>
<li><input type="text" name="org.apache.struts.taglib.html.BEAN[2].studentNames" value="[Ljava.lang.String;@1a175b" /></li>
<li><input type="text" name="org.apache.struts.taglib.html.BEAN[3].studentNames" value="[Ljava.lang.String;@1a175b" /></li>
<li><input type="text" name="org.apache.struts.taglib.html.BEAN[4].studentNames" value="[Ljava.lang.String;@1a175b" /></li>

As you can see I seem to be getting close to what I want but I'm not 
there just yet :(

Any pointers of where I've gone wrong would be greatly appreciated.

Thanks,

Bart.

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


Re: OK ... I'm almost there ..... just not quite yet

Posted by kalpesh modi <mo...@yahoo.com>.
You can create a Student object with say FirstName, LastName etc. The form will have a list of Student objects. I usually do with the ActionForm. I havent tried it with DynaActionForm. 

On the first page, when the user enters the number of students (say 5) , create a list of 5 student objects in your action class and forward it to the corresponding JSP. Iterate through the list, to view empty fields. I used nested tag for this. I think you can use the JSTL c:forEach tag also for this.

HTH.
Thanks,
-Kalpesh

 		
---------------------------------
Groups are talking. We&acute;re listening. Check out the handy changes to Yahoo! Groups. 

Re: OK ... I'm almost there ..... just not quite yet

Posted by David Gagnon <dg...@siunik.com>.
It`s been a while since I worked with that .. but I think you missing 
the name attribute in you text tag i.e.:

<html:text name="student" property="studentNames" indexed="true" />


Regards
/David
Bart Busschots wrote:
> OK ... I have a simple two-step for for allowing a user of our system 
> (a teacher) to create a group of students in our system. The first 
> step asks the teacher to enter some basic information about the group 
> as a whole, specifically, the name, the group type (a dropdown), a 
> group description (a text area) and the number of students in the 
> group. The second step then asks the teacher to enter the names of all 
> the students. The obvious thing to note is that the number of text 
> areas on the second page is dynamic. So, after some messing around I 
> decided to store that information as a list of Strings in the form 
> bean so my form looks like this in strutsconfig:
>
> <form-bean name="createStudentGroupForm" 
> type="org.apache.struts.validator.DynaValidatorForm">
>   <form-property name="groupName" type="java.lang.String" />
>   <form-property name="noStudents" type="java.lang.String" />
>   <form-property name="groupType" type="java.lang.String" />
>   <form-property name="wikiText" type="java.lang.String" />
>   *<form-property name="studentNames" type="java.lang.String[]" />*
>  </form-bean>
>
> The first form submits to an action that populates the studentNames 
> field like so:
>
>    //get the dynamic form
>    DynaValidatorForm theForm = (DynaValidatorForm)form;
>
>    //get the details from the form
>    String groupType = (String)theForm.get("groupType");
>    int numStudents = 
> Integer.parseInt(((String)theForm.get("noStudents")));
>
>    //prepare the list of default student names
>    String students[] = new String[numStudents];
>    for(int i = 0; i < numStudents; i++) {
>      students[i] = "Student " + (i + 1);
>    }
>
>    //insert into the form
>    theForm.set("studentNames", students);
>
> I then try to render the text areas in my JSP like so:
>
>
>  <html:form action="/mentor/createNamedStudentGroup" 
> styleClass="vtie_form" onsubmit="encodeNames()">
>
>  <ul class="form_element_list">
>   <jsp:useBean id="createStudentGroupForm" scope="session" 
> type="org.apache.struts.validator.DynaValidatorForm" />
>   <logic:iterate id="student" name="createStudentGroupForm" 
> property="studentNames">
>    <li><html:text property="studentNames" indexed="true" /></li>
>   </logic:iterate>
>  </ul>
>
> I do get out 5 text areas as you would expect but the values are 
> messed up. Here is the source generated:
>
> <li><input type="text" 
> name="org.apache.struts.taglib.html.BEAN[0].studentNames" 
> value="[Ljava.lang.String;@1a175b" /></li>
> <li><input type="text" 
> name="org.apache.struts.taglib.html.BEAN[1].studentNames" 
> value="[Ljava.lang.String;@1a175b" /></li>
> <li><input type="text" 
> name="org.apache.struts.taglib.html.BEAN[2].studentNames" 
> value="[Ljava.lang.String;@1a175b" /></li>
> <li><input type="text" 
> name="org.apache.struts.taglib.html.BEAN[3].studentNames" 
> value="[Ljava.lang.String;@1a175b" /></li>
> <li><input type="text" 
> name="org.apache.struts.taglib.html.BEAN[4].studentNames" 
> value="[Ljava.lang.String;@1a175b" /></li>
>
> As you can see I seem to be getting close to what I want but I'm not 
> there just yet :(
>
> Any pointers of where I've gone wrong would be greatly appreciated.
>
> Thanks,
>
> Bart.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
>
>



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


Re: OK ... I'm almost there ..... just not quite yet [resolved]

Posted by Bart Busschots <ba...@so-4pt.net>.
In case anyone else comes across this thread in the future and was 
wondering what the solution was, it is just a very small change in the 
JSP code. The correct code reads:

  <ul class="form_element_list">
   <jsp:useBean id="createStudentGroupForm" scope="session" 
type="org.apache.struts.validator.DynaValidatorForm" />
   <logic:iterate id="student" name="createStudentGroupForm" 
property="studentNames" indexId="i">
    <li><html:text property="studentNames[${i}]" /></li>
   </logic:iterate>
  </ul>

Now, if I can get validator beaten into shape I have a working 'clean' 
solution to my problem rather than a nasty hack so it's been a 
productive afternoon! Cheers for the various replies this afternoon .... 
they really helped steer me in the right direction.

Bart.

Bart Busschots wrote:
> OK ... I have a simple two-step for for allowing a user of our system 
> (a teacher) to create a group of students in our system. The first 
> step asks the teacher to enter some basic information about the group 
> as a whole, specifically, the name, the group type (a dropdown), a 
> group description (a text area) and the number of students in the 
> group. The second step then asks the teacher to enter the names of all 
> the students. The obvious thing to note is that the number of text 
> areas on the second page is dynamic. So, after some messing around I 
> decided to store that information as a list of Strings in the form 
> bean so my form looks like this in strutsconfig:
>
> <form-bean name="createStudentGroupForm" 
> type="org.apache.struts.validator.DynaValidatorForm">
>   <form-property name="groupName" type="java.lang.String" />
>   <form-property name="noStudents" type="java.lang.String" />
>   <form-property name="groupType" type="java.lang.String" />
>   <form-property name="wikiText" type="java.lang.String" />
>   *<form-property name="studentNames" type="java.lang.String[]" />*
>  </form-bean>
>
> The first form submits to an action that populates the studentNames 
> field like so:
>
>    //get the dynamic form
>    DynaValidatorForm theForm = (DynaValidatorForm)form;
>
>    //get the details from the form
>    String groupType = (String)theForm.get("groupType");
>    int numStudents = 
> Integer.parseInt(((String)theForm.get("noStudents")));
>
>    //prepare the list of default student names
>    String students[] = new String[numStudents];
>    for(int i = 0; i < numStudents; i++) {
>      students[i] = "Student " + (i + 1);
>    }
>
>    //insert into the form
>    theForm.set("studentNames", students);
>
> I then try to render the text areas in my JSP like so:
>
>
>  <html:form action="/mentor/createNamedStudentGroup" 
> styleClass="vtie_form" onsubmit="encodeNames()">
>
>  <ul class="form_element_list">
>   <jsp:useBean id="createStudentGroupForm" scope="session" 
> type="org.apache.struts.validator.DynaValidatorForm" />
>   <logic:iterate id="student" name="createStudentGroupForm" 
> property="studentNames">
>    <li><html:text property="studentNames" indexed="true" /></li>
>   </logic:iterate>
>  </ul>
>
> I do get out 5 text areas as you would expect but the values are 
> messed up. Here is the source generated:
>
> <li><input type="text" 
> name="org.apache.struts.taglib.html.BEAN[0].studentNames" 
> value="[Ljava.lang.String;@1a175b" /></li>
> <li><input type="text" 
> name="org.apache.struts.taglib.html.BEAN[1].studentNames" 
> value="[Ljava.lang.String;@1a175b" /></li>
> <li><input type="text" 
> name="org.apache.struts.taglib.html.BEAN[2].studentNames" 
> value="[Ljava.lang.String;@1a175b" /></li>
> <li><input type="text" 
> name="org.apache.struts.taglib.html.BEAN[3].studentNames" 
> value="[Ljava.lang.String;@1a175b" /></li>
> <li><input type="text" 
> name="org.apache.struts.taglib.html.BEAN[4].studentNames" 
> value="[Ljava.lang.String;@1a175b" /></li>
>
> As you can see I seem to be getting close to what I want but I'm not 
> there just yet :(
>
> Any pointers of where I've gone wrong would be greatly appreciated.
>
> Thanks,
>
> Bart.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>


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