代码之家  ›  专栏  ›  技术社区  ›  Steve Kuo

Grails“render”渲染模板

  •  11
  • Steve Kuo  · 技术社区  · 16 年前

    render

    def ajaxRandomPersonName = {
        def person = get a random person ...
        render "Name: ${person.name}"
    }
    

    渲染 渲染 只渲染而不使用模板?

    :

    def ajaxRandomPersonName = {
        def person = get a random person ...
        render (template:null, text:"Name: ${person.name}")
    }
    

    渲染

    后续行动2 : Parallel discussion

    :示例代码: 我对代码进行了最低限度的配对,但它仍然显示出不希望的模板呈现。

    class PersonController { 
        def index = { } 
        def home = { [message:"Hello"] } 
    
        def ajaxTest = { 
            println "ajaxTest called" 
            render text: "ajax message" 
        } 
    } 
    

    <html> 
    <head> 
        <title>Home View</title> 
        <g:javascript library="prototype" /> 
    </head> 
    <body> 
        <p> 
            <g:remoteLink action="ajaxTest" update="test1">ajax call</g:remoteLink> 
        </p> 
        <p>Message = ${message}</p> 
        <p id="test1">Blank</p> 
    </body> 
    </html> 
    

    <html> 
    <head> 
        <title>Test App - <g:layoutTitle/></title> 
        <g:layoutHead/> 
    </head> 
    <body> 
        <h1>Test App</h1> 
        <g:layoutBody/> 
    </body> 
    </html> 
    

    http://localhost:8080/test/person/home

    测试应用

    Test App 
    ajax call 
    Message = Hello 
    Test App 
    ajax message 
    

    <p>

    4 回复  |  直到 16 年前
        1
  •  13
  •   Steve Kuo    16 年前

    render text: "Name: ${person.name}", contentType: "text/plain"
    
        2
  •  3
  •   Oliver Weichhold    16 年前

    将[text:“名称:${person.Name}”]呈现为

        3
  •  3
  •   Shawn Flahave    14 年前

    您可能会被Grails中的“按约定布局”特性所困扰。例如,如果布局名称与控制器名称前缀匹配,Grails将把布局应用于该控制器管理的每个视图。不幸的是,它甚至适用于文本和模板。目前记录了一些与此相关的JIRA(参见 http://jira.grails.org/browse/GRAILS-7624

        4
  •  0
  •   Rhysyngsun    16 年前

    我们发现,从操作中显式返回false可以解决这个问题。

    推荐文章