【问题标题】:<h:form> within <ui:repeat> not entirely working, only the last <h:form> is processed<ui:repeat> 中的 <h:form> 不完全工作,只处理最后一个 <h:form>
【发布时间】: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 来破解它。但这有很多问题,我宁愿不走那条路。谢谢。

【问题讨论】:

  • 您可以通过仅用&lt;h:form/&gt; 包装&lt;h:commandButton/&gt; 来实现此目的。
  • @kolossus,你确定吗?我尝试将命令按钮包装在一个表单中。这并没有解决问题。
  • 哦?回想起来,在呈现标记时,您提供给 updateCustomer 方法的参数将被设置为最后一个值。您应该只使用 &lt;f:param/&gt; 将选定的值提供给支持 bean,也许只传递实体的 id。我以前用过这个,所以我确定它有效。但请注意,这样来回传递 ID 会带来安全风险。您可能希望以某种方式保护参数

标签: forms jsf-2 uirepeat


【解决方案1】:

这是 Mojarra 中 &lt;ui:repeat&gt; 的状态保存错误。在http://java.net/jira/browse/JAVASERVERFACES 和其他issue 2243 有几个类似的问题报告。

您基本上有 2 个选择:使用另一个迭代组件(例如 &lt;c:forEach&gt;&lt;h:dataTable&gt;&lt;t:dataList&gt;&lt;p:dataList&gt; 等),或者用 MyFaces 替换 Mojarra(此构造中的 &lt;ui:repeat&gt; 在那里)。

【讨论】:

  • 我已经使用 重新设计了我的解决方案,现在一切正常。谢谢!
猜你喜欢
  • 2014-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-17
  • 1970-01-01
  • 2012-09-15
  • 2012-12-15
  • 1970-01-01
相关资源
最近更新 更多