【发布时间】:2012-09-30 07:21:26
【问题描述】:
我想在同一页面中编辑项目列表。每个项目都应使用单独的表格进行编辑。我正在 ui:repeat 中创建一个 h:form。只有在提交最后一个表单时,用户输入才会应用于托管 bean。对于所有其他表单,用户输入不会应用于模型。
@ManagedBean
public class Controller {
Logger logger = Logger.getLogger("TestWeb");
private List<Customer> customerList;
public List<Customer> getCustomerList() {
if (customerList == null) {
customerList = new ArrayList<Customer>();
customerList.add(new Customer("Daffy Duck", "daffy@example.com"));
customerList.add(new Customer("Bugs Bunny", "bugs@example.com"));
customerList.add(new Customer("Samity Sam", "sam@example.com"));
}
return customerList;
}
public String updateCustomer(Customer c) {
logger.info("Updating: " + c.getName());
return null;
}
}
在视图中,我有
<ui:repeat var="c" value="#{controller.customerList}">
<h:form>
<h3>Edit Customer</h3>
Name: <h:inputText value="#{c.name}"/><br/>
E-mail: <h:inputText value="#{c.email}"/><br/>
<h:commandButton value="Update"
action="#{controller.updateCustomer(c)}"/>
</h:form>
</ui:repeat>
我搜索了几个小时而没有任何解决方案。这样做的正确方法是什么?我可以通过使用单个表单并在其中使用 ui:repeat 来破解它。但这有很多问题,我宁愿不走那条路。谢谢。
【问题讨论】:
-
您可以通过仅用
<h:form/>包装<h:commandButton/>来实现此目的。 -
@kolossus,你确定吗?我尝试将命令按钮包装在一个表单中。这并没有解决问题。
-
哦?回想起来,在呈现标记时,您提供给
updateCustomer方法的参数将被设置为最后一个值。您应该只使用<f:param/>将选定的值提供给支持 bean,也许只传递实体的 id。我以前用过这个,所以我确定它有效。但请注意,这样来回传递 ID 会带来安全风险。您可能希望以某种方式保护参数