How to dynamically build a back bean edit form

自古美人都是妖i 提交于 2019-12-03 08:48:51

Bind it to a Map<String, Object> property and use the brace notation [] for the dynamic map key.

E.g.

private List<Field> fields; // +getter (no setter required)
private Map<String, Object> values; // +getter (no setter required)

public UserBean() {
    fields = populateItSomehow();
    values = new HashMap<String, Object>();
}

// ...

with

<h:form>
    <c:forEach items="#{userBean.fields}" var="field">
        <h:inputText value="#{userBean.values[field.name]}" />                       
    </c:forEach> 
    <h:commandButton value="Login" action="#{userBean.loginAction}" />          
</h:form>

The field name becomes the map key and the field value becomes the map value.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!