Kshitij Bajracharya
Kshitij Bajracharya

Reputation: 851

Replace text returned from messages.properties in gsp

I have a text in gsp as follows: <g:message code="${code}" locale="${locale}"/>

The problem is the message returned can contain characters such as & and when exporting this gsp to pdf, the following error occurs:

Caused by: org.xml.sax.SAXParseException; lineNumber: 396; columnNumber: 44; The entity name must immediately follow the '&' in the entity reference.
    at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
    at org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:485)

This issue could be resolved if & could be replaced by &amp;

I have tried using g:encodeAs but that didn't help. I cannot change the & in messages.properties to &amp; or and.

Is it even possible to replace a text returned from messages.properties before rendering in gsp?

Upvotes: 2

Views: 182

Answers (1)

Kshitij Bajracharya
Kshitij Bajracharya

Reputation: 851

I solved the problem using the following code:

${JsonUtil.parseTextForXhtml(message(code:code, locale: locale))}

And then in JsonUtil, I replaced & with &amp; as follows:

static String parseTextForXhtml(String text) {
    text.replaceAll("&", "&amp;")
}

Upvotes: 1

Related Questions