Struts checkbox with ActionForm in request scope

S

sameergn

Hi,

I am trying to render check boxes using an indexed property say
selectedRole[0..n]. Since the Form is request scope, how one should
code the setter method setSelectedRole(int idx), when the form is
submitted. The number of submitted checkboxes are not known and
setSelectedRole(n) can be called in any order.

Is there any better option than keeping the ActionForm in session? With
session scope also, we will end up in same scenario when session
timeout occurs before user submits the page.

We can count #of request parameters starting with "selectedRole[" in
reset() method and then allocate the array, but this looks ugly.

Thanks,
Sameer
 
M

Manish Pandit

Hi,

I am trying to render check boxes using an indexed property say
selectedRole[0..n]. Since the Form is request scope, how one should
code the setter method setSelectedRole(int idx), when the form is
submitted. The number of submitted checkboxes are not known and
setSelectedRole(n) can be called in any order.

Is there any better option than keeping the ActionForm in session? With
session scope also, we will end up in same scenario when session
timeout occurs before user submits the page.

We can count #of request parameters starting with "selectedRole[" in
reset() method and then allocate the array, but this looks ugly.

Thanks,
Sameer


One option is to not use an action form for this, and have the action
(the one to which this form is submitted) do a request.getParameter( ).
Note that this will not work if it is a multipart request. You can name
the checkboxes consistently (like cb0 through cb10) and then in a for
loop, check for "presence" of this parameter. If the checkbox is
checked, it'd be present in the request. This way you can know which
ones were checked, and which ones were unchecked (by looking into the
previous state, maybe in the database).

I hope I understood your question correctly, and this helps.

-cheers,
Manish
 

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,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top