如何动态构建Back bean编辑表单 [英] How to dynamically build a back bean edit form

查看:93
本文介绍了如何动态构建Back bean编辑表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要构建一个动态放置inputText字段的表单,我正在使用以下代码:

I need to build a form dynamically putting inputText field, I'm using this code:

<h:form>
    <c:forEach items="#{userBean.getFieldList()}"  var="field">
        <h:inputText value="#{userBean.getFieldValue(field.name)}" />                       
    </c:forEach> 
    <h:commandButton value="Login" action="#{userBean.loginAction}" />          
</h:form>

var字段是元数据,不拥有字段值,而仅拥有其属性.所以我用

the var field is a metadata and not own the field value but only their attribute. So I use

#{userBean.getFieldValue(field.name)}

获取bean的字段值. 如果上面的代码仅用于查看页面,则效果很好. 但不在表单提交上,因为无法通过字段名称设置setFieldvalue. 有没有办法解决这个问题?有通用的方法来动态构建Back bean编辑表单吗?

to get the bean field value. The code above works well if it's used only to view the page. but not on form submit because of it's not possible to setFieldvalue by field name. Is there a way to override the problem? Is there a generale way to dynamically build a back bean edit form?

推荐答案

将其绑定到Map<String, Object>属性,并将大括号符号[]用于动态映射键.

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

例如

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>();
}

// ...

使用

<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.

这篇关于如何动态构建Back bean编辑表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆