0

I have a form with around 90 items. Is there a way to collect the generated values without having that many setters on the server side? can't I parse a list/array/object that I generate with JavaScript? It would help me a lot.

Many thanks, Martijn

jmj
  • 237,923
  • 42
  • 401
  • 438
Skiaddict
  • 48
  • 6
  • Setters getters runs on server and JS on client *can't I parse a list/array/object that I generate with JavaScrip t?* , please make it clear – jmj Apr 13 '11 at 10:01
  • Ah well, it is quite easy. When there is a web form, I can fill that one in. When I press submit, then I want to have the results to be parsed to the server. But I do not want to have 90 getters/setters in my bean when this is not needed. When this can be solved easily in JS with some objects/arrays/whatever and your smartness, it would be very optimal. – Skiaddict Apr 14 '11 at 11:01

1 Answers1

0

Create a Map property.

@ManagedBean
@ViewScoped
public class Bean {

    private Map<String, String> selectedItems = new HashMap<String, String>();

    public Map<String, String> getSelectedItems() {
        return selectedItems;
    }

    // ...
}

Which can be used as

<h:selectOneMenu value="#{bean.selectedItems.one}">
    ...
</h:selectOneMenu>

or

<h:selectOneMenu value="#{bean.selectedItems['one']}">
    ...
</h:selectOneMenu>

Here one becomes the map key and the selected item becomes the map value.

(yes, no setter is required!)


Update an alternative is to have a <h:dataTable> with a <h:selectOneMenu> in the column. This way you can just use a single List<Item> getter (no one setter is required). See also the answer on this question for an example.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I dn't think that was the question. – JSS Apr 13 '11 at 12:26
  • @JSS: I don't think that you know JSF very well. – BalusC Apr 13 '11 at 12:28
  • @BC: That was very harsh comment. Ok I dnt need to tell abt my expert level and not being very personal and respect to your rating. You really need to read & understand the question. – JSS Apr 13 '11 at 12:30
  • @JSS Yours was as harsh as well. You didn't explain what you think the OP was actually asking and you are insinuating that my answer is wrong. – BalusC Apr 13 '11 at 12:37
  • @BC- my apologies if you felt that way (had no intention). I think OP asking to collect all values from the form in one go and retrieve in a single variable on the server side. Either comma separated or other delimiter. This is ofcource possible either by collecting through javascript on client side or using FacesContext on server (Which ofcource very bad practise). Well this is what I'm thinking and OS is the best person to confirm. – JSS Apr 13 '11 at 13:08
  • @JSS: he was just asking if that is *an option* (which thus isn't, it's an extremely poor practice as well). – BalusC Apr 13 '11 at 13:09
  • @BC & @JSS: oh my, no virtual war in my topics :) And as it is for my JSF skills, I'm a total n00b. At this moment I still count my hours with JSF in hours. Thank you for your answers anyway! – Skiaddict Apr 14 '11 at 11:06