如何在 gsp grails 中设置字符串格式?

How to set format a string in gsp grails?

我试图在 grails 的 gsp 中格式化字符串,但没有得到我想要的结果。我只是在这里查看这个答案 () 但没有用...

这是我得到的字符串以及在视图中的显示方式:

{ "name":"john", "surname": "Blat", "age": 11, "width": 30, "width": 600, "height": 150}

这个是我想得到的:

  { "name":"john", 
    "surname": "Blat", 
    "age": 11, 
    "width": 30, 
    "width": 600, 
    "height": 150}

这是我的 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>

这是我正在尝试但什么都不做的一种方式 (.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>

这是另一种方式,带有前置标签:

<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>

我在我的 gsp 中尝试了这个。当然我已经在 db

中有了 '\n'
${myobjectinstance?.myfield.encodeAsHTML().replaceAll('\n', '<br/>') }

如果 encodeAsHtml 不起作用,这意味着属性之间没有 \n。这段代码应该可以解决问题(但您必须调整它以对齐您的元素):

myInstance?.person.replaceAll(',\s\"', ',<br/>\s\"')

但是,这是一种肮脏的方式!您应该尝试将您的字符串格式源更改为 encodeAsHtml 可以利用的内容;)