English 中文(简体)
Using g.render in a grails service
原标题:

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.

Any thoughts?

最佳回答

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
    }
}

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

问题回答

It s even simpler now in Grails 2 with the PageRenderer. e.g.:

class SomeService {
    def groovyPageRenderer

    void someMethod() {
        String html = groovyPageRenderer.render(view:  /email/someTemplateName )
    }
}

API - http://grails.org/doc/latest/api/grails/gsp/PageRenderer.html

More complete example - http://mrhaki.blogspot.com/2012/03/grails-goodness-render-gsp-views-and.html

My advice would be to do this in the controller. Service should have reusable logic and not depend on a view template, leave that work to the controller. Use the service to get the data you need to pass to the template, but leave the work of interacting with the template to the controller.

Here s a solution that s similar to Stefan s, but a bit simpler

import org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib
import org.springframework.context.ApplicationContext
import org.springframework.context.ApplicationContextAware

class MyService implements ApplicationContextAware {

    private ApplicationTagLib g

    void setApplicationContext(ApplicationContext applicationContext) {
        g = applicationContext.getBean(ApplicationTagLib)

        // now you have a reference to g that you can call render() on
    }
}




相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

How can i update div continuously

I have asp.net application where i have a div which showing the value from other site. The value of that site is changing continuously. I want that my div will automatically update in some interval ...

热门标签