Using g.render in a grails service

后端 未结 4 385
感动是毒
感动是毒 2020-12-08 21:58

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

4条回答
  •  萌比男神i
    2020-12-08 22:45

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

提交回复
热议问题