I'm trying to format a string in a gsp in grails, but in not getting the result that I want. I just look at this answer here ( How to show String new lines on gsp grails file? ) but is not working...
This is the string that I'm getting and how show in the view:
{ "name":"john", "surname": "Blat", "age": 11, "width": 30, "width": 600, "height": 150}
This is that I want to get it:
{ "name":"john",
"surname": "Blat",
"age": 11,
"width": 30,
"width": 600,
"height": 150}
This is my code of gsp:
<g:if test="${myInstance?.person}">
<li class="fieldcontain">
<span id="person-label" class="property-label"><g:message code="myInstance.person.label" default="person" /></span>
<span class="property-value" aria-labelledby="person-label"><g:fieldValue bean="${myInstance}" field="person"/></span>
</li>
</g:if>
This is one way that I'm trying but dont do nothing (.replace('\\n','
'):
<g:if test="${myInstance?.person.replace('\n','<br>')}">
<li class="fieldcontain">
<span id="person-label" class="property-label"><g:message code="myInstance.person.label" default="person" /></span>
<span class="property-value" aria-labelledby="person-label"><g:fieldValue bean="${myInstance}" field="person"/></span>
</li>
</g:if>
This is another way, with pre tab:
<g:if test="${myInstance?.person}">
<li class="fieldcontain">
<span id="person-label" class="property-label"><g:message code="myInstance.person.label" default="person" /></span>
<pre>
<span class="property-value" aria-labelledby="person-label"><g:fieldValue bean="${myInstance}" field="person"/>
</pre></span>
</li>
</g:if>
If the encodeAsHtml
isn't working, that means there are no \\n
between attributes. This code should do the trick (but you will have to tweak it to align your elements) :
myInstance?.person.replaceAll(',\s\"', ',<br/>\s\"')
But, this is a dirty way ! You should try to change your string format source to something exploitable by the encodeAsHtml ;)
I try this in my gsp. Of course I already have '\\n' in db
${myobjectinstance?.myfield.encodeAsHTML().replaceAll('\n', '<br/>') }
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.