代码之家  ›  专栏  ›  技术社区  ›  purecharger

将动态表单的多个提交值映射到单个bean属性

  •  2
  • purecharger  · 技术社区  · 16 年前

    我有以下JSF表单:

    <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>
    

    以及一个名为 list . 变量 cat 注入到组件中,由方法使用 list.getNames() list.choose() 每个广播组都要打电话。我不确定在JSF中这是否可行。每种方法都有一个不同的独立方法 selectOneRadio selectOneMenu 组。

    因为我有一个未知数量的类别,我不能/不想为每个可能的选择定义一个方法。

    感谢您的帮助!

    1 回复  |  直到 8 年前
        1
  •  3
  •   BalusC    8 年前

    制作 #{list.choose} Map<String, String> 其中,键表示类别,值表示所选选项可能是最简单的。

    这里有一个 MCVE

    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;
        }
    
    }
    

    <!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>
    
    推荐文章