calling String.replace(CharSequence, CharSequence) in title attribute yields weird result

UI Components for JSF
Post Reply
rosière
Posts: 92
Joined: 28 Nov 2012, 15:55
Contact:

20 Feb 2018, 16:41

Hello everyone,
I am trying to display parametrized message in <p:input> 's title attribute:

Code: Select all

  <p:inputText  ** title="#{msg['m_item_x'].replace('{0}', '1')}" >                         
The message is defined in bundle as:

Code: Select all

m_item_x=Item {0}
I would like to get a tip title as "Item 1" but I finally got "Item 10}" in my Firefox & Chrome browsers.

I also tried JSTL's fn tag:

Code: Select all

 fn:replace(msg['m_item_x'], '{0}', '1')
 
But it resulted in the same weird text.

May anyone tell me what went wrong? Thanks. Danke.
JDK: 1.8
Operating system: Windows 10
server: 4.1.1.171 #badassfish (build 136)
IDE: NetBeans 11.2
primefaces : version 6.0
primefaces extensions : 4.0.0
Apache Poi: 3.15

Jan Eckert
Posts: 84
Joined: 11 Sep 2014, 10:13
Location: Brussels, Belgium

21 Feb 2018, 12:35

String replacement with curly braces leads to very astonishing results. To make it work you have to double-escape them. This will work

Code: Select all

	<p:inputText  title="#{ fn:replace(msg['m_item_x'], '\\{0\\}', '1') }" />
Unrelated to the solution you might want to consider to change your approach. I would de-parameterize your message (remove the {0} part) and work with simple String concatenation in the EL expression (if you are using EL 3.0+). But I know only this small fraction of your usecase, maybe you have good reasons for this.

Please note that your question is not Primefaces-related and would have been better posted at Stackexchange.
Primefaces 6.1+
Wildfly 11

tandraschko
PrimeFaces Core Developer
Posts: 3979
Joined: 03 Dec 2010, 14:11
Location: Bavaria, DE
Contact:

21 Feb 2018, 13:47

My opinion: do the replace stuff in a controller bean, not via functions ;)
Thomas Andraschko

PrimeFaces | PrimeFaces Extensions

Apache Member | OpenWebBeans, DeltaSpike, MyFaces, BVal, TomEE

Sponsor me: https://github.com/sponsors/tandraschko
Blog: http://tandraschko.blogspot.de/
Twitter: https://twitter.com/TAndraschko

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 33 guests