h:outputText need style for particular text - jsf

I'm using Primefaces 5.1. In my student I need style particular style in particular text.Below code I try It will show error(The value of attribute "value" must not contain the '<' character). So I replace < to &< and &&gt for > that time I faces error(The entity name must immediately follow the '&' in the entity reference)
<p:outputPanel>
<h:outputText styleClass="noWrap" escape="false"
value="#{common.ConfirmMessage1} <b style='color:red'>'
#{student.numberOfUserFound}'</b>"/>
</p:outputPanel>
My doubt is separate text is solution or any other way?

Just don't use <h:outputText>. It has no technical necessity in this specific case.
<p:outputPanel>
<span class="noWrap">
#{common.ConfirmMessage1}
<b style='color:red'>'#{student.numberOfUserFound}'</b>
</span>
</p:outputPanel>
See also:
Is it suggested to use h:outputText for everything?
That being said, I suggest to take a step back from JSF and start learning some basic HTML first. JSF will then be easier to understand. In the context of this question, JSF is just a HTML code generator.

Related

I can't use varStatus value to get index in p:repeat [duplicate]

I'm trying to assign an id to a component inside a <ui:repeat> like that:
<ui:repeat value="#{bean.columns}" var="column">
<h:panelGroup layout="block" id="column_#{column.id}"
styleClass="#{column.id} dashboard_column">
The thing is that #{column.id} value is being placed properly inside the styleClass value but its not being set inside the id attribute. All that is being set inside the id attribute is the automatically generated id by the JSF + my hard coded value column_.
If I remove the hard coded column_ I get an exception:
java.lang.IllegalArgumentException: component identifier must not be a zero-length String
at
Any Ideas?
This is not possible with a render-time tag such as <ui:repeat>. The <ui:repeat> will however by itself already ensure the uniqueness of the generated client ID by prepending it with the row index. So just remove the EL part from the ID attribute of the component.
<ui:repeat value="#{bean.columns}" var="column">
<h:panelGroup layout="block" id="column">
With a view build time tag such as <c:forEach> (which will basically generate multiple <h:panelGroup> components instead of only one which is rendered multiple times), it is possible to specify a dynamic ID like that.
<c:forEach items="#{bean.columns}" var="column">
<h:panelGroup layout="block" id="column_#{column.id}">
(you should only be well aware of how JSTL works in Facelets)
An alternative is to use a static <div> element instead of a JSF <h:panelGroup layout="block"> component.
<ui:repeat value="#{bean.columns}" var="column">
<div id="column_#{column.id}">
See also:
JSTL in JSF2 Facelets... makes sense?
JSF prefixes the id automatically. If you simply write id="column" the generated HTML will contain such identifiers:
myForm:0:column
myForm:1:column
myForm:2:column
and so on.
Anyway: Do never use JSTL tags (like c:foreach and c:if) in JSF templates. They cause random behaviour, very difficult to debug. And if they work, the slow down the application a lot.
Use ui:repeat for loops, and ui:fragment for conditional blocks. Note that there is no replacement for c:set, such a construct does not exist anymore in JSF 2.

JSF id value to the component behaves different

I have the following code in my xhtml
<h:form id="xyForm">
<p:dataGrid id="xyGrid" ....>
<p:selectOneMenu id="code" ...> </p:selectOneMenu>
</p:dataGrid>
</h:form>
But when I saw the code generated, it looks like the following
<select name="xyForm:xyGrid:0:code_input" tabindex="-1" id="xyForm:xyGrid:0:code_input"> </select>
My question here is: why _input is getting appended with name and id.
As per my understanding id should be only xyForm:xyGrid:0:code not with appended _input. Could someone please clarify or tell me how to remove that _input?
An id attribute should be unique within an html page.
While rendering the SelectOneMenu, the select tag is wrapped inside a div tag. The div tag will have the id of the component i.e. xyForm:xyGrid:0:code, and so it makes sense that the select tag should have a different id.
Also, this is a common pattern in Primefaces, observed in other components like SelectBooleanCheckbox etc.
Instead of trying to removing _input, you will have to figure around how to work around it.

Conditionally render a <div> based on value of <h:outputText>

I am using jsf primefaces. I want to display an image depending on the value of a specific outputext. If the text value is 'Alarm *' then a div will appear whith a spesific image. If the value is 'Alarm **' then a div with an other image will appear, etc. I tried the code below but it does not work for me.
<h:outputText id="alarmCriticalityValue" value="#{msg[summary.criticality.key]}" />
<c:if test="#{alarmCriticalityValue=='Alarm *'}">
<div class="alarm1"></div>
</c:if>
How should i implement this idea?
You need to use binding attribute to put the UIComponent instance in the EL scope. The id attribute doesn't do that, on contrary to what you expected.
<h:outputText binding="#{alarmCriticality}" ... />
And then you need to use UIOutput#getValue() to obtain its value attribute.
<c:if test="#{alarmCriticality.value == 'Alarm *'}">
That said, you'd better use rendered attribute here, particularly if #{summary} represents the currently iterated item of a JSF iterating component like <ui:repeat> or <h:dataTable>.
<h:panelGroup layout="block" styleClass="alarm1"
rendered="#{alarmCriticality.value == 'Alarm *'}" />
See also:
How does the 'binding' attribute work in JSF? When and how should it be used?
How to conditionally render plain HTML elements like <div>s?
JSTL in JSF2 Facelets... makes sense?
Unrelated to the concrete problem. It's strange to see the conditional rendering depend on localized text. What if you change the locale and/or the localized text? This is very brittle. You'd better check the bundle key instead.
<h:outputText value="#{msg[summary.criticality.key]}" />
<h:panelGroup layout="block" styleClass="alarm1"
rendered="#{summary.criticality.key == 'some.alarm.key'}" />
This way you also don't need to bind the output text anymore.
Try
<c:if test="#{msg[summary.criticality.key].equals('Alarm *')}">
Or add a binding to the h:outputText and check against that.
Try this
<h:outputText id="alarmCriticalityValue" value="#{msg[summary.criticality.key]}" />
<h:panelGroup layout="block" styleClass="alarm1" rendered="#{alarmCriticality.value eq 'Alarm *'}" />

EL expression in c:choose, can't get it working

I'm trying something (JSF2) like this:
<p>#{projectPageBean.availableMethods}</p>
<c:choose>
<c:when test="${projectPageBean.availableMethods == true}">
<p>Abc</p>
</c:when>
<c:otherwise>
<p>Xyz</p>
</c:otherwise>
</c:choose>
But this doesn't seem to work, although the EL expression in the top paragraph changes from false to true, the next paragraph always shows Xyz?
I also tried to change the test to:
${projectPageBean.availableMethods}
But still the same problem!
First and foremost: JSTL tags runs during view build time, not during view render time.
Your concrete problem suggests that #{projectPageBean} is been set during view render time, such as would happen when definied as <ui:repeat var>, <h:dataTable var>, <p:tabView var>, etc. It's thus null during view build time.
In that case, you should not be using a view build time tag to conditionally render HTML. You should instead use a view render time component to conditionally render HTML. As first choice, use <ui:fragment>:
<p>#{projectPageBean.availableMethods}</p>
<ui:fragment rendered="#{projectPageBean.availableMethods}">
<p>Abc</p>
</ui:fragment>
<ui:fragment rendered="#{not projectPageBean.availableMethods}">
<p>Xyz</p>
</ui:fragment>
By the way, there's in Facelets no need to switch between #{} and ${}. In contrary to JSP, in Facelets the ${} behaves exactly the same as #{}. To avoid potential confusion and maintenance trouble, I recommend to stick to #{} all the time.
See also:
Conditional rendering of non-JSF components (plain vanilla HTML and template text)

JSF, how to use EL in onclick

I want to create links using database columns. I have a backing bean where I 'm connecting to the database. There is no problem with the connection and also no problem with the links names. I can see my links on my browser. I want to use onclick function and that's exactly where the problem starts. How can I use or can I use EL in onclick?
A little example:
<h:dataTable rows="7" value="#{frontSiteMenu.links}" var="row"
styleClass="sitemenu" width="200">
<h:column>
<h:outputText value='#{row.newsGroup}' />
</h:column>
</h:dataTable>
Thanks.
I take it you are using JSPs?
Use h:outputLink instead of an a tag and change the expression use the # character:
<h:outputLink value="#" onclick="dispNewsGroup('#{row.newsGroupId}')">
<h:outputText value='#{row.newsGroup}' />
</h:outputLink>
That is untested, but should be close to what you want.
The spec says this about # vs $:
...by convention the J2EE
web tier specifications use the
${expr} construct for immediate
evaluation and the #{expr} construct
for deferred evaluation.
So, in a repeat control where the underlying values change, it is desirable to use deferred evaluation.
There are also issues with using non-JSF tags as children of some JSF controls, so it is best to stick to using JSF controls where possible (though there is a f:verbatim tag). Many of these issues go away if you move to the newer Facelets view technology.

Resources