Richfaces4.2 conditional rendering issue - jsf-2

We are in process of migrating from JSP VDL to Facelets VDL. We have conditional rendering tag need to be ported to Facelets. Because of && symbol in condition rendering, .xhtml failing in compilation. Any thoughts on how to handle this?
<a4j:outputPanel styleClass="myclass" layout="block"
rendered="#{myBean.iscorrect && anotherBean.isCorrect}">
render something here ...
</a4j:outputPanel>
Thanks for your time.

Use and instead of &&. It's also immediately more self-documenting.
<a4j:outputPanel styleClass="myclass" layout="block"
rendered="#{myBean.iscorrect and anotherBean.isCorrect}">
The reason is because Facelets is a XML based view technology and that & is a special character in XML representing the start of an entity. The exact Facelets compilation error message which you have gotten should also have hinted something about that. Other special characters to be careful for are < and > which should in EL be replaced by lt and gt respectively.
This problem is unrelated to RichFaces. You would have exactly the same problem when doing so in standard JSF tags.
By the way, do you really have a isIscorrect() getter method? It would make more sense to me if you had a private boolean correct; with public boolean isCorrect() method and are evaluating as rendered="#{myBean.correct and anotherBean.correct}".

Related

Using f:selectItems var in passtrough attribute

can I pass expressions to JSF 2 passthrough-attributes?
the following code is not working. expression #{country.isoCode} is not evaluated.
<h:selectOneMenu value="#{bean.selectedCountry}" styleClass="selectlist">
<f:selectItems
value="#{bean.countries}" var="country"
itemLabel="#{country.countryName}"
pt:data-icon="flag flag-#{country.isoCode}"/>
</h:selectOneMenu>
I am using namespace
xmlns:pt="http://xmlns.jcp.org/jsf/passthrough"
and bootstrap-select. attribute "data-icon" is used to show an image. see:
http://silviomoreto.github.io/bootstrap-select/#data-icon
rendered output:
<i class="glyphicon flag flag-"></i>
EL is basically supported/evaluated over all place in a Facelet template. Also outside tags/attributes. Even in HTML comments, where many starters then fall over. So that's not the problem.
Your particular case is, unfortunately, "by design". Before rendering the first <option> element, the <f:selectItems> is is wholly parsed only once and turned into an iterator during which all EL expressions will be evaluated. Then, the component will iterate over it while rendering <option> elements during which all passthrough attributes will be evaluated. However, as the var was already evaluated during creating the iterator, it isn't available anywhere during rendering the passthrough attributes and ultimately evaluates to an empty string.
Fixing that would require quite some changes in standard JSF implementation of <f:selectItems>. I'm not sure if JSF guys would be all ears for that, but you can always try to create an issue.
You can work around this by creating physically multiple <f:selectItem> instances during view build time, with help of <c:forEach>.
<h:selectOneMenu ...>
<c:forEach items="#{bean.countries}" var="country">
<f:selectItem
itemValue="#{country}"
itemLabel="#{country.countryName}"
pt:data-icon="flag flag-#{country.isoCode}" />
</c:forEach>
</h:selectOneMenu>
See also:
JSTL in JSF2 Facelets... makes sense?

h:commandbutton calls wrong action method

I faced a interesting problem here. When I click commandButton, it calls wrong action method even button has not action attribute. What is this?
That may happen when the EL expression which you intented to use as a method expression is been evaluated as a value expression.
For example, when you have outcommented it using a HTML comment while not having configured Facelets to skip comments.
<!-- <h:commandButton value="submit" action="#{bean.submit()}" /> -->
The HTML comments namely doesn't stop EL expressions like #{bean.submit()} from being evaluated. It will ultimately generate HTML output with the return value of #{bean.submit()} inlined in the action attribute.
To naildown the root cause in your case, you should be creating and providing a fullworthy SSCCE.
See also:
How can I remove HTML comments in my Facelets?
Outcommented Facelets code still invokes EL expressions like #{bean.action()} and causes javax.el.PropertyNotFoundException on #{bean.action}

Trying to find c:out tag from the JSTL library within JSF application

I am building out a dynamic table using JSTL and was hoping to make use of the c:out tag to help build out some expressions, but am not able to find that tag available among the other JSTL core tags.
I have the following namespace being used:
xmlns:c="http://java.sun.com/jsp/jstl/core"
and made sure my web.xml file was set to use the 2.5 spec found here
https://stackoverflow.com/tags/jstl/info
but still only find catch, choose, forEach, if, otherwise, set, and when.
Additionally, I tried importing the JSTL 1.2.1.jar libraries as well with no success.
So, should the c:out tag be available for me to use in JSF2 ? If so, what steps am I missing?
Regards,
Mike
The <c:out> indeed not available in JSF2 Facelets. You don't need it in JSF2 Facelets anyway. Just use the JSF equivalent <h:outputText>,
<h:outputText value="#{bean.text}" />
or even better, just put EL in template text,
#{bean.text}
It will already be implicitly XML-escaped if that's your sole concern (and was during old JSP2 ages actually the sole reason why <c:out> is been used; in JSP1 the tag was simply mandatory in order to display a bean property as EL in template text wasn't supported in JSP1). The <c:out> offers no additional advantages over those standard JSF2 Facelets ways, that's why it's been removed from the JSTL subset for Facelets.
See also:
Is it suggested to use h:outputText for everything?

Can I use Get-Parameter in EL 2.2 without writing it into the Bean?

I want to parse the URL
title.xhtml?id=1
My code within "title.xhtml" should look like
...
<h:outputText value="#{titles.getTitle(${param.id}).id}"></h:outputText>
...
But unfortunatelly this does not work, since nested "#" and "$" is not accepted.
So my question is: Can I use an URL parameter and hand it over to a bean function without to store it separately within the bean?
That's invalid EL syntax. You can't and don't need to nest EL expressions in any way. Even nesting ${} is invalid. The only difference between #{} and ${} is that the #{} can perform a set operation as well (where applicable), while the ${} can do only a get operation.
This is valid EL syntax:
<h:outputText value="#{titles.getTitle(param.id).id}" />
Note that #{param.id} is fully legal JSF EL syntax. To avoid future confusions, it would be a good idea to make sure that you never use the old JSP EL syntax ${} in JSF anymore. See also Difference between JSP EL, JSF EL and Unified EL.

f:verbatim tag stops working when inside a dataTable

I posted this to the PrimeFaces user forum but I think they are too busy to look into it, so I thought I would try here.
I have server-side string that has markup in it, so when I want it rendered I do this:
<p:panel>
<f:verbatim>
#{daBean.markedUpString}
</f:verbatim>
</p:panel>
This works fine, but not if the same tag is used inside a p:dataTable -- either with or without the p:panel enclosure. What gets rendered is a div class="ui-dt-c" element with nothing in it. To test, if I take out the f:verbatim tag the marked-up text gets escaped and rendered.
I don't know if this should be considered a bug or not, but does anyone know of a work-around for this? This is with PrimeFaces 3.0.M3.
The <f:verbatim> tag is intented to hold plain text/HTML, not JSF components nor EL expressions. The tag is a leftover from JSF 1.0/1.1 ages when it was not possible to inline plain text/HTML between JSF components. The tag is deprecated in JSF2. You do not need it anymore.
Your concrete functional requirement is thus displaying some HTML string from a managed bean unescaped. For that you should use <h:outputText> with escape="false".
<h:outputText value="#{daBean.markedUpString}" escape="false" />
See also:
Getters inside f:verbatim called before form submission
JSF/Facelets: why is it not a good idea to mix JSF/Facelets with HTML tags?
What are the main disadvantages of Java Server Faces 2.0?

Resources