You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by js...@altec.org on 2008/04/02 19:59:22 UTC

Using sets of objects with tags

Perhaps this is the wrong place to seek help, and perhaps there is no
direct solution to my problem, but I have to try.

I am loading a simple set of objects representing users in an action; they
have names, passwords, etc., all loaded from a database.

I am displaying the elements of this set in a .jsp using the iterator tag,
creating a table of user objects with names, passwords, etc. Easy enough.

Next, I use the iterator to create textfield tags instead of property
tags, with the goal of allowing the viewer to edit all of the fields.

My problem is, how do I submit all of these fields? Understand that there
is a varying number of fields, and possibly quite a lot of them, so just
hardcoding matching setters/getters for N uniquely identified DOM tree
elements seems a poor solution.

I know I can just read in the submission stream and parse from there but I
was wondering if there is some elegant struts solution that I am missing.
If necessary I will happily provide illustrative code and links to
clarify.

Sorry again if this is not the place to pose this question, but various
forums have proved to be unresponsive. If this is the wrong place, please
let me know and I will refrain from doing so in the future.

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


Re: Problem with submitting an additional param in a form

Posted by Ryan <gr...@gmail.com>.
I think what you are looking for is:
<s:hidden name="status" value="waiting" />

This creates a hidden input field in the form which then passes the "status"
parameter to the request when the form submits.

On 4/3/08, Peter Theissen <pe...@web.de> wrote:
>
> Hello everbody,
>
> I'm new to Struts2 and also this mailing list.
> I'm programming a user management for a non-profit organisation.
>
> My question is now:
> I have started with the basic struts2 application. In the index.jsp
> there is a form which looks (nearly) like this:
> >>>
>           <s:form action="saveReg" validate="true">
>               <s:param name="status" value="waiting" />
> <s:textfield id="id" name="registration.id" cssStyle="display:none"/>
>               <s:textfield id="firstName" label="First Name"
> name="registration.firstName"/>
>               <s:textfield id="lastName" label="Laste Name"
> name="registration.lastName"/>
>               <s:textfield id="email" label="Email"
> name="registration.email"/>
>               <s:submit theme="ajax" targets="registrations"
> notifyTopics="/save"/>
>           </s:form>
> <<<
> Why cant I append the paramter 'status' and pass it through to the action
> class?
> I read a lot of docs and faqs but didnt find the answer.
> Is it my mistake, a bug or do I need a certain workaround?
> I thought about taking a display:none textfield, but it would make me
> quite unhappy to start the project with dirty tricks and workarounds ;-)
>
> Any help appreciated.
>
> Best regards
> Peter
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Problem with submitting an additional param in a form

Posted by Peter Theissen <pe...@web.de>.
Hello everbody,

I'm new to Struts2 and also this mailing list.
I'm programming a user management for a non-profit organisation.

My question is now:
I have started with the basic struts2 application. In the index.jsp
there is a form which looks (nearly) like this:
 >>>
            <s:form action="saveReg" validate="true">
                <s:param name="status" value="waiting" />   
                <s:textfield id="id" name="registration.id" 
cssStyle="display:none"/>
                <s:textfield id="firstName" label="First Name" 
name="registration.firstName"/>
                <s:textfield id="lastName" label="Laste Name" 
name="registration.lastName"/>
                <s:textfield id="email" label="Email" 
name="registration.email"/>
                <s:submit theme="ajax" targets="registrations" 
notifyTopics="/save"/>
            </s:form>
<<<
Why cant I append the paramter 'status' and pass it through to the 
action class?
I read a lot of docs and faqs but didnt find the answer.
Is it my mistake, a bug or do I need a certain workaround?
I thought about taking a display:none textfield, but it would make me
quite unhappy to start the project with dirty tricks and workarounds ;-)

Any help appreciated.

Best regards
Peter



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


Re: Using sets of objects with tags

Posted by Dave Newton <ne...@yahoo.com>.
--- jstraub@altec.org wrote:
> by using the "setname[id].fieldname" format above, would I simply 
> be able to access the entire set in the following Action by a getter 
> and setter with a like named set (i.e. "setname")?

Yep.

You might want to check out the type conversion docs [1] if you haven't
already. It gives a rough overview of this functionality.

Dave

[1] http://struts.apache.org/2.0.11.1/docs/type-conversion.html



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


Re: Using sets of objects with tags

Posted by js...@altec.org.
> You're not relating the fields to the students collection in any way.
>
> You'd need a collection of students in your action (probably mapped by
> ID),
> and each field would use something like "student[id].firstname" as the
> field
> name.
>
> The code you've provided (below) will just create a bunch of fields named
> "lastName", "firstName", etc. rather than being associated with your
> "students" set.
>
> Dave
>

I was curious about that. The code snippet I provided does actually
properly prefill the text fields from the proper members of the set as
long as the text field name matches the string fields of the Student
object.

However, as you indicated, once filled, there was no connection to the set.

I think I understand now. That being said, by using the
"setname[id].fieldname" format above, would I simply be able to access the
entire set in the following Action by a getter and setter with a like
named set (i.e. "setname")?

--Jared

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


Re: Using sets of objects with tags

Posted by Dave Newton <ne...@yahoo.com>.
You're not relating the fields to the students collection in any way.

You'd need a collection of students in your action (probably mapped by ID),
and each field would use something like "student[id].firstname" as the field
name.

The code you've provided (below) will just create a bunch of fields named
"lastName", "firstName", etc. rather than being associated with your
"students" set.

Dave

--- jstraub@altec.org wrote:
> > You don't need to 'parse the stream', Struts does that for you. Struts
> > will call the setters on the objects in your set to pass the form inputs
> > into them. In other words, the result you want to achieve is what
> > happens automatically, provided you specify your input fields correctly.
> 
> Hmmmm, thought I had tried that. How do I retrieve those objects then?
> 
> For example, in the first Action I create a set named 'students', and fill
> it with Student objects.
> 
> In the .jsp, I iterate over the set of students, like so:
> (please ignore the table, this is just preliminary formatting, .css is on
> the way)
> 
> <s:form action="TheNextOne"
>    <table>
>       <s:iterator status="stat" value="students">
>          <tr>
>             <s:hidden name="userId" />
>             <td><s:property value="#stat.index + 1" />.</td>
>             <td><s:textfield name="lastName" /></td>
>             <td><s:textfield name="firstName" /></td>
>             <td><s:textfield name="userName" /></td>
>             <td><s:textfield name="userPassword" /></td>
>             </tr>
>          </s:iterator>
>       </table>
>       <s:submit/>
>    </s:form>


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


Re: Using sets of objects with tags

Posted by js...@altec.org.
>
> You don't need to 'parse the stream', Struts does that for you. Struts
> will call the setters on the objects in your set to pass the form inputs
> into them. In other words, the result you want to achieve is what
> happens automatically, provided you specify your input fields correctly.
>
> L.
>

Hmmmm, thought I had tried that. How do I retrieve those objects then?

For example, in the first Action I create a set named 'students', and fill
it with Student objects.

In the .jsp, I iterate over the set of students, like so:
(please ignore the table, this is just preliminary formatting, .css is on
the way)

<s:form action="TheNextOne"
   <table>

      <s:iterator status="stat" value="students">

         <tr>

            <s:hidden name="userId" />

            <td><s:property value="#stat.index + 1" />.</td>

            <td><s:textfield name="lastName" /></td>

            <td><s:textfield name="firstName" /></td>

            <td><s:textfield name="userName" /></td>

            <td><s:textfield name="userPassword" /></td>

            </tr>

         </s:iterator>

      </table>
      <s:submit/>
   </s:form>

Is this incorrect? If not, how then do I retrieve the set of modified
student objects in the submit Action?

-Jared

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


Re: Using sets of objects with tags

Posted by Laurie Harper <la...@holoweb.net>.
jstraub@altec.org wrote:
>> Struts 2?
>>
> 
> Sorry, yes I am using Struts 2.
> 
>> Er, what other forums would you have tried?
>>
> 
> Varied coding forums that happened to have a
> struts/hibernate/whatever-else subsection
> 
>> Use map- (and/or) index-based field names. Trivial with S2, don't actually
>> remember for S1.
>>
> 
> I may not understand what you are saying.
> 
> I have a set of varying size that needs to generate modifiable text fields
> for each element in the set. This means that within my Action I simply
> load up a Set/Map and then in the .jsp use the iterator tag to pre-fill
> the text fields. The text fields are inside a form tag.
> 
> If I use index based field names, other than directly parsing the stream,
> is there a way to retrieve the modifications made to the text fields in
> the Action to which I submit the form?

You don't need to 'parse the stream', Struts does that for you. Struts 
will call the setters on the objects in your set to pass the form inputs 
into them. In other words, the result you want to achieve is what 
happens automatically, provided you specify your input fields correctly.

L.


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


Re: Using sets of objects with tags

Posted by js...@altec.org.
>
> Struts 2?
>

Sorry, yes I am using Struts 2.

>
> Er, what other forums would you have tried?
>

Varied coding forums that happened to have a
struts/hibernate/whatever-else subsection

>
> Use map- (and/or) index-based field names. Trivial with S2, don't actually
> remember for S1.
>

I may not understand what you are saying.

I have a set of varying size that needs to generate modifiable text fields
for each element in the set. This means that within my Action I simply
load up a Set/Map and then in the .jsp use the iterator tag to pre-fill
the text fields. The text fields are inside a form tag.

If I use index based field names, other than directly parsing the stream,
is there a way to retrieve the modifications made to the text fields in
the Action to which I submit the form?

If a visual example would help, the site I am being asked to develop can
be found here:

http://www-dev.arcademicskillbuilders.com

Feel free to login as username: test, password: test


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


Re: Using sets of objects with tags

Posted by Dave Newton <ne...@yahoo.com>.
--- jstraub@altec.org wrote:
> I am loading a simple set of objects representing users in an action; they
> have names, passwords, etc., all loaded from a database.

Struts 2?

> Next, I use the iterator to create textfield tags instead of property
> tags, with the goal of allowing the viewer to edit all of the fields.
> 
> My problem is, how do I submit all of these fields? Understand that there
> is a varying number of fields, and possibly quite a lot of them, so just
> hardcoding matching setters/getters for N uniquely identified DOM tree
> elements seems a poor solution.

Use map- (and/or) index-based field names. Trivial with S2, don't actually
remember for S1.

> Sorry again if this is not the place to pose this question, but various
> forums have proved to be unresponsive.

Er, what other forums would you have tried?

Dave


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