user1801069
user1801069

Reputation: 75

Inoperative p:commandLink when using #{param} in rendered attribute

I have a page displaying some profile, so it's URL ../faces/savedProfile.xhtml?profileId=1234.

On that page I have p:pane containing a few p:commandLink components, something like this:

<p:panel rendered="#{profileController.canViewProfile}">
...
    <p:commandLink 
        id="duplicateLink"
        value="Duplicate"
        action="#{profileController.duplicateProfile}"/>
...
</p:panel>

It works. Now I want to add another rendering condition:

<p:panel rendered="#{profileController.canViewProfile
        and param['profileId'] != null}">
...
    <p:commandLink 
        id="duplicateLink"
        value="Duplicate"
        action="#{profileController.duplicateProfile}"/>
...
</p:panel>

The p:commandLink is displayed, but it is not active. It seems that action method is not being invoked on click. Why would that additional rendering condition kill the commandLink?

Upvotes: 1

Views: 909

Answers (1)

BalusC
BalusC

Reputation: 1109635

The rendered attribute is not only evaluated during the HTTP request which returns the page with the command link, but it's also evaluated during the HTTP request initiated by the command link. Apparently you're not retaining the request parameter during that HTTP request which causes that the rendered attribute evaluates false and thus the clicked command link cannot be idenfified and hence its action event will never be queued.

This matches point 5 of commandButton/commandLink/ajax action/listener method not invoked or input value not updated.

The solution in your case is to add <f:param> to retain the request parameter:

<p:commandLink ...>
    <f:param name="profileId" value="#{param.profileId}" />
</p:commandLink>

Unrelated to the concrete problem, better use empty instead of != null which will then also cover empty strings:

<p:panel rendered="#{profileController.canViewProfile and not empty param.profileId}">

Or much better, use <f:viewParam> in combination with a view scoped bean (and a Profile converter):

<f:viewParam name="profileId" value="#{profileController.profile}" />

with

<p:panel rendered="#{profileController.canViewProfile and not empty profileController.profile}">

See also:

Upvotes: 1

Related Questions