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

发布于 2024-08-25 16:50:20 字数 788 浏览 11 评论 0原文

我有以下 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 是否可以实现这一点。每个 selectOneRadioselectOneMenu 组都有一个不同的单独方法。

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

当我提交表单时,我所有的选择都会在 POST 中发送,我只是不知道告诉 Seam 如何将它们分派到我的组件的正确方法。

任何帮助表示赞赏!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ˇ宁静的妩媚 2024-09-01 16:50:20

#{list.choose} 设为由当前迭代类别标识的数组、Collection 或 Map。 Map(其中键代表类别,值代表所选选项)可能是最简单的。

这是一个可以在这里工作的 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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文