• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How do I handle dynamic form elements?

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why not just use a map-based property?
 
David Dunham
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic