繁体   English   中英

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

[英]How to set format a string in gsp grails?

我试图在grails中的gsp中格式化字符串,但是没有得到我想要的结果。 我只是在这里看这个答案( 如何在gsp grails文件上显示String新行? ),但是不起作用...

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

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

这是另一种方式,带有pre选项卡:

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

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

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

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

我在我的gsp中尝试了这个。 当然我已经在数据库中有“ \\ n”

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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