我可以动态添加 JSF 组件吗?我需要一个带有按钮的表单,该按钮应该添加一个<h:inputText>
到表格。这可能吗?
我知道这在 JavaScript 中应该是可能的。有人知道如何在 JSF 中做到这一点吗?我认为主要问题是如何通过以下方式获取或设置新输入的值#{value}
.
使用迭代组件,例如<h:dataTable>
or <ui:repeat>
显示动态大小的List
的实体。做豆子@ViewScoped
以确保在同一视图上的回发过程中记住该列表,而不是一遍又一遍地重新创建。
开球示例<h:dataTable>
(当使用<ui:repeat>
只需更换<h:dataTable>
by <ui:repeat>
, and <h:column>
例如<li>
or <div>
):
<h:form>
<h:dataTable value="#{bean.items}" var="item">
<h:column><h:inputText value="#{item.value}" /></h:column>
<h:column><h:commandButton value="remove" action="#{bean.remove(item)}" /></h:column>
</h:dataTable>
<h:commandButton value="add" action="#{bean.add}" />
<h:commandButton value="save" action="#{bean.save}" />
</h:form>
托管bean:
@Named
@ViewScoped
public class Bean {
private List<Item> items;
@PostConstruct
public void init() {
items = new ArrayList<>();
}
public void add() {
items.add(new Item());
}
public void remove(Item item) {
items.remove(item);
}
public void save() {
System.out.println("items: " + items);
}
public List<Item> getItems() {
return items;
}
}
Model:
public class Item {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String toString() {
return String.format("Item[value=%s]", value);
}
}
也可以看看:
- 推荐的 JSF 2.0 CRUD 框架 https://stackoverflow.com/questions/3180400
- 如何创建动态 JSF 表单字段 https://stackoverflow.com/questions/3510614
- 如何使用 JSF 2.0 复合组件实现动态列表? https://stackoverflow.com/questions/6358066
- 如何选择合适的bean范围? https://stackoverflow.com/questions/7031885
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)