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

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>

Related

Passing dynamic param through bundle.properties to alt text value of graphicImage [duplicate]

I have use case in which I have to use resource bundle to display various texts on UI. Some of these resource bundle entries take paramets (e.g. {0}), for these I use h:outputFormat but sometimes that isn't enough.
e.g.
someMessage=Display this message with param {0}
in a resource bundle.
To display it on xhtml I normally do:
<h:outputFormat value="#{msg['someMessage']}"><f:param value="#{someBean.value}"/></h:outputFormat>
That works well when it's a simple case, but for more complex use cases it isn't enough. For example if I want the 'title' attribute of a commandLink to use the above resource bundle entry:
<h:commandLink action="logout" title="#{msg['someMessage']}">
<f:param value="#{someBean.value}" />
<h:graphicImage library="images" name="image.png" />
</h:commandLink>
which doesn't work. I also tried:
<h:commandLink action="logout">
<f:attribute name="title">
<h:outputFormat value="#{msg['someMessage']}"><f:param value="#{someBean.value}"/></h:outputFormat>
</f:attribute>
<h:graphicImage library="images" name="image.png" />
</h:commandLink>
which also doesn't work since f:attibute doesn't allow children.
Even if there is a hack to bypass this (e.g. using hover component from primefaces) there are other fields that might require a parameterized message.
Does anyone know of a way to use MessageFormat that takes an argument in a non-value field of a JSF component?
You could create a custom EL function for this with which you can ultimately end up like:
<h:commandLink ... title="#{my:format(msg['someMessage'], someBean.value)}" />
You can use the MessageFormat API to perform the job, exactly as <h:outputFormat> is doing under the covers.
An alternative is to create a custom component which does the same as JSTL's good 'ol <fmt:message> which supports a var attribute to export the formatted message into the EL scope.
<my:outputFormat ... var="linkTitle">
...
</my:outputFormat>
<h:commandLink ... title="#{linkTitle}" />
Update: JSF utility library OmniFaces has #{of:formatX()} functions and a <o:outputFormat> component for the very purpose.

EL conditional render based on composite component attribute [duplicate]

I'm using an <h:outputLink> as follows.
<c:set var="cid" value="1"/>
<c:set var="sid" value="2"/>
<h:outputLink value="Test.jsf">
<h:outputText value="Link"/>
<f:param name="cid" value="#{cid}"/>
<f:param name="sid" value="#{sid}"/>
</h:outputLink>
This is just an example. Both of the query-string parameters are dynamic. So, <c:set> used here is just for the sake of demonstration.
At any time, either one, both or none of the parameters may be present. In case, if only one or none of them is present then, parameter/s are unnecessarily appended to the URL which should not happen. Preventing unnecessary query-string parameters from being appended to the URL requires a conditional rendering of <f:param>.
JSTL <c:if> like the following
<c:if test="${not empty cid}">
<f:param name="cid" value="#{cid}"/>
</c:if>
did not work.
How can it be made possible to conditionally render <f:param> inside <h:outputLink>?
The <f:param> has a disable (not disabled!) attribute for the purpose.
<f:param name="cid" value="#{cid}" disable="#{empty cid}" />
<f:param name="sid" value="#{sid}" disable="#{empty sid}" />
Note that this has a bug in Mojarra versions older than 2.1.15, because they typo'ed the actual UIParameter property to be disble instead of disable. See also issue 2312.
As to the <c:if> approach, that would only work if the #{cid} and #{sid} is available during view build time. In other words, it would fail if they are only available during view render time, e.g. when they depend on var of a repeater component. See also JSTL in JSF2 Facelets... makes sense?
See also:
f:param tag attribute 'disable' is not work
Don't you like this kind of a solution?
<f:param name="#{cid == null ? '' : 'cid'}" value="#{cid}"/>
<f:param name="#{sid == null ? '' : 'sid'}" value="#{sid}"/>

What different between p:param and custom attribute

I suspect about 2 way of passing value through attribute.
First: f:param
<p:inputText value="#{inputTextView.inputVal}">
<f:param name="fieldA" value="inputA" />
<p:ajax process="#form" update="#form"></p:ajax>
</p:inputText>
Second: custom field
<p:inputText value="#{inputTextView.inputVal}"
fieldB="inputB">
<p:ajax process="#form" update="#form"></p:ajax>
</p:inputText>
The first way, I can get value of attribute by using
FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("fieldA");
While the second way use
UIComponent.getCurrentComponent(FacesContext.getCurrentInstance()).getAttributes().get("fieldB");
Does anyone know what different between first and second?
What situation which appropriate for use the first approach?
First of all, if you want to set a custom attribute, you cannot just add a random new attribute to a tag because JSF ignores unsupported tag attributes. BalusC gives some options on how to work around this in this answer.
f:param should be used if you want to add values to the query string or request parameters. It should be used with command components (e.g. h:commandButton, h:commandLink or h:outputLink).
f:attribute on the other hand, adds entries to the attribute Map of a component.

Passing EL method expression as attribute of custom Facelets tagfile

I created a custom JSF tag:
<ui:composition>
<h:panelGroup>
<rich:dataScroller id="#{id}" for="#{table}" execute="#{table}"
page="#{scrollerPage}" render="#{table}-sc1" maxPages="5"
fastControls="hide" oncomplete="#{onCompl}" scrollListener="#{scrollListenerBean[scrollListenerMethod]}" />
<h:inputText value="#{scrollerPage}" id="#{table}-sc1" size="2">
<f:convertNumber integerOnly="true" />
</h:inputText>
<h:outputText styleClass="outputText"
value=" of #{scrollPagesCount} " />
<h:commandButton value="GO! " />
</h:panelGroup>
</ui:composition>
To pass the listener method, I used the solution suggested in a quite old blog:
<my:dataScroller id="idDS1" table="table1"
scrollerPage="#{bean.navigationHelper.scrollerPage}"
scrollPagesCount="#{bean.navigationHelper.scrollPagesCount}"
onCompl="initForm();"
scrollListenerBean="#{bean}"
scrollListenerMethod="aMethod" />
My questions are: is this the best way to do this? How can I make the method optional?
Thanks a lot for any Help! bye!
My questions are: is this the best way to do this?
That's the only way anyway, provided that you can only use standard JSF/EL facilities and you cannot create a custom taghandler.
You could however create a custom taghandler to convert the value expression to a method expression. The OmniFaces JSF utility library has a <o:methodParam> for exactly this purpose. See also the <o:methodParam> demo page.
You could then end up like:
<my:dataScroller ... scrollListener="#{bean.aMethod}" />
and
<o:methodParam name="scrollListenerMethod" value="#{scrollListener}" />
<rich:dataScroller ... scrollListener="#{scrollListenerMethod}" />
See also:
Dynamic ui include and commandButton
How can I make the method optional?
Theoretically, you could use JSTL tags to build the view conditionally. Something like:
<h:someComponent>
<c:if test="#{not empty fooAttribute}">
<f:attribute name="foo" value="#{fooAttriubte}" />
</c:if>
</h:someComponent>
But that's in the particular case of a special method expression listener attribute unfortunately not possible. There's no such thing as <rich:scrollListener> or something which allows you binding a RichFaces specific scrollListener as a separate tag to the <rich:dataScroller>. Best what you could do without creating custom taghandlers is duplicating the whole <rich:dataScroller> in two <c:if>s (or a <c:choose>); one with and other without scrollListener. This is too clumsy. You'd really better create a custom <my:richScrollListener> taghandler for this which you could then place in a <c:if>.

Reuse variable outside of composition - passing variables up in jsf?

I'm using jsf 1.2 and have got some weird javascript which gets repeated at several places in my project.
I do not want to repeat myself.. but i found no way to do something similar to this:
<ui:composition>
<c:set var="confirmBlockJS" value="if (confirm('#{tk:encodeJS(confirmMessage)}')) {#{confirmed}} else{ #{notConfirmed}}"/>
</ui:composition>
Now i want to use the set variable but obv this will fail.
<ui:include src="/blocks/tech/confirmBlock.xhtml">
<ui:param name="confirmMessage" value="#{appTexts['action.logout.title']}"/>
<ui:param name="confirmed" value="return false;"/>
<ui:param name="notConfirmed" value=" #{doJS}"/>
</ui:include>
<h:outputLink id="logout" value="#" rendered="#{renderLogout}"
onclick='#{confirmBlockJS};'
styleClass="_allowEnterKey">
<h:outputText value="#{appTexts['action.logout']}"></h:outputText>
</h:outputLink>
confirmBlockJS is obviously empty at that point, which is actually in my eyes a good thing. But is there no way to pass the variable up? I tried ui:param without luck.
SOLUTION
Use ui:decorate when you want to access variable set with c:set outside of the page which uses the ui:decorate. It will still be visible. In the case of ui:include the c:set will not be visible outside, but ui:param should pass it up (e.g. use ui:param instead of c:set in the ui:include composition)
If you didn't want to inline it as an attribute value, then <ui:decorate> would be the solution for you.
An EL function is closest what you can get to work for this particular functional requirement:
<h:outputLink id="logout" value="#" rendered="#{renderLogout}"
onclick="#{js:confirmBlock(appTexts['action.logout.title'], 'return false;', doJS)}"
styleClass="_allowEnterKey">
<h:outputText value="#{appTexts['action.logout']}"></h:outputText>
</h:outputLink>
or:
<ui:param name="confirmBlock" value="#{js:confirmBlock(appTexts['action.logout.title'], 'return false;', doJS)}" />
<h:outputLink id="logout" value="#" rendered="#{renderLogout}"
onclick="#{confirmBlock}"
styleClass="_allowEnterKey">
<h:outputText value="#{appTexts['action.logout']}"></h:outputText>
</h:outputLink>
Note that generating JS code by Java/JSF/EL this way is a bit a smell. Is it really not possible to make it a simple JS function which you put in a .js file and parameterize it with arguments?

Resources