我可以在我的表单中添加动态输入字段:
<h:form>
<h:dataTable id="tblFields" value="#{templateOptionBean.fields}"
var="field">
<h:column>
<h:inputText value="#{field.value}" />
</h:column>
<h:column>
<h:commandButton value="Remove">
<f:ajax
listener="#{templateOptionBean.onButtonRemoveFieldClick(field)}"
immediate="true" render="@form" />
</h:commandButton>
</h:column>
</h:dataTable>
<h:commandButton value="Add">
<f:ajax listener="#{templateOptionBean.onButtonAddFieldClick}"
execute="@form" render="tblFields" />
</h:commandButton>
</h:form>
我的后端是这样的:
@ManagedBean
@ViewScoped
public class TemplateOptions implements Serializable
{
private List<Field> m_lFields;
public TemplateOptions()
{
m_lFields = new ArrayList();
m_lFields.add(new Field());
}
public void setFields(List<Field> p_lFields)
{
m_lFields = p_lFields;
}
public List<Field> getFields()
{
return m_lFields;
}
public void onButtonRemoveFieldClick(final Field p_oField)
{
m_lFields.remove(p_oField);
}
public void onButtonAddFieldClick(AjaxBehaviorEvent p_oEvent)
{
m_lFields.add(new Field());
}
}
但是,当我将表单中的按钮更改为p:commandButton
then 我的逻辑不再起作用。
但是,我想使用这些类型的按钮。因此,我该如何使用这种类型的按钮?
真的很感谢你的回答!!!