在控制器中,如何将 json 视图输出重定向到文件而不是 http 响应?
圣杯 3.2.5。
另一个简单的选择是:
def action() {
def json = [ key1:'value1', key2:[ key21:'value21' ]
new File( '/the/path' ).withOutputStream{ it << ( json as JSON ) }
[ some:result ]
}
你可以做这样的事情......
@Autowired
JsonViewTemplateEngine templateEngine
void myMethod() {
Template t = templateEngine.resolveTemplate('/book/show')
def writable = t.make(book: new Book(title:"The Stand"))
def fw = new FileWriter(...)
writable.writeTo( fw )
...
}