Using single quotes within a string in EL - jsf

I need to write this string on JSF code :
rendered="#{person.value.equals('it's him')}"
My problem is with the ' character.
Thank's for your help.

You can do the following:
<h:outputText value="My text with \'single quotes\' and \"double quotes\"" />
So:
rendered="#{person.value.equals('it\'s him')}"
Look at this:
<af:outputText inlineStyle="font-weight:bold;" value="#{viewControllerBundle.BEAN_ONLINE_VARIABLE_PASS_1}" id="ot22p_" rendered="#{empty pageFlowScope.sensor.payed and pageFlowScope.sensor.down eq 'false'}"/>

Related

Can't we give two values for one lable in JSF?

I have one label field which is license and I want give two license numbers for this..how to give two values here. I have one properties file (manageprofile) and one java class (manageprofiledatabean) which contains the license values
<h:outputLabel value="#('label.manageprofile.license')"/>
<h:outputText value="#{manageProfileDataBean.license}" />
<h:outputText escape="false" value=" "></h:outputText>
Concat both values in one string variable of manageProfileDataBean and then set it in
public class ManageProfileDataBean{
private String license = "";
private String val1 = "val1":
private String val2 = "val2";
public ManageProfileDataBean(){
license = val1+val2;
}
//setter getter
}
firstly, h:outputLabel is meant to be used for labelling input controls - it renders as the html <label> tag(e.g.)
<h:outputLabel for="myname" value="#{bean.labelValue}" />
<h:inputText id="myname" value="#{bean.name} />
If you need to specify multiple values in expression language you can do so like this:
<h:outputText value="#{bean.property1 bean.property2}"/>
so I guess in your case you would write it as
<h:outputText value="#{'label.manageprofile.license' manageProfileDataBean.license}" />
Finally, you don't need to use an h:outputText if you're not referenceing any propery so the last pair of nbsp characters don;t need to be in in outputText tag at all.

Having only a numeric in an inputText

I have a jsf problem:
I want to an "inputText" to be only a numeric. For this I use but it does not work.
When I fill the field with a word (for instance), no error message appears.
Here is my xhtml code:
<h:form id="profil">
<p:inputText value="#{effectifBean.efCadre}"
validatorMessage="#{messages['content.msg']}" >
<f:validateRegex pattern="[1-9]*" />
</p:inputText>
</h:form>
Anyone have an idea ?
Thank you for your help.
why you not use inputMask if you know the number of char inside input ?
<p:inputMask value="#{effectifBean.efCadre}" mask="99999"/>

Output curly brace in JSF h:outputText

I want to use the following EL inside a custom component:
<ui:param name="valueAfter"
value="#{not empty valueAfter ? valueAfter : false}" />
<h:outputText
value="#{x.label}#{valueAfter == true ? {x.value} : ''}" />
This does not work as I cannot output the curly braces around x.value but now I am looking for a good way to actually output those.
Try this:
<h:outputText value="#{x.label"}/>
<h:outputText value="{#{x.value}}" rendered="#{valueAfter}"/>
Here you go -In case you are using EL 2 (I think), one line with one h:outputText solution thanks to the .concat()
<h:outputText value="#{x.label}#{valueAfter == true?'{'.concat(x.value).concat('}'):''}" />

Concatenating strings within EL expression defined in an attribute of a facelets tag

I need to write an EL expression for an attribute which goes something like this:
#{cc.attrs.appreciatedByCurrentUser ? (cc.attrs.count +'<br/>'+ (cc.attrs.count-1)) : ((cc.attrs.count+1) +'<br/>'+ cc.attrs.count)}
Now the problem is that this gives an error as strings cannot be concatenated, the way I am doing it. So how can I rectify this?
I'm using JSF 2.0 with facelets.
EDIT :
I'm resolving the issue using the following inline javascript
<script type="text/javascript">
var count=#{cc.attrs.count};
document.write(#{cc.attrs.appreciatedByCurrentUser} ? (count-1) +'<br/>'+count : count+'<br/>'+ (count+1));
</script>
Can you think of any issue with this?
It is possible to concatenate Strings in EL using the java.lang.String.concat(String) method. Thus your code could look like:
<h:outputText value="#{cc.attrs.appreciatedByCurrentUser ? (''.concat(cc.attrs.count).concat('<br/>').concat(cc.attrs.count-1)) : (''.concat((cc.attrs.count+1)).concat('<br/>').concat(cc.attrs.count))}" escape="false" />
In this particular case however I would go with one of the options that Mr BalusC had suggested because the code above doesn't look quite elegant. In some cases knowing this technique could be useful, though.
I would hardly recommend using javascript as a workaround here.
String concatenation in EL is only possible by just inlining in the expression. The + operator is in EL exclusively a sum operator. Further, < and > are invalid characters in XML attributes, so you have to escape them (and instruct <h:outputText> to not escape them once again by escape="false"):
<h:outputText value="#{cc.attrs.count}<br/>#{cc.attrs.count-1}" escape="false" rendered="#{cc.attrs.appreciatedByCurrentUser}" />
<h:outputText value="#{cc.attrs.count+1}<br/>#{cc.attrs.count}" escape="false" rendered="#{!cc.attrs.appreciatedByCurrentUser}" />
Alternatively, you can also use <c:set> to alias the expression:
<c:set var="appreciated" value="#{cc.attrs.count}<br/>#{cc.attrs.count-1}" />
<c:set var="notAppreciated" value="#{cc.attrs.count+1}<br/>#{cc.attrs.count}" />
<h:outputText value="#{cc.attrs.appreciatedByCurrentUser ? appreciated : notAppreciated}" escape="false" />
This is the only thing i can come up with.
<h:panelGroup rendered="#{cc.attrs.appreciatedByCurrentUser}">
<h:outputText value="#{(cc.attrs.count)}" style="display:block;" />
<h:outputText value="#{(cc.attrs.count-1)}" />
</h:panelGroup>
<h:panelGroup rendered="#{not cc.attrs.appreciatedByCurrentUser}">
<h:outputText value="#{(cc.attrs.count+1)}" style="display:block;" />
<h:outputText value="#{(cc.attrs.count)}" />
</h:panelGroup>
Putting <br> in a value attribute will always throw errors in JSF, so you'll have to use display:block.

JSF: h:outputText; how to show a dash when the value is empty string?

I'm using h:outputText tags to display readonly data. Ex:
<h:outputText value="Phone Number:" />
<h:outputText value="#{userHandler.user.phoneNumber}" />
When "phoneNumber" is an empty string or a null, I want to display a dash "-" as the value.
Is there any easy way to do this maybe with expression language or something?
BTW, I thought about adding methods to the User class like getPhoneNumberDisplayText() that could do the check internally, but I since it's a view issue, I'd rather keep the code in the JSF page.
<h:outputText value="#{userHandler.user.phoneNumber != null
? userHandler.user.phoneNumber : '-'}" />
Or, you could make a new outputText:
<h:outputText rendered="#{userHandler.user.phoneNumber == null}" value="-" />

Resources