Passing a method to composite component executes the method directly [duplicate] - jsf

I have a UI component with a MethodExpression attribute changeListener:
<composite:interface>
<composite:attribute name="changeListener" required="false" method-signature="void actionListener(javax.faces.event.ActionEvent)" />
..
</composite:interface>
<composite:implementation>
<p:remoteCommand name="ajaxOnChange"
update="#{cc.attrs.onChangeUpdate}"
oncomplete="#{cc.attrs.onchange}"
actionListener="#{cc.attrs.changeListener}" />
..
</composite:implementation>
This changeListener attribute is an optional method expression used as actionListener in the remoteCommand and I want to render the <p:remoteCommand> ONLY IF the changeListener attribute has been set.
I have tried several ways to check whether the attribute is set or not, especially:
<c:if test="#{! empty cc.attrs.changeListener}">
and
<p:remoteCommand rendered="#{cc.attrs.changeListener != null}" />
But I get a javax.el.PropertyNotFoundException because it tries to evaluate the attribute as a property instead.
How can I evaluate whether the optional method attribute is set or not ?
thanks

You was in the right direction with <c:if> already. The rendered one is never going to work. You only need to check if the EL expression is been set instead of actually evaluating the whole EL expression as a value expression and checking if its result is not empty, which would of course fail if the EL expression represents a method expression.
<c:if test="#{not empty cc.getValueExpression('changeListener')}">
...
</c:if>
This solution is however somewhat scary: you're grabbing the method expression as a value expression here. However, as long as you don't actually evaluate the enclosed EL expression (like as what your initial #{cc.attrs.changeListener} attempt does under the covers), then there's nothing at matter. There's no other clean way as there's nothing like UIComponent#getMethodExpression() in JSF API.

Related

Setting f:setPropertyActionListener value with a f:param value

I'm trying to use the setPropertyActionListener tag to set a value in my backing bean. However, it doesn't work as I expected.
Context: userService is an instance of my backing bean, which contains an int member, reqID. This, in turn, is the key to a map of objects that belong to a class called User. I'm trying to create a page that will list all instances of User, and provide a button to visit a separate view that shows that particular User's information. To do this, I'm attempting to set userService.reqID to the id of the chosen User so it can generate a reference to that user for the next view (which is done in the call userService.toUserInfo).
If I use the xhtml snippet below:
<ui:define name="content">
<h:form>
<h:panelGrid>
<ui:repeat value="#{userService.UserList.getUserList()}" var="user">
<li>
<h:outputText value="#{user.name}" />
<h:commandButton value="View details of #{user.name}" action="#{userService.toUserInfo}">
<f:param name="id" value="#{user.id}" />
<f:setPropertyActionListener target="#{userService.reqID}" value="#{id}"/>
</h:commandButton>
</li>
</ui:repeat>
</h:panelGrid>
</h:form>
</ui:define>
The tag does not appear to evaluate id correctly and I get a Null Pointer Exception.
Earlier, I tried changing my setPropertyActionListenerTag so it read out as:
<f:setPropertyActionListener target="#{userService.reqID}" value="id"/>
which gave me an error, because the tag was sending the string "id" as opposed to the int value of the parameter.
Is there some way to force f:setPropertyActionListener to evaluate the expression under value? Or is there another tag that will allow me to do this?
Also, is ui:param used appropriately here?
The <f:param> (and <ui:param>) doesn't work that way. The <f:param> is intented to add HTTP request parameters to outcome of <h:xxxLink> and <h:xxxButton> components, and to parameterize the message format in <h:outputFormat>. The <ui:param> is intented to pass Facelet context parameters to <ui:include>, <ui:decorate> and <ui:define>. Mojarra had the bug that it also behaves like <c:set> without a scope. This is not the intented usage.
Just use <c:set> without a scope if it's absolutely necessary to "alias" a (long) EL expression.
<c:set var="id" value="#{user.id}" />
Put it outside the <h:commandLink> though. Also in this construct, it's kind of weird. It doesn't make the code better. I'd just leave out it.
<f:setPropertyActionListener ... value="#{user.id}" />
See also:
Setting ui:param conditionally
what is the scope of <ui:param> in JSF?
Defining and reusing an EL variable in JSF page
Unrelated to the concrete problem, if you're using EL 2.2 (as you're using JSF 2.2, you undoubtedly are as it requires a minimum of Servlet 3.0, which goes hand in hand with EL 2.2), then just pass it as bean action method argument without <f:setPropertyActionListener> mess. See also a.o. Invoke direct methods or methods with arguments / variables / parameters in EL and How can I pass selected row to commandLink inside dataTable?
<h:commandButton ... action="#{userService.toUserInfo(user.id)}">
On again another unrelated note, such a "View user" or "Edit user" request is usually idempotent. You'd better use <h:link> (yes, with <f:param>) for this. See also a.o. Creating master-detail pages for entities, how to link them and which bean scope to choose and How to navigate in JSF? How to make URL reflect current page (and not previous one).
Oh, that <h:panelGrid> around the <ui:repeat><li> doesn't make sense in HTML perspective. Get rid of it and use <ul> instead. See also HTMLDog HTML Beginner tutorial.

commandLink with ui:repeat in composite component

I am trying to use links in a composite component, my links are in a simple array and do not belong to any managed bean.
<composite:interface>
<composite:attribute name="links" required="true" />
</composite:interface>
<composite:implementation>
<ui:param name="linksSplit" value="#{fn:split(cc.attrs.links, ',')}" />
<ui:repeat var="link" value="#{linksSplit}" >
<h:commandLink value="#{option}" action="#{link}" />
</ui:repeat>
</composite:implementation>
I am getting the following error :
Identity '#{link}' does not reference a MethodExpression instance, returned type: java.lang.String
We are supposed to use String or bean methods in EL expression but I don't understand why we cannot evaluate a parameter which is a String (link in my case).
If I put a real String referenced in my faces-config, it works
<h:commandLink value="#{option}" action="#navigate" />
If you have an explanation or a workaround to get my link working, it would be great
When specifying an EL expression in <h:commandLink action>, it's interpreted as a method expression returning a String (or void if you don't want to navigate). See also the tag documentation:
Name action
Type javax.el.MethodExpression
(signature must match java.lang.Object action())
Description MethodExpression representing the application action to invoke when this component is activated by the user. The expression must evaluate to a public method that takes no parameters, and returns an Object (the toString() of which is called to derive the logical outcome) which is passed to the NavigationHandler for this application.
Given the fact that you seem to want pure page-to-page navigation links, you're actually going in the wrong direction as to using <h:commandLink> for that. You should instead be using <h:link> for that. It generates SEO-friendly and bookmarkable GET links instead of a piece of JavaScript which submits a parent POST form.
<ui:repeat var="link" value="#{linksSplit}" >
<h:link value="#{option}" outcome="#{link}" />
</ui:repeat>
See also:
When should I use h:outputLink instead of h:commandLink?
How to navigate in JSF? How to make URL reflect current page (and not previous one)
Note that this all has nothing to do with composite components. You'd have had exactly the same problem when using this in a normal page.

Evaluating if MethodExpression attribute is set (getting PropertyNotFoundException)

I have a UI component with a MethodExpression attribute changeListener:
<composite:interface>
<composite:attribute name="changeListener" required="false" method-signature="void actionListener(javax.faces.event.ActionEvent)" />
..
</composite:interface>
<composite:implementation>
<p:remoteCommand name="ajaxOnChange"
update="#{cc.attrs.onChangeUpdate}"
oncomplete="#{cc.attrs.onchange}"
actionListener="#{cc.attrs.changeListener}" />
..
</composite:implementation>
This changeListener attribute is an optional method expression used as actionListener in the remoteCommand and I want to render the <p:remoteCommand> ONLY IF the changeListener attribute has been set.
I have tried several ways to check whether the attribute is set or not, especially:
<c:if test="#{! empty cc.attrs.changeListener}">
and
<p:remoteCommand rendered="#{cc.attrs.changeListener != null}" />
But I get a javax.el.PropertyNotFoundException because it tries to evaluate the attribute as a property instead.
How can I evaluate whether the optional method attribute is set or not ?
thanks
You was in the right direction with <c:if> already. The rendered one is never going to work. You only need to check if the EL expression is been set instead of actually evaluating the whole EL expression as a value expression and checking if its result is not empty, which would of course fail if the EL expression represents a method expression.
<c:if test="#{not empty cc.getValueExpression('changeListener')}">
...
</c:if>
This solution is however somewhat scary: you're grabbing the method expression as a value expression here. However, as long as you don't actually evaluate the enclosed EL expression (like as what your initial #{cc.attrs.changeListener} attempt does under the covers), then there's nothing at matter. There's no other clean way as there's nothing like UIComponent#getMethodExpression() in JSF API.

Pass an input value directly as action method argument

Is there a way to do pass an input value as a action's parameter without using managed properties?
i.e.
<h:form>
<h:inputText id="input" />
<h:commandButton action="#{someBean.doSome(input)}" />
</h:form>
Yes, it's during the form submit already there in the JSF component state. Just bind the input component to the view by binding attribute, which will reference an UIInput instance, which in turn has a getValue() method for the very purpose of retrieving the input value (so that you can pass it as action method argument):
<h:form>
<h:inputText ... binding="#{input}" />
<h:commandButton ... action="#{someBean.doSome(input.value)}" />
</h:form>
The properness of this approach is however highly questionable and depends on concrete functional requirements. This approach is namely basically tight-coupling the view with the model and therefore considered a bad practice.
See also:
How to send form input values and invoke a method in JSF bean
How does the 'binding' attribute work in JSF? When and how should it be used?

what wrong with <h:commandButton value="...?userId=#{...}"

The whole syntax is as below
<h:commandLink action="CustomerDetails?faces-redirect=true&customerId=#{item.id}" value="#{item.name}"/>
This is inside a dataTable, hence explain the use if item. But the above code give me
Not a Valid Method Expression: CustomerDetails?faces-redirect=true&customerId=#{item.id}
It seems to not allow me to concatenate string and EL expression. Done that many inside value attribute, must be something with the action attribute. Any one got a solution for this?
Try using an f:param in an h:link to set the customerId, which is the canonical way of setting query parameters.
So:
<h:link outcome="CustomerDetails" value="#{item.name}">
<f:param name="customerId" value="#{item.id}" />
</h:link>

Resources