2016-03-08 63 views
0

我想格式化gsp中的字符串在grails中,但沒有得到我想要的結果。我只是在看這個答案在這裏(How to show String new lines on gsp grails file?),但不工作...如何在gsp grails中設置格式字符串?

這是我得到和字符串如何顯示視圖:

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

回答

1

如果encodeAsHtml不工作,這意味着有屬性之間沒有\n。此代碼應該做的伎倆(但你將不得不調整它來對齊你的元素):

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

但是,這是一個骯髒的方式!你應該嘗試你的字符串格式的源改爲由encodeAsHtml利用的東西)

0

我嘗試這在我的GSP。當然,我已經有「\ n」在DB

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

我加入這個,不要做什麼... \t \t \t ')}「> –

+0

右鍵單擊頁面並查看源代碼。你看到'
'嗎?如果不是那麼它可能就是你在那裏沒有'\ n'的地方。檢查控制器,看看你是否在字符串中有'\ n'。另外一個測試是用'
'替換任何字符,確保它能正常工作。 – Arjang

相關問題