2014-02-20 67 views
0

我使用gsp模板渲染XML,一切正常,但自閉標籤被轉換爲空標籤。例如,如果我的模板具有以下標籤:將gsp變換自關閉標籤渲染爲空標籤

<Custom:tag /> 

,當我在控制器上渲染:

String xml = g.render template:template, model: model 

xml變量的值是

<Custom:tag></Custom:tag> 

它仍然是一個有效的XML,但是當我嘗試發送到無法控制的服務器時,由於Custom:tag,我得到驗證錯誤。

如何防止Grails解析這個標籤?

+0

的Grails有一個良好的API來渲染XML,所以模板是不最好的方式來做到這一點。你有沒有[嘗試過](http://grails.org/doc/latest/ref/Controllers/render.html)? –

+1

你應該真的使用supercool groovy xml構建器,你在做什麼是一個混亂 –

+0

我考慮使用模板,因爲我想支持許多XML,但[Groovy XML Builder](http://groovy.codehaus.org/Creating+XML+使用+ Groovy's + MarkupBuilder)絕對是最好的選擇。 –

回答

1

在這裏你有一個簡單的例子,你怎麼能輕易呈現XML

def renderXml() { 

     render(contentType: 'text/xml') { 
      startTag(version: '1.2', state:'FeelGood') { 
       childTag(value:"this is a value") 
       nestingFun(howmuchisthefish:"42"){ 
        childTag(value:"this is a value") 
        childTag(value:"this is a value") 
       } 

      } 
     } 

    } 

這將導致類似的東西像

<?xml version="1.0" encoding="UTF-8"?> 

<startTag version="1.2" state="feelGood"> 
     <childTag>"this is a value"</childTag> 
     <nestingFun howmuchisthefish="42" > 
      <childTag>"this is a value"</childTag> 
      <childTag>"this is a value"</childTag> 
     </nestingFun> 
</startTag> 
+0

感謝您的評論!我無法呈現自封閉的標籤,但在閱讀您的評論後,我得到了答案:selfClosedTag() – Vladimir