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