How to use fn:replace() to replace one doublequote by two doublequotes - jsf-2

In my JSF application I am trying to use fn:replace() to replace " by "". I tried the following:
<h:outputText value="#{fn:replace(str, '\"', '\"\"')}" />
However, it causes a XML parsing exception:
javax.faces.view.facelets.FaceletException: Error Parsing /test.xhtml: Error Traced[line: 20] Element type "h:outputText" must be followed by either attribute specifications, ">" or "/>".
The same code is working for other charecters like the below one:
<h:outputText value="#{fn:replace(str, 'a', 'b')}" />
How can I replace a doublequote by two doublequotes using fn:replace()?

Indeed, this is nasty. The " has special treatment in XML. It represents the start and end of an attribute value. Your best bet is to parameterize it into another variable with help of <c:set>.
<c:set var="doublequote" value='"' />
<c:set var="twodoublequotes" value='""' />
<h:outputText value="#{fn:replace(bean.string, doublequote, twodoublequotes)}" />

Related

Does not work properly id generation on ui:repeat My faces 2.2.9 + prime faces 5.3

Actually I'm working with Myfaces version 2.2.9 and I've the following structure to generate any panel according with a specific number selected by the user.
...
<ui:repeat value="#{garajes}" var="garaje" varStatus="loop">
<p:panelGrid >
<h:outputLabel value="Numero de garaje #{loop.index+1}: " />
<h:outputLabel value="Matricula #{loop.index+1}: " />
<p:inputText value="#{garaje.numeroGaraje}" maxlength="5" >
</p:inputText>
<p:inputText id="matriculaInmobiliariaGaraje-#{loop.index+1}" value="#{garaje.matriculaInmobiliaria}"
maxlength="20">
</p:inputText>
...
</p:panelGrid>
</ui:repeat>
....
So, when is rendered the above code the identifiers are weird, has another things like the following image:
So I don't know how to remove this weird things inside of id
Note: I need a specific id to update another component inside of the loop.
What can I do to get a right identifiers inside of ui:repeat?
As to the concrete problem, just give all NamingContainer components a fixed ID. This includes the <ui:repeat> itself.
<ui:repeat id="garajes" ...>
As to the concrete requirement, you're overcomplicating things. A NamingContainer will all by itself worry about uniqueness of IDs of children. Your attempt in id="matriculaInmobiliariaGaraje-#{loop.index+1}" won't work at all as #{loop} variable isn't available during view build time, when the component is being instantiated with id. Get rid of it. You can inside a NamingContainer just use a relative ID to reference another component in the same NamingContainer.
<ui:repeat ...>
<p:inputText id="foo" />
<p:inputText ...><p:ajax ... update="foo" /></p:inputText>
</ui:repeat>
This will work just fine.
See also:
How to find out client ID of component for ajax update/render? Cannot find component with expression "foo" referenced from "bar"
How to use EL with <ui:repeat var> in id attribute of a JSF component

cannot have space when exceding three integer with f:converternumber jsf2

I want to see all number like that : 123 456.67 : a space when I exceeded 3 integer and always two fractions number
I tested that :
<f:view contentType="text/html" locale="en" encoding="UTF-8">
and that on each inputText :
<p:column headerText="Ajust Prix">
<h:inputText value="#{car2.ajustPrix}" >
<f:convertNumber pattern="#0.00" maxFractionDigits="2" />
</h:inputText>
</p:column>
but I don't have the space when exceeding three integer,
how can I resolve this
The pattern attribute of <f:convertNumber> uses the java formatting patterns that can be found in javadocs for DecimalFormat. With the pattern you may specify the so-called 'grouping separator', which is what you're after. Also, it's worth reading some tutorials on the subject, like Oracle's one.
All in all, you need to use the following pattern: #,##0.00. It can be used like this:
<f:convertNumber pattern="#,##0.00" />

Primefaces crop converter error

I am using the following code and try to crop an image which takes its value from a bean :
<p:panelGrid columns="2">
<p:row>
<p:column>
<p:imageCropper id="imageCropperId" value="#{imageCropperBean.croppedImage}"
image="#{imageCropperBean.newImageName}">
</p:imageCropper>
</p:column>
<p:column>Bild innerhalb des Rahmens positionieren</p:column>
<p:column>
<p:graphicImage id="localCroppedImage" value="#{imageCropperBean.newImageName}" />
</p:column>
</p:row>
<p:row>
<p:column colspan="3">
<p:commandButton value="Crop" actionListener="#{imageCropperBean.crop}"
update=":growl imageCropperId localCroppedImage" />
</p:column>
</p:row>
</p:panelGrid>
I am always getting the error : {0}: Conversion error occurred. inside growl, due to :
value="#{imageCropperBean.croppedImage}"
The converter attribute from declaration says :
An el expression or a literal text that defines a converter for the
component. When it's an EL expression, it's resolved to a converter
instance. In case it's a static text, it must refer to a converter id.
but the EL expression is evaluated at String. What should I do? It works if I put a normal String pointing to an existing image.
I have managed to fix this error.
For the ones which will have the same problem, basically this occurs when
Either errors are being thrown during cropping phase (e.g. files not found or if the cropped image is not relative to the original one)
Either the value which is evaluated from EL image="#{imageCropperBean.newImageName}" is evaluated to null, while trying to skip the cropping (I couldn't suceed with required="true" so I used rendering="someCondition")

condition in tag xhtml with JSF2

I have this part of code using primeface component :
<h:outputText value="Division : " />
<p:inplace id="basic2">
<p:inputText value="#{utilisateursController.u1.division}" />
</p:inplace>
I want to testing if #{utilisateursController.u1.division} is equals "" (or null) it displays "Please enter a valid division" otherwise, it displays the value of #{utilisateursController.u1.division}
I have already see that somewhere but I don't know how to use
can you remember me ?
thanks
Something like this ?
<p:inputText value="#{(not empty utilisateursController.u1.division)?utilisateursController.u1.division:'Please enter a valid division'}" />

Get id of parent naming container in template for in render / update attribute

I have a template and in its Definition I use several forms and buttons.
The problem is the definition (define) xhtml file does not know the component hierarchy.
And for example I want to update the element "table2" in a different form in the same define file.
Template Insert:
<p:tabView id="nav"> <!-- nav -->
<ui:insert name="content_nav">content navigation</ui:insert>
</p:tabView>
defines the first level of my hierarchy "nav"
Template define:
<ui:define name="content_nav">
<h:form id="form1"> <!-- nav:form1 -->
<h:dataTable id="table1"/> <!-- nav:form1:table1 -->
<p:inputText value="#{bean.value}"/>
<p:commandButton action="..." update="nav:form2:table2"/>
</h:form>
<h:form id="form2">
<h:dataTable id="table2"/> <!-- nav:form2:table2 -->
<!-- other elements -->
</h:form>
</ui:define>
In my define part I don't want to know "nav"!
How can I do this? or how can I move one naming component upwards?, or save the highest parent complete id in a variable?
sometimes i saw something like:
update=":table2"
But I could not find any informations about this?, the JavaEE 6 documentation just mentions the # keywords.
Ugly, but this should work out for you:
<p:commandButton action="..." update=":#{component.namingContainer.parent.namingContainer.clientId}:form2:table2" />
As you're already using PrimeFaces, an alternative is to use #{p:component(componentId)}, this helper function scans the entire view root for a component with the given ID and then returns its client ID:
<p:commandButton action="..." update=":#{p:component('table2')}" />
ugly answer works well
update=":#{component.namingContainer.parent.namingContainer.clientId}:form2:table2
mainly more useful updating from opened dialog to parent datatable
You may use binding attribute to declare EL variable bound to JSF component. Then you may access absolute client id of this component by using javax.faces.component.UIComponent.getClientId(). See example below:
<t:selectOneRadio
id="yourId"
layout="spread"
value="#{yourBean.value}"
binding="#{yourIdComponent}">
<f:selectItems value="#{someBean.values}" />
</t:selectOneRadio>
<h:outputText>
<t:radio for=":#{yourIdComponent.clientId}" index="0" />
</h:outputText>
Try this:
<h:commandButton value="Click me">
<f:ajax event="click" render="table" />
</h:commandButton>
Additionally to the solutions above I had the problem, that I had to dynamically generate the to-be-updated components (many) based on server-side logic (with maybe harder to find out nesting).
So the solution on the server-side is an equivalent to update=":#{p:component('table2')}"1 which uses org.primefaces.util.ComponentUtils.findComponentClientId( String designId ):
// UiPnlSubId is an enum containing all the ids used within the webapp xhtml.
// It could easily be substituted by a string list or similar.
public static String getCompListSpaced( List< UiPnlSubId > compIds ) {
if ( compIds == null || compIds.isEmpty() )
return "" ;
StringBuffer sb = new StringBuffer( ":" ) ;
for ( UiPnlSubId cid : compIds )
sb.append( ComponentUtils.findComponentClientId( cid.name() ) ).append( " " ) ;
return sb.deleteCharAt( sb.length() - 1 ).toString() ; // delete suffixed space
}
called via some other method using it, e.g. like ... update="#{foo.getCompListComputed( 'triggeringCompId' )}".
1: first I tried without too much thinking to return public static String getCompListSpaced0() { return ":#{p:component('table2')}" ; } in an ... update="#{foo.getCompListSpaced0()} expression, which of course (after thinking about how the framework works :) ) is not resolved (returned as is) and may cause the issues with it some users experienced. Also my Eclipse / JBoss Tools environment suggested to write :#{p.component('table2')} ("." instead of ":") which did not help - of course.

Resources