【问题标题】:Cannot render form tag using 'render' in Grails Controller无法在 Grails 控制器中使用“渲染”来渲染表单标签
【发布时间】:2009-10-09 16:08:49
【问题描述】:

在渲染中使用标记不会添加表单标签。

我用 contentType "text/html", "txt/xml" 试过了,但它不起作用。

我的控制器中有这个:

 def myTest= {
    render(contentType: "text/plain") {
        div(id:"myDiv") {
            p "somess text inside the div"
            form (action:'get') {
                p "inside form"
            }
        }
    }

我得到了这个:

<div id='myDiv'><p>somess text inside the div</p><p>inside form</p></div>

我想要这个:

<div id='myDiv'><p>somess text inside the div</p><form><p>inside form</p></form></div>

有人知道为什么不添加表单以及如何添加吗?

谢谢,

费德里科

【问题讨论】:

    标签: grails groovy controller render


    【解决方案1】:

    我之前发现了这个问题,解决方法是直接使用构建器

    def test = {
        def sw = new StringWriter()
        def b = new MarkupBuilder(sw)
        b.html(contentType: "text/html") {
            div(id: "myDiv") {
                p "somess text inside the div"
                b.form(action: 'get') {
                    p "inside form"
                }
            }
    
        }
        render sw
    }
    

    将呈现以下 HTML

    <html contentType='text/html'>
      <div id='myDiv'>
        <p>somess text inside the div</p>
        <form action='get'>
          <p>inside form</p>
        </form>
      </div>
    </html>
    

    【讨论】:

    • Aaron 是对的,自己调用 form 可能与动态添加到所有控制器的 g:form taglib 方法发生冲突
    猜你喜欢
    • 2012-09-13
    • 1970-01-01
    • 2014-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-05
    • 2010-11-30
    相关资源
    最近更新 更多