aspose file tools*
The moose likes Struts and the fly likes How do I handle dynamic form elements? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How do I handle dynamic form elements?" Watch "How do I handle dynamic form elements?" New topic
Author

How do I handle dynamic form elements?

David Dunham
Greenhorn

Joined: Jan 23, 2009
Posts: 9
I have a form which contains a set of search results, as such they are dynamic:

From the HTML:
<input type="checkbox" name="runner_3650" value="true" id="addrunners_runner_3650"/>
<input type="hidden" name="__checkbox_runner_3650" value="true" /></div> <div id="wwlbl_addrunners_runner_3650" class="wwlbl">
<label for="addrunners_runner_3650" class="checkboxLabel">Jim</label></div>


From the JSP:
<s:checkbox label="%{runnerName}" name="runner_%{id}" />

What I get when I try to run this is an input error. (it wants to use setRunner_3650() when there is no such setter.)

What I am looking for is something along the lines of a way to handle dynamically generated form parameters.
In the past using regular JSPs I've directly gotten the information from the request, but the action doesn't have access to that as far as I can tell, and it gives a input error trying to set the fields.
David Dunham
Greenhorn

Joined: Jan 23, 2009
Posts: 9
I've found out how to get the Request, but I still haven't figured out how to avoid the errors when the action is started.

Maybe I could remove the attributes from the form so when the action happens there is no error there to happen, but I think that I would have to do that in an interceptor.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Why not just use a map-based property?
David Dunham
Greenhorn

Joined: Jan 23, 2009
Posts: 9
I am not sure what you mean by a map-based parameter (unless you mean a multiselect dropdown).

What I am trying to do is something like:
<input type="checkbox" id="addrunner_runner_3650" name="runner_3650"/><label for="addrunner_runner_3650"> Add Jim to your running group</runner>
<ul>
<li>Location: Baltimore</li>
<li>Running status: active</li>
<li><a href="...">Further Info on Jim</a></a>
</ul>

Which doesn't lend itself well to a dropdown.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Not sure what you're talking about with regards to dropdowns, but that aside: if a parameter has a name like foo['2652'] or whatever then it's a map. That lends itself pretty handily to a collection of checkboxes where the name is created from the action property name ("foo") and bracket notation with an ID in it.

The "foo" map would then contain a collection of true/falses keyed by IDs.
David Dunham
Greenhorn

Joined: Jan 23, 2009
Posts: 9
Do you mean something like:
<s:checkbox label="%{runnerName}" name="runner['%{id}']" />

And then in the jsp, and adding a get/set runner functions in the action?

I'm getting this error when I do this:
[http-8090-Processor23] ERROR com.opensymphony.xwork2.interceptor.ParametersInterceptor - ParametersInterceptor - [setParameters]: Unexpected Exception caught setting 'runner['3650']' on 'class xxxx.add.AddRunnersAction: Error setting expression 'runner['3650']' with value 'false'
David Dunham
Greenhorn

Joined: Jan 23, 2009
Posts: 9
OK, I found my error that was causing that. However I still end up getting redirected to the "input" result (actually an exception since I don't have that one mapped in my struts.xml), but in the logfile there is no mention of what the error was other than it got redirected to the "input" result.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Without knowing what you're using for Java code it's hard to help. (I also don't know what version of Struts you're using.)

I have working code with a Map<String, Boolean> that gets filled with true/false based on whether or not the checkbox is checked, so it's probably something relatively straight-forward.
David Dunham
Greenhorn

Joined: Jan 23, 2009
Posts: 9
Struts 2.
I figured out what I was doing wrong by adding the "input" result in the struts.xml. (I had something marked as required that wasn't).

Your solution worked, I can now get the dynamic elements to work.

Thank you.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Good deal.

It can, however, be important to state the actual version of Struts 2 you're using--in addition to some of the major changes between S2.0 and S2.1 there were several non-trivial upgrades in the S2.0.n series. Just for future reference.
 
Consider Paul's rocket mass heater.
 
subject: How do I handle dynamic form elements?