简体   繁体   中英

Grails 2.3.7 redirecting controller action with flash.message, i18n and html markup

I use Grails 2.3.7 and want to understand the following: I have a controller which sets a flash.message object with HTML markup from an i18n file (messages.properties) and args with no HTML markup. When I call render, the corresponding GSP renders as expected, with HTML markup. When I call a redirect to another action, that does exactly the same (calling render), the rendered GSP shows the escaped HTML markup. So what happens to the flash variable during a redirect? A simple println shows no difference. For reference, here is my code:

Controller:

class DebugController {

    def testFlashEncoding(){
        flash.message=message(code: "debug.flash.test", args: ["inner text with no markup"]) //debug.flash.test=<div style="border:1px solid red;">{0}</div>
        flash.each {
            println("flash in testFlashEncoding(): " + it)
        }
//        redirect(action: "index") //redirects to index(), html is rendered as text
        render view: "testFlashEncoding" //renders testFlashEncoding.gsp, html is interpreted
    }

    def index(){
        flash.each {
            println("flash in index(): " + it)
        }
        render view: "testFlashEncoding" //renders testFlashEncoding.gsp, html is rendered as text

    }
}

GSP:

<!DOCTYPE html>
<html>
<head>
    <meta name="layout" content="main">
    <title>DEBUG</title>
</head>

<body>
<g:if test="${flash.message}">
    <div class="alert alert-success" role="status">${flash.message}</div>
</g:if>
<g:if test="${flash.error}">
    <div class="alert alert-danger" role="status">${flash.error}</div>
</g:if>
</body>
</html>

So when I call the testFlashEncoding() action as shown above, the GSP is shown with a red border around the simple text. But when I use the redirect to the index() action instead, the GSP shows <div style="border:1px solid red;">inner text with no markup</div> . The println statements show the expected values:

flash in testFlashEncoding(): message=<div style="border:1px solid red;">inner text with no markup</div>

flash in index(): message=<div style="border:1px solid red;">inner text with no markup</div>

So the content of flash is not touched, but somehow during the redirect Grails has some kind of switch that tells the rendering engine to escape the flash.message object.

For clarification, I use a freshly setup standard grails application, so my page codec ist set to grails.views.default.codec = "html" and I do know about the possibilities to alter the page codec or to force raw rendering, I just want to know what happens during the redirect (that scenario happens quite often in my current project).

Try either one of these:

${raw(flash.message)}

<%= flash.message  %>

Worked for me. It printed correct marked up HTML

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM