Map multiple submitted values of dynamic form to single bean property

試著忘記壹切 提交于 2019-12-11 00:16:18

问题


I've got the following JSF form:

<h:form>
    <ui:repeat value="#{list.categories}" var="cat">
        <h:selectOneRadio id="sel1Rad" value="#{list.choose}" layout="pageDirection">
            <f:selectItems value="#{list.names}"/>
        </h:selectOneRadio> 
    </ui:repeat>
    <h:commandButton id="submit" action="#{list.submit}" value="Submit"/>
</h:form>

And a component named list. The variable cat is injected to the component, used by the method list.getNames(). What I am trying to have happen is to have list.choose() be called for each radio group. I'm not sure if this is possible with JSF. There is a distinct separate method for each selectOneRadio or selectOneMenu group.

Since I have an unknown number of categories, I can't / don't want to define a method for each possible choice.

When I submit the form, all my choices are sent in the POST, I just don't know the correct way to tell Seam how to dispatch them to my component.

Any help is appreciated!


回答1:


Make #{list.choose} an array, Collection or Map which is identified by currently iterated category. A Map<String, String> wherein the key represents the category and the value represents the selected option is probably the easiest.

Here's a MCVE which works right here.

package com.stackoverflow.q2493671;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.enterprise.context.RequestScoped;
import javax.faces.model.SelectItem;
import javax.inject.Named;

@Named
@RequestScoped
public class Bean {

    private List<String> categories;
    private List<String> selectItems;
    private Map<String, String> selectedItemsByCategory = new HashMap<>();

    @PostConstruct
    public void init() {
        categories = Arrays.asList("cat1", "cat2", "cat3");
        selectItems = Arrays.asList("item1", "item2", "item3");
    }

    public void submit() {
        for (Entry<String, String> entry : selectedItemsByCategory.entrySet()) {
            String category = entry.getKey();
            String selectedItem = entry.getValue();
            System.out.println(category + "=" + selectedItem);
        }
    }

    public List<String> getCategories() {
        return categories;
    }

    public List<String> getSelectItems() {
        return selectItems;
    }

    public Map<String, String> getSelectedItemsByCategory() {
        return selectedItemsByCategory;
    }

}

in combination with

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" 
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
>
    <h:head>
        <title>SO question 2493671</title>
    </h:head>
    <h:body>
        <h:form>
            <ui:repeat value="#{bean.categories}" var="category">
                <h:selectOneRadio value="#{bean.selectedItemsByCategory[category]}" layout="pageDirection">
                    <f:selectItems value="#{bean.selectItems}" />
                </h:selectOneRadio>
            </ui:repeat>
            <h:commandButton value="submit" action="#{bean.submit}" />
        </h:form>
    </h:body>
</html>


来源:https://stackoverflow.com/questions/2493671/map-multiple-submitted-values-of-dynamic-form-to-single-bean-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!