p:commandLink内的f:setPropertyActionListener未将属性值设置到目标请求作用域bean中。

3
我正在尝试使用<f:setPropertyActionListener><p:commandLink>中设置目标bean的属性值,具体操作如下。
<h:form id="form" prependId="true">
    <p:panel id="dataPanel" closable="false" toggleOrientation="horizontal" toggleable="true" style="border: none; text-align: center;">
        <p:dataGrid id="dataGrid" value="#{productDetailsManagedBean}" var="row" rowIndexVar="rowIndex" rows="4" first="0" columns="1" paginator="true" paginatorAlwaysVisible="false" pageLinks="10" lazy="true" rowsPerPageTemplate="5,10,15">

            <p:commandLink process="@this">
                <h:outputText styleClass="ui-icon ui-icon-search" style="margin:0 auto;" />
                <!--row.subCatName is correctly retrieved here.-->
                <f:setPropertyActionListener value="#{row.subCatName}" target="#{productDetailsManagedBean.subCatName}" />
            </p:commandLink>

        </p:dataGrid>
    </p:panel>
</h:form>
相应的JSF管理bean如下所示。
@ManagedBean
@RequestScoped
public final class ProductDetailsManagedBean extends LazyDataModel<SubCategory>
{
    public ProductDetailsManagedBean() {}

    @EJB
    private final ProductDetailsBeanLocal productService=null;
    private String subCatName;

    public String getSubCatName() {
        return subCatName;
    }

    public void setSubCatName(String subCatName) {
        System.out.println("setSubCatName() called. : "+subCatName); //Never invoked.
        this.subCatName = subCatName;
    }

    //The following methods are not needed to be reviewed.

    @Override
    public List<SubCategory> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters)
    {
        int rowCount = productService.rowCount().intValue();
        setRowCount(rowCount);
        return productService.getList(first, pageSize);
    }
}
当单击<p:commandLink>时,预计将调用Bean中的相应setter方法setSubCatName(String subCatName),但这个方法从未被调用,因此bean属性subCatName从未设置为一个值。 我在这里忽略了什么? 我正在使用Mojarra 2.2.5和PrimeFaces 4.0。 <p:commandLink>process属性已经设置为@this。我已经尝试将<h:form>prependId设置为false,但也没有改变。
编辑: 当托管Bean的范围从@RequestScoped更改为@ViewScoped时,它可以工作。这应该在@RequestScoped的bean中也起作用。我做错了什么?这是期望的行为吗?

当没有<p:dataGrid>时,它可以正常工作,但是当在<p:dataGrid>中使用<p:commandLink>时,它停止工作。我无法想象,@VeenarM - Tiny
何时加载运行?以填充表格?您将EJB标记为final,因此它将始终为空?可能存在空指针异常? - VeenarM
load() 方法按预期被调用。它在页面加载时首先被调用,然后在更改页面(下一页1、2、3...上一页)时被调用,[仅在需要时从数据库中惰性加载数据]。这是预期的行为。rowCount 和子类别列表从数据库中正确填充。它们不是 null(或空)。 - Tiny
当我将JSF托管bean的范围从@RequestScoped更改为@ViewScoped时,它起作用了。它应该可以使用@RequestScoped。在这种情况下,@ViewScoped是完全不必要的,因为我只是显示数据而不是操作它们(插入、更新、删除,这些操作我都没有执行)。 - Tiny
非常奇怪,因为我复制了你的代码并使用了@RequestScoped,它可以正常工作。(即使在数据网格中) - VeenarM
显示剩余2条评论
1个回答

阿里云服务器只需要99元/年,新老用户同享,点击查看详情
2

这确实需要比请求范围更高的范围才能使其工作(在这种情况下,至少需要一个视图作用域bean)。请参见此答案。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,