Hi,
I'm hoping to get some ideas/technical suggestions on how to handle a task in
struts 2.
Here's a brief overview of the functionality I need.... I have a table with a bunch of choices that should be expressed as a checkbox, and I want to keep track of which choices a User has selected. Modeled in a db, it looks like
Choices
- name
Users
- name
ChoicesUsers
name_id
user_id
selected
where selected is a boolean indicating whether a user has or has not selected the particular item (I've left out some obvious things like id's and so forth, since it's pretty standard... choices and Users are in N:1 relationships with the ChoicesUsers table - ie., many to many through ChoicesUsers)
So, I want to pass an object representing a choice and whether it was selected to a
jsp, where I can display the choice name and put a checkmark if it was previously selected. If the user edits the choices, the form will submit, updates will be made to the db, and a new page will be rendered. So far, standard enough (I think).
One thing I really like about Struts2 is the convention that if I've named something choiceA in a page, I can have a getter/setter named getChoiceA() or setChoiceA(Boolean choice) and not have to deal with the request parameters directly. But unfortunately, I can't (easily) create on the fly methods called getChoiceA() (what do you think this is, rails? ;) )
I've struggled plenty with the checkboxlist tag, and while I've found lots of basic documentation by googling, I haven't been able to get much in the way of more how to tweak it to do something like this. Another thought was to use an iterator, which seems to work...
Though of course this puts me in a situation where I still either need to have a getter/setter for each choice name, or get parameters and parse the names (circumventing the whole get/set conventions that make Struts2 actions so useful, and creating pretty ugly code).
Another possibility would be to create an array of choices, but after hacking around, I wasn't able to get it to work. This would be the best solution - then I could have a getter/setter for an array (of booleans? strings would be easy enough to deal with as well). If anyone knows how to re-write the above so that I could just have a setChoices(ArrayList<Boolean> choices) method (or something similar), that would be really, really helpful.
Thanks to anyone who put in the time to read this far. Hey, if I'm approaching it all wrong and there's an easier way to do this, feel free to post your ideas!