Using g.render in a grails service

后端 未结 4 393
感动是毒
感动是毒 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条回答
  •  不知归路
    2020-12-08 22:36

    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.

提交回复
热议问题