3

I'm trying to set a property value to a target bean using <f:setPropertyActionListener> inside <p:commandLink> as follows.

<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>

The corresponding JSF managed bean is as follows.

@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);
    }
}

When <p:commandLink> is clicked, the corresponding setter method in the bean setSubCatName(String subCatName) is expected to be invoked but this method is never invoked and consequently the bean property subCatName is never set to a value.

What am I overlooking here?

I'm using Mojarra 2.2.5 and PrimeFaces 4.0.

The process attribute of <p:commandLink> is already set to @this. I have already tried setting prependId of <h:form> to false but that did not make a difference either.


EDIT:

This works, when the scope of the managed bean is changed from @RequestScoped to @ViewScoped. This should work in @RequestScoped beans too. What wrong am I doing here? Is this the expected behaviour?

Tiny
  • 27,221
  • 105
  • 339
  • 599
  • Something seems to be wrong with your datagrid/ejb - try to check the calls using chrome, I tested your code and it calls your setter just fine however I didnt know your data so I removed the EJB and datagrid and just had a value in it. – VeenarM Mar 23 '14 at 07:20
  • It works without a `` but stops functioning, when a `` is used inside a ``. I cannot imagine, @VeenarM – Tiny Mar 23 '14 at 07:44
  • when is load run? to populate the table? you've marked your EJB as final so it's always going to be null? likely null pointers are abound? – VeenarM Mar 23 '14 at 07:49
  • The `load()` method is called as expected. It is invoked on page load first and then upon changing a page (next 1, 2, 3... previous), [data are lazily loaded from the database only when they are required]. It is the expected behaviour. `rowCount` and the sub category list are correctly populated from the database. They are not null (or empty). – Tiny Mar 23 '14 at 07:53
  • Not really sure what else to help with, I just ran the test using the primefaces DataGrid from there showcase and it works perfectly using datagrid, commandlink with actionlistener using value = #{row.model} target - the setters called fine. Can you maybe provide more information of the data or the other form object code. – VeenarM Mar 23 '14 at 08:13
  • This worked, when I changed the scope of the JSF managed bean from `@RequestScoped` to `@ViewScoped`. It should work with `@RequestScoped`. `@ViewScoped` in this case, is otherwise completely unnecessary because I'm just displaying data and not manipulating them (insert, update, delete, none of them I'm performing here). – Tiny Mar 23 '14 at 09:16
  • Very strange, because I copied your code and was using @RequestScoped and it worked fine. (even in a datagrid) – VeenarM Mar 23 '14 at 09:17

1 Answers1

2

This indeed requires a scope higher than the request scope for it to work (in this case, at least a view scoped bean is required). Please see this answer.

Community
  • 1
  • 1
Tiny
  • 27,221
  • 105
  • 339
  • 599