在一个Grails服务中使用g.render [英] Using g.render in a grails service

查看:162
本文介绍了在一个Grails服务中使用g.render的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用g.render在Grails的服务,但现在看来,g的默认情况下不提供服务。有没有办法让模板引擎渲染服务中的观点?我可能要对这个错误的方式。我在找的就是呈现从局部模板为字符串的看法,并发送所产生的字符串返回以与AJAX的更新使用JSON响应的一部分。

I'm trying to use g.render in a grails service, but it appears that g is not provided to services by default. Is there a way to get the templating engine to render a view in the service? I may be going about this the wrong way. What I'm looking to to is render the view from a partial template to a string, and send the resulting string back as part of a JSON response to be used with AJAX updates.

有什么想法?

推荐答案

我完全同意约翰的论证 - 做GSP服务通常是一个糟糕的设计决策。但是,如果没有异常没有规矩!如果您仍然想这样做,请尝试以下方法:

I totally agree with John's argumentation - doing GSP in services is generally a bad design decision. But no rules without exceptions! If you still want to do this, try the following approach:

class MyService implements InitializingBean {
    boolean transactional = false
    def gspTagLibraryLookup  // being automatically injected by spring
    def g

    public void afterPropertiesSet() {
        g = gspTagLibraryLookup.lookupNamespaceDispatcher("g")
        assert g
    }

    def serviceMethod() {    
       // do anything with e.g. g.render
    }
}

在服务中使用的gspTagLibraryLookup豆你当然可以访问所有其他所需的标记库。

Using the gspTagLibraryLookup bean you can of course access every other desired taglib in a service.

这篇关于在一个Grails服务中使用g.render的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆