Grails TagLib encoding different when running on Tomcat 9

59 Views Asked by At

I've recently upgraded a Grails 2 application to Grails 5 and have one outstanding problem before I'm ready to put this into our test environment.

We call the

g.render(template: template, model: model)

method where the template is just a string reference to a GSP, and the model is a Map<String, String> that is used to populate that GSP.

Behaviour differs between local run-app (in IntelliJ IDEA) and Tomcat 9 on Ubuntu, in a way that breaks the view referenced in template.

The crux of the matter is; given the exact same inputs to the g.render() method, the outputs differ like:

IntelliJ IDEA run-app:

'EUR (MSRP: 85.34, Range from 79.90 - 106.90, Avg.: 88.94)'

Tomcat 9:

'EUR \u0028MSRP: 85.34\u002c Range from 79.90 - 106.90\u002c Avg.: 88.94\u0029'

There is obviously some character encoding issue going on, but I'm not sure how to override the behaviour of Tomcat or the deployed WAR to make g.render() consistent with local grails run-app.

We already have this config in our Grails 5 YML:

    views:
        default:
            codec: none # none, html, base64
        gsp:
            encoding: UTF-8
            codecs:
                expression: none
                scriptlet: none
                taglib: none
                staticparts: none

What could be the reason for this change in behaviour when running under Tomcat?

Thanks in advance for any suggestions.

1

There are 1 best solutions below

0
ionised On

Answered my own question.

My particular issue is related to RenderTagLib encoding, and the behaviour seems to differ between local/dev execution and other environments (like production) like I found in this other question (unfortunately after I had spent days searching for a solution, ultimately successfully):

Grails GSP expressions encoded only in production

Anyway, in dev the application worked as expected and

g.render(template: template, model: model)

worked as expected. However in TEST/PROD environments this same function would return a character stream that was full of escape sequences.

The solution was to call the method like so:

g.render(template: template, model: model, encodeAs: 'raw')

which gave me the desired behaviour in any environment. Initially I tried this, unsuccessfully:

views:
    default:
        codec: none # none, html, base64
    gsp:
        encoding: UTF-8
        codecs:
            taglib: raw   

Which did fix part of my application, but was too broad a solution and broke other things.

The first example I gave above g.render(encodeAs: 'raw') needed to be used selectively.

Again, very annoying that this behaviour changes when non-dev profiles are used. I couldn't find any documentation explaining it either.