我正在使用 Primefaces 向导组件。在一个选项卡上,我根据以前的选项卡输入(用户类型)动态创建输入框。输入框文本标签来自一个列表。在我的支持 bean 中,我有一个映射,其中包含作为键的输入标签和作为值的输入框输入。
单击下一步,我希望使用用户输入更新 map (值)(对应于键)
<c:forEach items="#{gdsiGeodataBean.actionCommand.fields}" var="reqs">
<h:outputLabel for="#{reqs.name}" value="#{reqs.name}:* " />
<pou:inputText value="#{gdsiGeodataBean.actionCommand.values['reqs.name']}" required="true" requiredMessage="Input is required."/>
</c:forEach>
我的支持 bean :
private List<RequiredParam> fields; // +getter (no setter required)
private Map<String, String> values; // +getter (no setter required)
public CommandAction(String actionName, String actionParams, String context) {
this.actionName = actionName;
this.actionParams = actionParams;
this.contextName = context;
//Set up parameters
getRequiredParams();
getOptionalParams();
fields = getFields();
values = new HashMap<String, String>();
}
基本上我想要的是使用来自文本输入框的用户输入来更新 map 值。
最佳答案
您将输入值绑定(bind)到 map 的方法并不完全正确。
<pou:inputText value="#{gdsiGeodataBean.actionCommand.values['reqs.name']}" required="true" requiredMessage="Input is required."/>
您正在指定 固定 映射键而不是基于当前迭代的动态映射键
#{reqs}
.这样,所有提交的值都将在一个相同的固定映射键 "reqs.name"
中结束。 ,其中每个都相互覆盖,因此您只能获得 map 中最后一个字段的值。您需要删除这些单引号以使其成为真正的动态键。
<pou:inputText value="#{gdsiGeodataBean.actionCommand.values[reqs.name]}" required="true" requiredMessage="Input is required."/>
无关 对于具体问题,即使这种方法在您的问题中按原样使用时也会起作用,
<c:forEach>
在某些情况下会失败。例如。在复合组件或迭代 JSF 组件中使用时。宁可使用 <ui:repeat>
反而。另见 JSTL in JSF2 Facelets... makes sense?
关于jsf - 使用向导时,如何将输入框值绑定(bind)到支持 bean 属性中的映射值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8726280/