2015-05-14 2 views

ответ

2

Да, вы можете легко это сделать. Здесь вы идете:

import grails.gsp.PageRenderer 

class MyLib { 

    static namespace = "foo" 
    static defaultEncodeAs = "raw" 

    PageRenderer groovyPageRenderer 

    def externalTemplate = { attrs, body -> 
     String externalFilePath = attrs.externalPath 

     /* 
     * Put content of that external template to a file inside grails-app/views folder 
     * with a temporary unique name appended by current timestamp 
     */ 
     String temporaryFileName = "_external-" + System.currentTimeMillis() + ".gsp" 

     File temporaryFile = new File("./grails-app/views/temp/$temporaryFileName") 

     /* 
     * Copy content of external file path to the temporary file in views folder. 
     * This is required since the groovyPageRenderer can compile any GSP located inside 
     * the views folder. 
     */ 
     temporaryFile.text << new File(externalFilePath).text 

     /* 
     * Now compile the content of the external GSP code and render it 
     */ 
     out << groovyPageRenderer.render([template: "/temp/$temporaryFileName", model: attrs.model]) 

     // Delete the file finally 
     temporaryFile.delete() 
    } 
} 

Теперь в Вашем GSP, где вы хотите, чтобы включить внешний GSP, вы можете написать так:

<body> 
    <foo:externalTemplate externalPath="/home/user/anyExternalFile.gsp" model="${[status: 1}" /> 
</body> 
+0

Спасибо за ваш answer.I закончил с использованием ajax.load для загрузки содержание. Но ваше решение также будет работать. Будете иметь в виду это – Rradhak