将多个提交的动态表单值映射到单个bean属性 [英] Map multiple submitted values of dynamic form to single bean property
问题描述
我有以下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
组都有不同的单独方法.
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.
提交表单时,我所有的选择都在POST中发送,我只是不知道告诉Seam如何将它们分配给我的组件的正确方法.
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.
感谢您的帮助!
推荐答案
使#{list.choose}
为数组,Collection或Map,该数组,Collection或Map由当前迭代的类别标识. Map<String, String>
其中的键代表类别,值代表所选的选项,可能是最简单的.
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.
这是一个 MCVE ,可在此处使用.
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;
}
}
结合
<!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>
这篇关于将多个提交的动态表单值映射到单个bean属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!