繁体   English   中英

f:ajax和ui:repeat内部的输入-为什么不调用getter?

[英]f:ajax and inputs inside ui:repeat - why aren't getters called?

我无法理解为什么我期望在ui:repeat中没有调用我的吸气剂的原因。 我正在使用Glassfish 3.1.1 / Mojarra 2.1.3。

下面的代码将呈现一个类似

Alice [empty input] [empty output]  [link: update value] [link: cancel]
Bob   [empty input] [empty output]  [link: update value] [link: cancel]

如果我在Alice行上单击“更新值”,然后在“ Bob”行上单击“更新值”,则结果如下:

Alice [Alice] Alice
Bob   [Alice] Bob

我不明白为什么“鲍勃”行的输出却选择了“爱丽丝”。 就像在渲染-响应阶段未调用getter一样,而是将托管Bean中的旧值粘贴在update-model-values阶段中的UIInput上。

奇怪的是,如果我在Alice行上单击“更新值”, 然后在Bob行上单击“ 取消” ,然后单击“更新值”, 可以得到预期的结果。

另外,如果我在“更新值”链接上的render = ...上添加“ @form”,我将看到正确的值(尽管它们将在每行上重复)。 我不太喜欢这样做,主要是因为我不想更新整个表来处理一行。

是什么原因造成的? 我对JSF生命周期缺少什么?

另外-相同的模式在ui:repeat外部也可以正常工作。 在这种情况下,h:inputText似乎总是从托管bean中以正确的值刷新,并按我期望的那样在“渲染响应”阶段调用getter。

这最初使用的是PrimeFaces p:commandLink,但是我在标准JSF h:commandLink和f:ajax上得到的行为完全相同。

我也知道PrimeFaces行编辑器,这可能是解决一般总体问题的更好解决方案-我仍然想了解为什么这行不通。

谢谢!

相关的XHTML如下

<h:form id="testForm">
    <table style="width:400px; ">
    <ui:repeat value="#{testBean.customers}" var="customer" varStatus="status">
    <tr>
        <td><h:outputText id="output" value="#{customer.name}"/></td>
        <td><h:inputText id="input" value="#{testBean.customerEdit.name}"/></td>
        <td><h:outputText id="otherOutput" value="#{testBean.customerEdit.name}"/></td>
        <td>
            <h:commandLink actionListener="#{testBean.edit(status.index)}">
                <f:ajax execute="@this" render="input otherOutput"/>
                Update value
            </h:commandLink>
            <h:commandLink actionListener="#{testBean.cancel}">
                <f:ajax execute="@this" render="input otherOutput"/>
                Cancel
            </h:commandLink>
        </td>
    </tr>
    </ui:repeat>
    </table>
</h:form>

“ testBean”受管bean具有视图作用域:

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ViewScoped
@ManagedBean
public class TestBean implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;
public static class Customer {
    private String name;

    public Customer(String name) {
        this.name = name;
    }

    public String getName() {
        System.out.println("returning name: " + name);
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

private List<Customer> customers;
private Customer customerEdit = new Customer(null);

@PostConstruct
public void init() {
    customers = Arrays.asList(new Customer("Alice"), 
            new Customer("Bob"), new Customer("Carol"), new Customer("David"), new Customer("Eve"));
}
public Customer getCustomerEdit() {
    return customerEdit;
}
public void setCustomerEdit(Customer customerEdit) {
    this.customerEdit = customerEdit;
}

public void edit(int index) {
    System.out.println("Called edit with index: " + index);
    customerEdit = new Customer(customers.get(index).getName());
}

public void save(int index) {
    System.out.println("Called save with index: " + index + " new name = " + customerEdit.getName());
    customers.set(index, customerEdit);
    customerEdit = null;
}

public void cancel() {
    System.out.println("Called cancel");
    customerEdit = null;
}
public List<Customer> getCustomers() {
    return customers;
}
public void setCustomers(List<Customer> customers) {
    this.customers = customers;
}
}

您的问题在这一行:

 <h:commandLink actionListener="#{testBean.edit(status.index)}">

您不能通过这种方式将参数发送给动作侦听器,而事实并非如此。 您需要将该行更改为以下内容:

<h:commandLink actionListener="#{testBean.edit}" customerIndex="#{status.index}>

然后将编辑方法更改为这样。

public void edit(ActionEvent ae) {
    int index = ae.getComponent().getAttributes().get("customerIndex");
    System.out.println("Called edit with index: " + index);
    customerEdit = new Customer(customers.get(index).getName());
}

另外,我不确定您的“保存”方法与其他任何方法之间的关系,但这可能只是因为您跳过了一些不相关的代码。

编辑:如果这是一种javascript方法,则可以通过这种方式发送参数,但不能发送给托管bean或#{}标记内的其他任何东西。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM