Component in JSF that preserve new lines and whitespaces [duplicate] - jsf

This question already has an answer here:
h:outputText seems to trim whitespace, how do I preserve whitespace?
(1 answer)
Closed 7 years ago.
Mojara 2.1.21, Primefaces 3.5, Omnifaces 1.5
Is it possible in JSF to use a component that preserve whitespaces and new lines ?
I have something like that:
<c:forEach items="{bean.items}" var="item">
<h:outputText value="#{item.text}" />
<p:inputText value="#{item.getText[item.id]}" />
</c:forEach >

Instead of staring at JSF source code, you should first investigate how the generated HTML output should look like in order to achieve the concrete UI requirement. In plain HTML terms, you can achieve that by either putting the text content in a HTML <pre> element, or to apply CSS white-space:pre style on the parent element.
None of them have a JSF component equivalent. But that's also not necessary. You can just write HTML and CSS the usual way in JSF.
<pre><h:outputText value="#{item.text}" /></pre>
or
<h:outputText value="#{item.text}" style="white-space: pre;" />
(note: above example is intented as kickoff, normal practice is to use a normal CSS class)

Related

Turn a facelet tag into composite component

Some time ago, based on one of #BalusC answers (really don't know which one of them) I wrote a facelet tag that is a kind of a button generator, that generates a variable number of buttons disposed side-by-side, each one with some parameters passed through the attributes.
Now I want to turn the facelet tag into a composite component (because on the latter, I have the cc:interface tag, that provides information to others developers who wants to use the custom component).
In the rewriting process I must deal with the actions attributes of the dynamically generated buttons. In the facelet tag approach I don't have to declare them as methods, so I can pass their names as simple strings and process them inside the facelet tag also as simple strings and call the action methods. In the composite component approach it seems that I must pass the methods names as actual methods names (by using the method-signature attribute of cc:attribute). The problem is: as I don't know previously the number of buttons (it generates the buttons dinamically), I'm not able to declare the action methods in the cc:interface section.
A simplified version of my working facelet tag is:
<c:set var="idValues" value="#{fn:split(ids,',')}" />
<c:set var="actionValues" value="#{fn:split(actions,',')}" />
<c:set var="numberOfButtons" value="#{fn:length(idValues)}" />
<h:panelGrid id="#{id}" columns="#{numberOfButtons}">
<c:forEach begin="0" end="#{numberOfButtons-1}" varStatus="i">
<p:commandButton id="#{idValues[i.index]}"
action="#{bean[actionValues[i.index]]}" />
</c:forEach>
</h:panelGrid>
and the facelet tag can be used like this:
<my:button id="idButton"
ids="btnSave,btnCancel"
actions="onSave,onCancel"
bean="#{myBean}" />
[EDIT]
As sugested by #BalusC in the answers, adding code like below in the taglib.xml file was enough for the Eclipse autocompletion to work fine:
<tag>
<tag-name>button</tag-name>
<source>tags/bottons.xhtml</source>
<attribute><name>id</name></attribute>
<attribute><name>ids</name></attribute>
<attribute><name>actions</name></attribute>
<attribute><name>bean</name></attribute>
</tag>

h:outputText need style for particular text

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.

Setting a variable in JSF [duplicate]

This question already has an answer here:
How to use Parameterized MessageFormat with non-Value attributes of JSF components
(1 answer)
Closed 6 years ago.
I have something like this in my jsf:
<div title="Percentage: #{myBean.numberToBeConvertedToPercentage}"></div>
I wanted to make something like this:
<h:outputText value="#{myBean.numberToBeConvertedToPercentage}">
<f:convertNumber maxFractionDigits="2" type="percent"/>
</h:outputText>
Of course, not inside an output text, but instead setting the converted number to a variable, so I can use it inside my div.
I don't want to take this converted number direct from my bean, since I use it in another places of my view without formatting it, and creating a get just for this, I don't like the idea. There is any way to make this only in my view?
To go with <h:outputText> + converter, it is the best approach for this purpose in JSF. You can redesign your Html to separate the label "Percentage: " and the data. to use rendered value in JS code to pass it to 'title' attribute.
<div id="titleDiv" />
<script type="text/javascript">
$('#titleDiv').title = 'Percentage: <h:outputText value="#{myBean.numberToBeConvertedToPercentage}"><f:convertNumber maxFractionDigits="2" type="percent"/></h:outputText>';
</script>
If you still want to stay without converters, then you can create one more bean per your view and render values in get methods.
Other option is to use EL 2.2 Method call feature and to call your own format bean
<div title="Percentage: #{formatBean.formatNumber(myBean.numberToBeConvertedToPercentage, 2)}"></div>
I have not checked this, but maybe you can use ui:param and assign a converter to it.
and then use it in your page
<div title="Percentage: #{myVar}></div>
see Defining and reusing an EL variable in JSF page

What tag to use to hide content in JSF

When I want to hide some content in JSF, what tag is made for this purpose? There are several tags that can do the job:
<f:subview rendered="#{...condition...}" />
and
<c:when test="#{...conditon...}" />
Which is the right one to use?
in JSF, using rendered is the best approach.
Using JSTL tags like <c:when>, is not recommended at all, and even break some functunality of JSF like ViewScope annotation. Always try to use JSF tags (like ui:repeat instead of c:forEach)
<ui:remove>
Look here: http://www.jsftoolbox.com/documentation/facelets/10-TagReference/facelets-ui-remove.html
UPDATE
If you want to conditionally hide some content, you can use
<h:panelGroup rendered="#{...condition...}">
It renders as <span>, you can also add attribute layout="block"
<h:panelGroup rendered="#{...condition...}" layout="block">
to render it as <div>.

Jsf control that format text with html tags [duplicate]

This question already has an answer here:
Component to inject and interpret String with HTML code into JSF page
(1 answer)
Closed 6 years ago.
Good morning all.
Is there any jsf control that escapes the html tags?
Imagine that i have the following string in resources:
text.String=lalala<br/>lelele
and i want to print it on Xhtml file with a simple control like:
<h:outputText value="#{messages['text.String']}" />
how do i get the result formatted with the html <br/> tag?
Result should be:
lalala
lelele
instead of:
lalala<br/>lelele
Thanks
the outputText control has an 'escape' property which controls that behaviour.
See here (outputText reference).
So basically:
<h:outputText escape="false" value="#{messages['text.String']}" />
should do the job.

Resources