g.render from a Service?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
16 messages Options
Reply | Threaded
Open this post in threaded view
|

g.render from a Service?

elvanor
Hello,

I'd like to send emails from within a service. The view technology I
naturally planned to use was GSP.

How can I obtain a string from a rendered GSP page, within a service?
I usually use g.render in the controller, but I never did that from
within a service. I guess I need to get a reference to the GSP
rendering engine.

Thanks

Jean-Noel

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: g.render from a Service?

Daniel Honig
In other frameworks, such as ATG, we used to grab the
"PageCompileServlet" and simulate a request to render our templates.
I think in grails it would be great to have similar functionality.  If
not Groovy seems to have good templating support...

but I agree that what you really want is the ability to render full
blown GSP's and capture the output for use as an e-mail message body

On Thu, Jul 17, 2008 at 12:48 PM, Jean-Noël Rivasseau <[hidden email]> wrote:

> Hello,
>
> I'd like to send emails from within a service. The view technology I
> naturally planned to use was GSP.
>
> How can I obtain a string from a rendered GSP page, within a service?
> I usually use g.render in the controller, but I never did that from
> within a service. I guess I need to get a reference to the GSP
> rendering engine.
>
> Thanks
>
> Jean-Noel
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: g.render from a Service?

Konstantyn Smirnov
In reply to this post by elvanor
You can use SimpleTemplateEngine for that.

The only diff to GSP are missing <%%> tags. On the good side, you don't need to reference the GSP servlet, and your service can be used in stand-alone mode...
Reply | Threaded
Open this post in threaded view
|

Re: g.render from a Service?

Graeme-Rocher
In reply to this post by Daniel Honig
We already have that. See
http://gordopazexperience.blogspot.com/2008/07/writing-groovy-server-pages-output-to.html
for an example

Cheers

On Fri, Jul 18, 2008 at 5:27 AM, Daniel Honig <[hidden email]> wrote:

> In other frameworks, such as ATG, we used to grab the
> "PageCompileServlet" and simulate a request to render our templates.
> I think in grails it would be great to have similar functionality.  If
> not Groovy seems to have good templating support...
>
> but I agree that what you really want is the ability to render full
> blown GSP's and capture the output for use as an e-mail message body
>
> On Thu, Jul 17, 2008 at 12:48 PM, Jean-Noël Rivasseau <[hidden email]> wrote:
>> Hello,
>>
>> I'd like to send emails from within a service. The view technology I
>> naturally planned to use was GSP.
>>
>> How can I obtain a string from a rendered GSP page, within a service?
>> I usually use g.render in the controller, but I never did that from
>> within a service. I guess I need to get a reference to the GSP
>> rendering engine.
>>
>> Thanks
>>
>> Jean-Noel
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>    http://xircles.codehaus.org/manage_email
>>
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>



--
Graeme Rocher
Grails Project Lead
G2One, Inc. Chief Technology Officer
http://www.g2one.com

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: g.render from a Service?

Graeme-Rocher
Also note you can get a reference to the Spring configured GSP engine using:

GroovyPagesTemplateEngine groovyPagesTemplateEngine

Cheers

On Fri, Jul 18, 2008 at 9:36 AM, Graeme Rocher <[hidden email]> wrote:

> We already have that. See
> http://gordopazexperience.blogspot.com/2008/07/writing-groovy-server-pages-output-to.html
> for an example
>
> Cheers
>
> On Fri, Jul 18, 2008 at 5:27 AM, Daniel Honig <[hidden email]> wrote:
>> In other frameworks, such as ATG, we used to grab the
>> "PageCompileServlet" and simulate a request to render our templates.
>> I think in grails it would be great to have similar functionality.  If
>> not Groovy seems to have good templating support...
>>
>> but I agree that what you really want is the ability to render full
>> blown GSP's and capture the output for use as an e-mail message body
>>
>> On Thu, Jul 17, 2008 at 12:48 PM, Jean-Noël Rivasseau <[hidden email]> wrote:
>>> Hello,
>>>
>>> I'd like to send emails from within a service. The view technology I
>>> naturally planned to use was GSP.
>>>
>>> How can I obtain a string from a rendered GSP page, within a service?
>>> I usually use g.render in the controller, but I never did that from
>>> within a service. I guess I need to get a reference to the GSP
>>> rendering engine.
>>>
>>> Thanks
>>>
>>> Jean-Noel
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe from this list, please visit:
>>>
>>>    http://xircles.codehaus.org/manage_email
>>>
>>>
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>    http://xircles.codehaus.org/manage_email
>>
>>
>>
>
>
>
> --
> Graeme Rocher
> Grails Project Lead
> G2One, Inc. Chief Technology Officer
> http://www.g2one.com
>



--
Graeme Rocher
Grails Project Lead
G2One, Inc. Chief Technology Officer
http://www.g2one.com

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: g.render from a Service?

elvanor
Despite my efforts and searching through the ML I still cannot get this to work.

When getting a reference to the Spring configured GSP engine (obtained
in the controller via dependency injection), no matter which Writer I
use, the output is always written to the HTTP reply. This is my code:


                def template =
groovyPagesTemplateEngine.createTemplate("/customerOrder/_email.gsp")
               
                def writeable = template.make()
                def sw = new StringWriter()
                def pw = new PrintWriter(sw)
                writeable.writeTo(pw)
                def content = sw.toString()
                println content

content is always empty, but the result of the template goes into the
HTTP reply (the code is in service called by a controller). This is
obviously not what I need.

Note that writeable.writeTo() would produce the same effect and not an
exception as I anticipated, strange...

I also tried to create a new groovyPagesTemplateEngine like this:

                GroovyPagesTemplateEngine engine = new GroovyPagesTemplateEngine()
                def template = engine.createTemplate("/customerOrder/_email.gsp")

                def writeable = template.make()
                def sw = new StringWriter()
                def pw = new PrintWriter(sw)
                writeable.writeTo(pw)
                def content = sw.toString()

I get this exception:

java.lang.IllegalStateException: TemplateEngine not initialised
correctly, no [resourceLoader] specified!

Any help?

Jean-Noel

On Fri, Jul 18, 2008 at 10:38 AM, Graeme Rocher <[hidden email]> wrote:

> Also note you can get a reference to the Spring configured GSP engine using:
>
> GroovyPagesTemplateEngine groovyPagesTemplateEngine
>
> Cheers
>
> On Fri, Jul 18, 2008 at 9:36 AM, Graeme Rocher <[hidden email]> wrote:
>> We already have that. See
>> http://gordopazexperience.blogspot.com/2008/07/writing-groovy-server-pages-output-to.html
>> for an example
>>
>> Cheers
>>
>> On Fri, Jul 18, 2008 at 5:27 AM, Daniel Honig <[hidden email]> wrote:
>>> In other frameworks, such as ATG, we used to grab the
>>> "PageCompileServlet" and simulate a request to render our templates.
>>> I think in grails it would be great to have similar functionality.  If
>>> not Groovy seems to have good templating support...
>>>
>>> but I agree that what you really want is the ability to render full
>>> blown GSP's and capture the output for use as an e-mail message body
>>>
>>> On Thu, Jul 17, 2008 at 12:48 PM, Jean-Noël Rivasseau <[hidden email]> wrote:
>>>> Hello,
>>>>
>>>> I'd like to send emails from within a service. The view technology I
>>>> naturally planned to use was GSP.
>>>>
>>>> How can I obtain a string from a rendered GSP page, within a service?
>>>> I usually use g.render in the controller, but I never did that from
>>>> within a service. I guess I need to get a reference to the GSP
>>>> rendering engine.
>>>>
>>>> Thanks
>>>>
>>>> Jean-Noel
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe from this list, please visit:
>>>>
>>>>    http://xircles.codehaus.org/manage_email
>>>>
>>>>
>>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe from this list, please visit:
>>>
>>>    http://xircles.codehaus.org/manage_email
>>>
>>>
>>>
>>
>>
>>
>> --
>> Graeme Rocher
>> Grails Project Lead
>> G2One, Inc. Chief Technology Officer
>> http://www.g2one.com
>>
>
>
>
> --
> Graeme Rocher
> Grails Project Lead
> G2One, Inc. Chief Technology Officer
> http://www.g2one.com
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: g.render from a Service?

elvanor
Follow-up: by creating a Spring Resource and feeding it to the engine,
I can get the same behavior with a newly created engine than with the
default Spring one (eg, no more TemplateEngine not initialised
correctly, no [resourceLoader] specified!). That does not help me much
though :(

On Tue, Jul 22, 2008 at 8:57 PM, Jean-Noël Rivasseau <[hidden email]> wrote:

> Despite my efforts and searching through the ML I still cannot get this to work.
>
> When getting a reference to the Spring configured GSP engine (obtained
> in the controller via dependency injection), no matter which Writer I
> use, the output is always written to the HTTP reply. This is my code:
>
>
>                def template =
> groovyPagesTemplateEngine.createTemplate("/customerOrder/_email.gsp")
>
>                def writeable = template.make()
>                def sw = new StringWriter()
>                def pw = new PrintWriter(sw)
>                writeable.writeTo(pw)
>                def content = sw.toString()
>                println content
>
> content is always empty, but the result of the template goes into the
> HTTP reply (the code is in service called by a controller). This is
> obviously not what I need.
>
> Note that writeable.writeTo() would produce the same effect and not an
> exception as I anticipated, strange...
>
> I also tried to create a new groovyPagesTemplateEngine like this:
>
>                GroovyPagesTemplateEngine engine = new GroovyPagesTemplateEngine()
>                def template = engine.createTemplate("/customerOrder/_email.gsp")
>
>                def writeable = template.make()
>                def sw = new StringWriter()
>                def pw = new PrintWriter(sw)
>                writeable.writeTo(pw)
>                def content = sw.toString()
>
> I get this exception:
>
> java.lang.IllegalStateException: TemplateEngine not initialised
> correctly, no [resourceLoader] specified!
>
> Any help?
>
> Jean-Noel
>
> On Fri, Jul 18, 2008 at 10:38 AM, Graeme Rocher <[hidden email]> wrote:
>> Also note you can get a reference to the Spring configured GSP engine using:
>>
>> GroovyPagesTemplateEngine groovyPagesTemplateEngine
>>
>> Cheers
>>
>> On Fri, Jul 18, 2008 at 9:36 AM, Graeme Rocher <[hidden email]> wrote:
>>> We already have that. See
>>> http://gordopazexperience.blogspot.com/2008/07/writing-groovy-server-pages-output-to.html
>>> for an example
>>>
>>> Cheers
>>>
>>> On Fri, Jul 18, 2008 at 5:27 AM, Daniel Honig <[hidden email]> wrote:
>>>> In other frameworks, such as ATG, we used to grab the
>>>> "PageCompileServlet" and simulate a request to render our templates.
>>>> I think in grails it would be great to have similar functionality.  If
>>>> not Groovy seems to have good templating support...
>>>>
>>>> but I agree that what you really want is the ability to render full
>>>> blown GSP's and capture the output for use as an e-mail message body
>>>>
>>>> On Thu, Jul 17, 2008 at 12:48 PM, Jean-Noël Rivasseau <[hidden email]> wrote:
>>>>> Hello,
>>>>>
>>>>> I'd like to send emails from within a service. The view technology I
>>>>> naturally planned to use was GSP.
>>>>>
>>>>> How can I obtain a string from a rendered GSP page, within a service?
>>>>> I usually use g.render in the controller, but I never did that from
>>>>> within a service. I guess I need to get a reference to the GSP
>>>>> rendering engine.
>>>>>
>>>>> Thanks
>>>>>
>>>>> Jean-Noel
>>>>>
>>>>> ---------------------------------------------------------------------
>>>>> To unsubscribe from this list, please visit:
>>>>>
>>>>>    http://xircles.codehaus.org/manage_email
>>>>>
>>>>>
>>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe from this list, please visit:
>>>>
>>>>    http://xircles.codehaus.org/manage_email
>>>>
>>>>
>>>>
>>>
>>>
>>>
>>> --
>>> Graeme Rocher
>>> Grails Project Lead
>>> G2One, Inc. Chief Technology Officer
>>> http://www.g2one.com
>>>
>>
>>
>>
>> --
>> Graeme Rocher
>> Grails Project Lead
>> G2One, Inc. Chief Technology Officer
>> http://www.g2one.com
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>    http://xircles.codehaus.org/manage_email
>>
>>
>>
>
Reply | Threaded
Open this post in threaded view
|

Re: g.render from a Service?

elvanor
Still stuck, anyone?

On Tue, Jul 22, 2008 at 9:43 PM, Jean-Noël Rivasseau <[hidden email]> wrote:

> Follow-up: by creating a Spring Resource and feeding it to the engine,
> I can get the same behavior with a newly created engine than with the
> default Spring one (eg, no more TemplateEngine not initialised
> correctly, no [resourceLoader] specified!). That does not help me much
> though :(
>
> On Tue, Jul 22, 2008 at 8:57 PM, Jean-Noël Rivasseau <[hidden email]> wrote:
>> Despite my efforts and searching through the ML I still cannot get this to work.
>>
>> When getting a reference to the Spring configured GSP engine (obtained
>> in the controller via dependency injection), no matter which Writer I
>> use, the output is always written to the HTTP reply. This is my code:
>>
>>
>>                def template =
>> groovyPagesTemplateEngine.createTemplate("/customerOrder/_email.gsp")
>>
>>                def writeable = template.make()
>>                def sw = new StringWriter()
>>                def pw = new PrintWriter(sw)
>>                writeable.writeTo(pw)
>>                def content = sw.toString()
>>                println content
>>
>> content is always empty, but the result of the template goes into the
>> HTTP reply (the code is in service called by a controller). This is
>> obviously not what I need.
>>
>> Note that writeable.writeTo() would produce the same effect and not an
>> exception as I anticipated, strange...
>>
>> I also tried to create a new groovyPagesTemplateEngine like this:
>>
>>                GroovyPagesTemplateEngine engine = new GroovyPagesTemplateEngine()
>>                def template = engine.createTemplate("/customerOrder/_email.gsp")
>>
>>                def writeable = template.make()
>>                def sw = new StringWriter()
>>                def pw = new PrintWriter(sw)
>>                writeable.writeTo(pw)
>>                def content = sw.toString()
>>
>> I get this exception:
>>
>> java.lang.IllegalStateException: TemplateEngine not initialised
>> correctly, no [resourceLoader] specified!
>>
>> Any help?
>>
>> Jean-Noel
>>
>> On Fri, Jul 18, 2008 at 10:38 AM, Graeme Rocher <[hidden email]> wrote:
>>> Also note you can get a reference to the Spring configured GSP engine using:
>>>
>>> GroovyPagesTemplateEngine groovyPagesTemplateEngine
>>>
>>> Cheers
>>>
>>> On Fri, Jul 18, 2008 at 9:36 AM, Graeme Rocher <[hidden email]> wrote:
>>>> We already have that. See
>>>> http://gordopazexperience.blogspot.com/2008/07/writing-groovy-server-pages-output-to.html
>>>> for an example
>>>>
>>>> Cheers
>>>>
>>>> On Fri, Jul 18, 2008 at 5:27 AM, Daniel Honig <[hidden email]> wrote:
>>>>> In other frameworks, such as ATG, we used to grab the
>>>>> "PageCompileServlet" and simulate a request to render our templates.
>>>>> I think in grails it would be great to have similar functionality.  If
>>>>> not Groovy seems to have good templating support...
>>>>>
>>>>> but I agree that what you really want is the ability to render full
>>>>> blown GSP's and capture the output for use as an e-mail message body
>>>>>
>>>>> On Thu, Jul 17, 2008 at 12:48 PM, Jean-Noël Rivasseau <[hidden email]> wrote:
>>>>>> Hello,
>>>>>>
>>>>>> I'd like to send emails from within a service. The view technology I
>>>>>> naturally planned to use was GSP.
>>>>>>
>>>>>> How can I obtain a string from a rendered GSP page, within a service?
>>>>>> I usually use g.render in the controller, but I never did that from
>>>>>> within a service. I guess I need to get a reference to the GSP
>>>>>> rendering engine.
>>>>>>
>>>>>> Thanks
>>>>>>
>>>>>> Jean-Noel
>>>>>>
>>>>>> ---------------------------------------------------------------------
>>>>>> To unsubscribe from this list, please visit:
>>>>>>
>>>>>>    http://xircles.codehaus.org/manage_email
>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>> ---------------------------------------------------------------------
>>>>> To unsubscribe from this list, please visit:
>>>>>
>>>>>    http://xircles.codehaus.org/manage_email
>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> Graeme Rocher
>>>> Grails Project Lead
>>>> G2One, Inc. Chief Technology Officer
>>>> http://www.g2one.com
>>>>
>>>
>>>
>>>
>>> --
>>> Graeme Rocher
>>> Grails Project Lead
>>> G2One, Inc. Chief Technology Officer
>>> http://www.g2one.com
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe from this list, please visit:
>>>
>>>    http://xircles.codehaus.org/manage_email
>>>
>>>
>>>
>>
>
Reply | Threaded
Open this post in threaded view
|

Re: g.render from a Service?

Graeme-Rocher
Hmm i think i understand what the problem is. Try doing

def webRequest = RequestContextHolder.currentRequestAttributes()
def originalOut = webRequest.out
try {

   ... // create print writer pw
   webRequest.out = pw
   ...
   // use groovPagesTemplateEngine

} finally {
  webRequest.out = originalOut
}


Cheers

On Wed, Jul 23, 2008 at 1:25 PM, Jean-Noël Rivasseau <[hidden email]> wrote:

> Still stuck, anyone?
>
> On Tue, Jul 22, 2008 at 9:43 PM, Jean-Noël Rivasseau <[hidden email]> wrote:
>> Follow-up: by creating a Spring Resource and feeding it to the engine,
>> I can get the same behavior with a newly created engine than with the
>> default Spring one (eg, no more TemplateEngine not initialised
>> correctly, no [resourceLoader] specified!). That does not help me much
>> though :(
>>
>> On Tue, Jul 22, 2008 at 8:57 PM, Jean-Noël Rivasseau <[hidden email]> wrote:
>>> Despite my efforts and searching through the ML I still cannot get this to work.
>>>
>>> When getting a reference to the Spring configured GSP engine (obtained
>>> in the controller via dependency injection), no matter which Writer I
>>> use, the output is always written to the HTTP reply. This is my code:
>>>
>>>
>>>                def template =
>>> groovyPagesTemplateEngine.createTemplate("/customerOrder/_email.gsp")
>>>
>>>                def writeable = template.make()
>>>                def sw = new StringWriter()
>>>                def pw = new PrintWriter(sw)
>>>                writeable.writeTo(pw)
>>>                def content = sw.toString()
>>>                println content
>>>
>>> content is always empty, but the result of the template goes into the
>>> HTTP reply (the code is in service called by a controller). This is
>>> obviously not what I need.
>>>
>>> Note that writeable.writeTo() would produce the same effect and not an
>>> exception as I anticipated, strange...
>>>
>>> I also tried to create a new groovyPagesTemplateEngine like this:
>>>
>>>                GroovyPagesTemplateEngine engine = new GroovyPagesTemplateEngine()
>>>                def template = engine.createTemplate("/customerOrder/_email.gsp")
>>>
>>>                def writeable = template.make()
>>>                def sw = new StringWriter()
>>>                def pw = new PrintWriter(sw)
>>>                writeable.writeTo(pw)
>>>                def content = sw.toString()
>>>
>>> I get this exception:
>>>
>>> java.lang.IllegalStateException: TemplateEngine not initialised
>>> correctly, no [resourceLoader] specified!
>>>
>>> Any help?
>>>
>>> Jean-Noel
>>>
>>> On Fri, Jul 18, 2008 at 10:38 AM, Graeme Rocher <[hidden email]> wrote:
>>>> Also note you can get a reference to the Spring configured GSP engine using:
>>>>
>>>> GroovyPagesTemplateEngine groovyPagesTemplateEngine
>>>>
>>>> Cheers
>>>>
>>>> On Fri, Jul 18, 2008 at 9:36 AM, Graeme Rocher <[hidden email]> wrote:
>>>>> We already have that. See
>>>>> http://gordopazexperience.blogspot.com/2008/07/writing-groovy-server-pages-output-to.html
>>>>> for an example
>>>>>
>>>>> Cheers
>>>>>
>>>>> On Fri, Jul 18, 2008 at 5:27 AM, Daniel Honig <[hidden email]> wrote:
>>>>>> In other frameworks, such as ATG, we used to grab the
>>>>>> "PageCompileServlet" and simulate a request to render our templates.
>>>>>> I think in grails it would be great to have similar functionality.  If
>>>>>> not Groovy seems to have good templating support...
>>>>>>
>>>>>> but I agree that what you really want is the ability to render full
>>>>>> blown GSP's and capture the output for use as an e-mail message body
>>>>>>
>>>>>> On Thu, Jul 17, 2008 at 12:48 PM, Jean-Noël Rivasseau <[hidden email]> wrote:
>>>>>>> Hello,
>>>>>>>
>>>>>>> I'd like to send emails from within a service. The view technology I
>>>>>>> naturally planned to use was GSP.
>>>>>>>
>>>>>>> How can I obtain a string from a rendered GSP page, within a service?
>>>>>>> I usually use g.render in the controller, but I never did that from
>>>>>>> within a service. I guess I need to get a reference to the GSP
>>>>>>> rendering engine.
>>>>>>>
>>>>>>> Thanks
>>>>>>>
>>>>>>> Jean-Noel
>>>>>>>
>>>>>>> ---------------------------------------------------------------------
>>>>>>> To unsubscribe from this list, please visit:
>>>>>>>
>>>>>>>    http://xircles.codehaus.org/manage_email
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>
>>>>>> ---------------------------------------------------------------------
>>>>>> To unsubscribe from this list, please visit:
>>>>>>
>>>>>>    http://xircles.codehaus.org/manage_email
>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> Graeme Rocher
>>>>> Grails Project Lead
>>>>> G2One, Inc. Chief Technology Officer
>>>>> http://www.g2one.com
>>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> Graeme Rocher
>>>> Grails Project Lead
>>>> G2One, Inc. Chief Technology Officer
>>>> http://www.g2one.com
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe from this list, please visit:
>>>>
>>>>    http://xircles.codehaus.org/manage_email
>>>>
>>>>
>>>>
>>>
>>
>



--
Graeme Rocher
Grails Project Lead
G2One, Inc. Chief Technology Officer
http://www.g2one.com

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: g.render from a Service?

elvanor
Thanks Graeme - works perfectly. At last I got this part working right :)

It clearly looks like a hack though... should I JIRA for an
architecture improvement that would allow more straightforward use of
the groovPagesTemplateEngine?

Or maybe it will already be rewritten as part of 1.1 in order to make
GSP usable standalone? (I would guess this is the case).

Jean-Noel

On Wed, Jul 23, 2008 at 3:10 PM, Graeme Rocher <[hidden email]> wrote:

> Hmm i think i understand what the problem is. Try doing
>
> def webRequest = RequestContextHolder.currentRequestAttributes()
> def originalOut = webRequest.out
> try {
>
>   ... // create print writer pw
>   webRequest.out = pw
>   ...
>   // use groovPagesTemplateEngine
>
> } finally {
>  webRequest.out = originalOut
> }
>
>
> Cheers
>
> On Wed, Jul 23, 2008 at 1:25 PM, Jean-Noël Rivasseau <[hidden email]> wrote:
>> Still stuck, anyone?
>>
>> On Tue, Jul 22, 2008 at 9:43 PM, Jean-Noël Rivasseau <[hidden email]> wrote:
>>> Follow-up: by creating a Spring Resource and feeding it to the engine,
>>> I can get the same behavior with a newly created engine than with the
>>> default Spring one (eg, no more TemplateEngine not initialised
>>> correctly, no [resourceLoader] specified!). That does not help me much
>>> though :(
>>>
>>> On Tue, Jul 22, 2008 at 8:57 PM, Jean-Noël Rivasseau <[hidden email]> wrote:
>>>> Despite my efforts and searching through the ML I still cannot get this to work.
>>>>
>>>> When getting a reference to the Spring configured GSP engine (obtained
>>>> in the controller via dependency injection), no matter which Writer I
>>>> use, the output is always written to the HTTP reply. This is my code:
>>>>
>>>>
>>>>                def template =
>>>> groovyPagesTemplateEngine.createTemplate("/customerOrder/_email.gsp")
>>>>
>>>>                def writeable = template.make()
>>>>                def sw = new StringWriter()
>>>>                def pw = new PrintWriter(sw)
>>>>                writeable.writeTo(pw)
>>>>                def content = sw.toString()
>>>>                println content
>>>>
>>>> content is always empty, but the result of the template goes into the
>>>> HTTP reply (the code is in service called by a controller). This is
>>>> obviously not what I need.
>>>>
>>>> Note that writeable.writeTo() would produce the same effect and not an
>>>> exception as I anticipated, strange...
>>>>
>>>> I also tried to create a new groovyPagesTemplateEngine like this:
>>>>
>>>>                GroovyPagesTemplateEngine engine = new GroovyPagesTemplateEngine()
>>>>                def template = engine.createTemplate("/customerOrder/_email.gsp")
>>>>
>>>>                def writeable = template.make()
>>>>                def sw = new StringWriter()
>>>>                def pw = new PrintWriter(sw)
>>>>                writeable.writeTo(pw)
>>>>                def content = sw.toString()
>>>>
>>>> I get this exception:
>>>>
>>>> java.lang.IllegalStateException: TemplateEngine not initialised
>>>> correctly, no [resourceLoader] specified!
>>>>
>>>> Any help?
>>>>
>>>> Jean-Noel
>>>>
>>>> On Fri, Jul 18, 2008 at 10:38 AM, Graeme Rocher <[hidden email]> wrote:
>>>>> Also note you can get a reference to the Spring configured GSP engine using:
>>>>>
>>>>> GroovyPagesTemplateEngine groovyPagesTemplateEngine
>>>>>
>>>>> Cheers
>>>>>
>>>>> On Fri, Jul 18, 2008 at 9:36 AM, Graeme Rocher <[hidden email]> wrote:
>>>>>> We already have that. See
>>>>>> http://gordopazexperience.blogspot.com/2008/07/writing-groovy-server-pages-output-to.html
>>>>>> for an example
>>>>>>
>>>>>> Cheers
>>>>>>
>>>>>> On Fri, Jul 18, 2008 at 5:27 AM, Daniel Honig <[hidden email]> wrote:
>>>>>>> In other frameworks, such as ATG, we used to grab the
>>>>>>> "PageCompileServlet" and simulate a request to render our templates.
>>>>>>> I think in grails it would be great to have similar functionality.  If
>>>>>>> not Groovy seems to have good templating support...
>>>>>>>
>>>>>>> but I agree that what you really want is the ability to render full
>>>>>>> blown GSP's and capture the output for use as an e-mail message body
>>>>>>>
>>>>>>> On Thu, Jul 17, 2008 at 12:48 PM, Jean-Noël Rivasseau <[hidden email]> wrote:
>>>>>>>> Hello,
>>>>>>>>
>>>>>>>> I'd like to send emails from within a service. The view technology I
>>>>>>>> naturally planned to use was GSP.
>>>>>>>>
>>>>>>>> How can I obtain a string from a rendered GSP page, within a service?
>>>>>>>> I usually use g.render in the controller, but I never did that from
>>>>>>>> within a service. I guess I need to get a reference to the GSP
>>>>>>>> rendering engine.
>>>>>>>>
>>>>>>>> Thanks
>>>>>>>>
>>>>>>>> Jean-Noel
>>>>>>>>
>>>>>>>> ---------------------------------------------------------------------
>>>>>>>> To unsubscribe from this list, please visit:
>>>>>>>>
>>>>>>>>    http://xircles.codehaus.org/manage_email
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>
>>>>>>> ---------------------------------------------------------------------
>>>>>>> To unsubscribe from this list, please visit:
>>>>>>>
>>>>>>>    http://xircles.codehaus.org/manage_email
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> --
>>>>>> Graeme Rocher
>>>>>> Grails Project Lead
>>>>>> G2One, Inc. Chief Technology Officer
>>>>>> http://www.g2one.com
>>>>>>
>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> Graeme Rocher
>>>>> Grails Project Lead
>>>>> G2One, Inc. Chief Technology Officer
>>>>> http://www.g2one.com
>>>>>
>>>>> ---------------------------------------------------------------------
>>>>> To unsubscribe from this list, please visit:
>>>>>
>>>>>    http://xircles.codehaus.org/manage_email
>>>>>
>>>>>
>>>>>
>>>>
>>>
>>
>
>
>
> --
> Graeme Rocher
> Grails Project Lead
> G2One, Inc. Chief Technology Officer
> http://www.g2one.com
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: g.render from a Service?

fabien7474
Is there any progress on this issue OR a simpler solution than the hack described here?

If not, is there an existing JIRA that I can at least vote for ?

Thank you,

Fabien.

Jean-Noël Rivasseau wrote
Thanks Graeme - works perfectly. At last I got this part working right :)

It clearly looks like a hack though... should I JIRA for an
architecture improvement that would allow more straightforward use of
the groovPagesTemplateEngine?

Or maybe it will already be rewritten as part of 1.1 in order to make
GSP usable standalone? (I would guess this is the case).

Jean-Noel

On Wed, Jul 23, 2008 at 3:10 PM, Graeme Rocher <graeme@g2one.com> wrote:
> Hmm i think i understand what the problem is. Try doing
>
> def webRequest = RequestContextHolder.currentRequestAttributes()
> def originalOut = webRequest.out
> try {
>
>   ... // create print writer pw
>   webRequest.out = pw
>   ...
>   // use groovPagesTemplateEngine
>
> } finally {
>  webRequest.out = originalOut
> }
>
>
> Cheers
>
> On Wed, Jul 23, 2008 at 1:25 PM, Jean-Noël Rivasseau <elvanor@gmail.com> wrote:
>> Still stuck, anyone?
>>
>> On Tue, Jul 22, 2008 at 9:43 PM, Jean-Noël Rivasseau <elvanor@gmail.com> wrote:
>>> Follow-up: by creating a Spring Resource and feeding it to the engine,
>>> I can get the same behavior with a newly created engine than with the
>>> default Spring one (eg, no more TemplateEngine not initialised
>>> correctly, no [resourceLoader] specified!). That does not help me much
>>> though :(
>>>
>>> On Tue, Jul 22, 2008 at 8:57 PM, Jean-Noël Rivasseau <elvanor@gmail.com> wrote:
>>>> Despite my efforts and searching through the ML I still cannot get this to work.
>>>>
>>>> When getting a reference to the Spring configured GSP engine (obtained
>>>> in the controller via dependency injection), no matter which Writer I
>>>> use, the output is always written to the HTTP reply. This is my code:
>>>>
>>>>
>>>>                def template =
>>>> groovyPagesTemplateEngine.createTemplate("/customerOrder/_email.gsp")
>>>>
>>>>                def writeable = template.make()
>>>>                def sw = new StringWriter()
>>>>                def pw = new PrintWriter(sw)
>>>>                writeable.writeTo(pw)
>>>>                def content = sw.toString()
>>>>                println content
>>>>
>>>> content is always empty, but the result of the template goes into the
>>>> HTTP reply (the code is in service called by a controller). This is
>>>> obviously not what I need.
>>>>
>>>> Note that writeable.writeTo() would produce the same effect and not an
>>>> exception as I anticipated, strange...
>>>>
>>>> I also tried to create a new groovyPagesTemplateEngine like this:
>>>>
>>>>                GroovyPagesTemplateEngine engine = new GroovyPagesTemplateEngine()
>>>>                def template = engine.createTemplate("/customerOrder/_email.gsp")
>>>>
>>>>                def writeable = template.make()
>>>>                def sw = new StringWriter()
>>>>                def pw = new PrintWriter(sw)
>>>>                writeable.writeTo(pw)
>>>>                def content = sw.toString()
>>>>
>>>> I get this exception:
>>>>
>>>> java.lang.IllegalStateException: TemplateEngine not initialised
>>>> correctly, no [resourceLoader] specified!
>>>>
>>>> Any help?
>>>>
>>>> Jean-Noel
>>>>
>>>> On Fri, Jul 18, 2008 at 10:38 AM, Graeme Rocher <graeme@g2one.com> wrote:
>>>>> Also note you can get a reference to the Spring configured GSP engine using:
>>>>>
>>>>> GroovyPagesTemplateEngine groovyPagesTemplateEngine
>>>>>
>>>>> Cheers
>>>>>
>>>>> On Fri, Jul 18, 2008 at 9:36 AM, Graeme Rocher <graeme@g2one.com> wrote:
>>>>>> We already have that. See
>>>>>> http://gordopazexperience.blogspot.com/2008/07/writing-groovy-server-pages-output-to.html
>>>>>> for an example
>>>>>>
>>>>>> Cheers
>>>>>>
>>>>>> On Fri, Jul 18, 2008 at 5:27 AM, Daniel Honig <daniel.honig@gmail.com> wrote:
>>>>>>> In other frameworks, such as ATG, we used to grab the
>>>>>>> "PageCompileServlet" and simulate a request to render our templates.
>>>>>>> I think in grails it would be great to have similar functionality.  If
>>>>>>> not Groovy seems to have good templating support...
>>>>>>>
>>>>>>> but I agree that what you really want is the ability to render full
>>>>>>> blown GSP's and capture the output for use as an e-mail message body
>>>>>>>
>>>>>>> On Thu, Jul 17, 2008 at 12:48 PM, Jean-Noël Rivasseau <elvanor@gmail.com> wrote:
>>>>>>>> Hello,
>>>>>>>>
>>>>>>>> I'd like to send emails from within a service. The view technology I
>>>>>>>> naturally planned to use was GSP.
>>>>>>>>
>>>>>>>> How can I obtain a string from a rendered GSP page, within a service?
>>>>>>>> I usually use g.render in the controller, but I never did that from
>>>>>>>> within a service. I guess I need to get a reference to the GSP
>>>>>>>> rendering engine.
>>>>>>>>
>>>>>>>> Thanks
>>>>>>>>
>>>>>>>> Jean-Noel
>>>>>>>>
>>>>>>>> ---------------------------------------------------------------------
>>>>>>>> To unsubscribe from this list, please visit:
>>>>>>>>
>>>>>>>>    http://xircles.codehaus.org/manage_email
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>
>>>>>>> ---------------------------------------------------------------------
>>>>>>> To unsubscribe from this list, please visit:
>>>>>>>
>>>>>>>    http://xircles.codehaus.org/manage_email
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> --
>>>>>> Graeme Rocher
>>>>>> Grails Project Lead
>>>>>> G2One, Inc. Chief Technology Officer
>>>>>> http://www.g2one.com
>>>>>>
>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> Graeme Rocher
>>>>> Grails Project Lead
>>>>> G2One, Inc. Chief Technology Officer
>>>>> http://www.g2one.com
>>>>>
>>>>> ---------------------------------------------------------------------
>>>>> To unsubscribe from this list, please visit:
>>>>>
>>>>>    http://xircles.codehaus.org/manage_email
>>>>>
>>>>>
>>>>>
>>>>
>>>
>>
>
>
>
> --
> Graeme Rocher
> Grails Project Lead
> G2One, Inc. Chief Technology Officer
> http://www.g2one.com
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: g.render from a Service?

ld@ldaley.com
As far as I am aware this is currently the best solution.

Please raise a JIRA with something like “make it easier to render a gsp template outside of a controller”.

On 06/10/2010, at 11:34 PM, fabien7474 wrote:

>
> Is there any progress on this issue OR a simpler solution than the hack
> described here?
>
> If not, is there an existing JIRA that I can at least vote for ?
>
> Thank you,
>
> Fabien.
>
>
> Jean-Noël Rivasseau wrote:
>>
>> Thanks Graeme - works perfectly. At last I got this part working right :)
>>
>> It clearly looks like a hack though... should I JIRA for an
>> architecture improvement that would allow more straightforward use of
>> the groovPagesTemplateEngine?
>>
>> Or maybe it will already be rewritten as part of 1.1 in order to make
>> GSP usable standalone? (I would guess this is the case).
>>
>> Jean-Noel
>>
>> On Wed, Jul 23, 2008 at 3:10 PM, Graeme Rocher <[hidden email]> wrote:
>>> Hmm i think i understand what the problem is. Try doing
>>>
>>> def webRequest = RequestContextHolder.currentRequestAttributes()
>>> def originalOut = webRequest.out
>>> try {
>>>
>>>  ... // create print writer pw
>>>  webRequest.out = pw
>>>  ...
>>>  // use groovPagesTemplateEngine
>>>
>>> } finally {
>>> webRequest.out = originalOut
>>> }
>>>
>>>
>>> Cheers
>>>
>>> On Wed, Jul 23, 2008 at 1:25 PM, Jean-Noël Rivasseau <[hidden email]>
>>> wrote:
>>>> Still stuck, anyone?
>>>>
>>>> On Tue, Jul 22, 2008 at 9:43 PM, Jean-Noël Rivasseau <[hidden email]>
>>>> wrote:
>>>>> Follow-up: by creating a Spring Resource and feeding it to the engine,
>>>>> I can get the same behavior with a newly created engine than with the
>>>>> default Spring one (eg, no more TemplateEngine not initialised
>>>>> correctly, no [resourceLoader] specified!). That does not help me much
>>>>> though :(
>>>>>
>>>>> On Tue, Jul 22, 2008 at 8:57 PM, Jean-Noël Rivasseau
>>>>> <[hidden email]> wrote:
>>>>>> Despite my efforts and searching through the ML I still cannot get
>>>>>> this to work.
>>>>>>
>>>>>> When getting a reference to the Spring configured GSP engine (obtained
>>>>>> in the controller via dependency injection), no matter which Writer I
>>>>>> use, the output is always written to the HTTP reply. This is my code:
>>>>>>
>>>>>>
>>>>>>               def template =
>>>>>> groovyPagesTemplateEngine.createTemplate("/customerOrder/_email.gsp")
>>>>>>
>>>>>>               def writeable = template.make()
>>>>>>               def sw = new StringWriter()
>>>>>>               def pw = new PrintWriter(sw)
>>>>>>               writeable.writeTo(pw)
>>>>>>               def content = sw.toString()
>>>>>>               println content
>>>>>>
>>>>>> content is always empty, but the result of the template goes into the
>>>>>> HTTP reply (the code is in service called by a controller). This is
>>>>>> obviously not what I need.
>>>>>>
>>>>>> Note that writeable.writeTo() would produce the same effect and not an
>>>>>> exception as I anticipated, strange...
>>>>>>
>>>>>> I also tried to create a new groovyPagesTemplateEngine like this:
>>>>>>
>>>>>>               GroovyPagesTemplateEngine engine = new
>>>>>> GroovyPagesTemplateEngine()
>>>>>>               def template =
>>>>>> engine.createTemplate("/customerOrder/_email.gsp")
>>>>>>
>>>>>>               def writeable = template.make()
>>>>>>               def sw = new StringWriter()
>>>>>>               def pw = new PrintWriter(sw)
>>>>>>               writeable.writeTo(pw)
>>>>>>               def content = sw.toString()
>>>>>>
>>>>>> I get this exception:
>>>>>>
>>>>>> java.lang.IllegalStateException: TemplateEngine not initialised
>>>>>> correctly, no [resourceLoader] specified!
>>>>>>
>>>>>> Any help?
>>>>>>
>>>>>> Jean-Noel
>>>>>>
>>>>>> On Fri, Jul 18, 2008 at 10:38 AM, Graeme Rocher <[hidden email]>
>>>>>> wrote:
>>>>>>> Also note you can get a reference to the Spring configured GSP engine
>>>>>>> using:
>>>>>>>
>>>>>>> GroovyPagesTemplateEngine groovyPagesTemplateEngine
>>>>>>>
>>>>>>> Cheers
>>>>>>>
>>>>>>> On Fri, Jul 18, 2008 at 9:36 AM, Graeme Rocher <[hidden email]>
>>>>>>> wrote:
>>>>>>>> We already have that. See
>>>>>>>> http://gordopazexperience.blogspot.com/2008/07/writing-groovy-server-pages-output-to.html
>>>>>>>> for an example
>>>>>>>>
>>>>>>>> Cheers
>>>>>>>>
>>>>>>>> On Fri, Jul 18, 2008 at 5:27 AM, Daniel Honig
>>>>>>>> <[hidden email]> wrote:
>>>>>>>>> In other frameworks, such as ATG, we used to grab the
>>>>>>>>> "PageCompileServlet" and simulate a request to render our
>>>>>>>>> templates.
>>>>>>>>> I think in grails it would be great to have similar functionality.
>>>>>>>>> If
>>>>>>>>> not Groovy seems to have good templating support...
>>>>>>>>>
>>>>>>>>> but I agree that what you really want is the ability to render full
>>>>>>>>> blown GSP's and capture the output for use as an e-mail message
>>>>>>>>> body
>>>>>>>>>
>>>>>>>>> On Thu, Jul 17, 2008 at 12:48 PM, Jean-Noël Rivasseau
>>>>>>>>> <[hidden email]> wrote:
>>>>>>>>>> Hello,
>>>>>>>>>>
>>>>>>>>>> I'd like to send emails from within a service. The view technology
>>>>>>>>>> I
>>>>>>>>>> naturally planned to use was GSP.
>>>>>>>>>>
>>>>>>>>>> How can I obtain a string from a rendered GSP page, within a
>>>>>>>>>> service?
>>>>>>>>>> I usually use g.render in the controller, but I never did that
>>>>>>>>>> from
>>>>>>>>>> within a service. I guess I need to get a reference to the GSP
>>>>>>>>>> rendering engine.
>>>>>>>>>>
>>>>>>>>>> Thanks
>>>>>>>>>>
>>>>>>>>>> Jean-Noel
>>>>>>>>>>
>>>>>>>>>> ---------------------------------------------------------------------
>>>>>>>>>> To unsubscribe from this list, please visit:
>>>>>>>>>>
>>>>>>>>>>   http://xircles.codehaus.org/manage_email
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>
>>>>>>>>> ---------------------------------------------------------------------
>>>>>>>>> To unsubscribe from this list, please visit:
>>>>>>>>>
>>>>>>>>>   http://xircles.codehaus.org/manage_email
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>> --
>>>>>>>> Graeme Rocher
>>>>>>>> Grails Project Lead
>>>>>>>> G2One, Inc. Chief Technology Officer
>>>>>>>> http://www.g2one.com
>>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> --
>>>>>>> Graeme Rocher
>>>>>>> Grails Project Lead
>>>>>>> G2One, Inc. Chief Technology Officer
>>>>>>> http://www.g2one.com
>>>>>>>
>>>>>>> ---------------------------------------------------------------------
>>>>>>> To unsubscribe from this list, please visit:
>>>>>>>
>>>>>>>   http://xircles.codehaus.org/manage_email
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>
>>>>>
>>>>
>>>
>>>
>>>
>>> --
>>> Graeme Rocher
>>> Grails Project Lead
>>> G2One, Inc. Chief Technology Officer
>>> http://www.g2one.com
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe from this list, please visit:
>>>
>>>   http://xircles.codehaus.org/manage_email
>>>
>>>
>>>
>>
>>
>
> --
> View this message in context: http://grails.1312388.n4.nabble.com/g-render-from-a-Service-tp1352284p2964959.html
> Sent from the Grails - user mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: g.render from a Service?

James Cook-15
You could use the Grails Template Engine plugin
(http://www.grails.org/plugin/grails-template-engine).
It probably still has the issue that it doesn't compile out of the
bag, but it's an easy fix. We use it a lot :)

James

On 8 October 2010 06:22, Luke Daley <[hidden email]> wrote:

> As far as I am aware this is currently the best solution.
>
> Please raise a JIRA with something like “make it easier to render a gsp template outside of a controller”.
>
> On 06/10/2010, at 11:34 PM, fabien7474 wrote:
>
>>
>> Is there any progress on this issue OR a simpler solution than the hack
>> described here?
>>
>> If not, is there an existing JIRA that I can at least vote for ?
>>
>> Thank you,
>>
>> Fabien.
>>
>>
>> Jean-Noël Rivasseau wrote:
>>>
>>> Thanks Graeme - works perfectly. At last I got this part working right :)
>>>
>>> It clearly looks like a hack though... should I JIRA for an
>>> architecture improvement that would allow more straightforward use of
>>> the groovPagesTemplateEngine?
>>>
>>> Or maybe it will already be rewritten as part of 1.1 in order to make
>>> GSP usable standalone? (I would guess this is the case).
>>>
>>> Jean-Noel
>>>
>>> On Wed, Jul 23, 2008 at 3:10 PM, Graeme Rocher <[hidden email]> wrote:
>>>> Hmm i think i understand what the problem is. Try doing
>>>>
>>>> def webRequest = RequestContextHolder.currentRequestAttributes()
>>>> def originalOut = webRequest.out
>>>> try {
>>>>
>>>>  ... // create print writer pw
>>>>  webRequest.out = pw
>>>>  ...
>>>>  // use groovPagesTemplateEngine
>>>>
>>>> } finally {
>>>> webRequest.out = originalOut
>>>> }
>>>>
>>>>
>>>> Cheers
>>>>
>>>> On Wed, Jul 23, 2008 at 1:25 PM, Jean-Noël Rivasseau <[hidden email]>
>>>> wrote:
>>>>> Still stuck, anyone?
>>>>>
>>>>> On Tue, Jul 22, 2008 at 9:43 PM, Jean-Noël Rivasseau <[hidden email]>
>>>>> wrote:
>>>>>> Follow-up: by creating a Spring Resource and feeding it to the engine,
>>>>>> I can get the same behavior with a newly created engine than with the
>>>>>> default Spring one (eg, no more TemplateEngine not initialised
>>>>>> correctly, no [resourceLoader] specified!). That does not help me much
>>>>>> though :(
>>>>>>
>>>>>> On Tue, Jul 22, 2008 at 8:57 PM, Jean-Noël Rivasseau
>>>>>> <[hidden email]> wrote:
>>>>>>> Despite my efforts and searching through the ML I still cannot get
>>>>>>> this to work.
>>>>>>>
>>>>>>> When getting a reference to the Spring configured GSP engine (obtained
>>>>>>> in the controller via dependency injection), no matter which Writer I
>>>>>>> use, the output is always written to the HTTP reply. This is my code:
>>>>>>>
>>>>>>>
>>>>>>>               def template =
>>>>>>> groovyPagesTemplateEngine.createTemplate("/customerOrder/_email.gsp")
>>>>>>>
>>>>>>>               def writeable = template.make()
>>>>>>>               def sw = new StringWriter()
>>>>>>>               def pw = new PrintWriter(sw)
>>>>>>>               writeable.writeTo(pw)
>>>>>>>               def content = sw.toString()
>>>>>>>               println content
>>>>>>>
>>>>>>> content is always empty, but the result of the template goes into the
>>>>>>> HTTP reply (the code is in service called by a controller). This is
>>>>>>> obviously not what I need.
>>>>>>>
>>>>>>> Note that writeable.writeTo() would produce the same effect and not an
>>>>>>> exception as I anticipated, strange...
>>>>>>>
>>>>>>> I also tried to create a new groovyPagesTemplateEngine like this:
>>>>>>>
>>>>>>>               GroovyPagesTemplateEngine engine = new
>>>>>>> GroovyPagesTemplateEngine()
>>>>>>>               def template =
>>>>>>> engine.createTemplate("/customerOrder/_email.gsp")
>>>>>>>
>>>>>>>               def writeable = template.make()
>>>>>>>               def sw = new StringWriter()
>>>>>>>               def pw = new PrintWriter(sw)
>>>>>>>               writeable.writeTo(pw)
>>>>>>>               def content = sw.toString()
>>>>>>>
>>>>>>> I get this exception:
>>>>>>>
>>>>>>> java.lang.IllegalStateException: TemplateEngine not initialised
>>>>>>> correctly, no [resourceLoader] specified!
>>>>>>>
>>>>>>> Any help?
>>>>>>>
>>>>>>> Jean-Noel
>>>>>>>
>>>>>>> On Fri, Jul 18, 2008 at 10:38 AM, Graeme Rocher <[hidden email]>
>>>>>>> wrote:
>>>>>>>> Also note you can get a reference to the Spring configured GSP engine
>>>>>>>> using:
>>>>>>>>
>>>>>>>> GroovyPagesTemplateEngine groovyPagesTemplateEngine
>>>>>>>>
>>>>>>>> Cheers
>>>>>>>>
>>>>>>>> On Fri, Jul 18, 2008 at 9:36 AM, Graeme Rocher <[hidden email]>
>>>>>>>> wrote:
>>>>>>>>> We already have that. See
>>>>>>>>> http://gordopazexperience.blogspot.com/2008/07/writing-groovy-server-pages-output-to.html
>>>>>>>>> for an example
>>>>>>>>>
>>>>>>>>> Cheers
>>>>>>>>>
>>>>>>>>> On Fri, Jul 18, 2008 at 5:27 AM, Daniel Honig
>>>>>>>>> <[hidden email]> wrote:
>>>>>>>>>> In other frameworks, such as ATG, we used to grab the
>>>>>>>>>> "PageCompileServlet" and simulate a request to render our
>>>>>>>>>> templates.
>>>>>>>>>> I think in grails it would be great to have similar functionality.
>>>>>>>>>> If
>>>>>>>>>> not Groovy seems to have good templating support...
>>>>>>>>>>
>>>>>>>>>> but I agree that what you really want is the ability to render full
>>>>>>>>>> blown GSP's and capture the output for use as an e-mail message
>>>>>>>>>> body
>>>>>>>>>>
>>>>>>>>>> On Thu, Jul 17, 2008 at 12:48 PM, Jean-Noël Rivasseau
>>>>>>>>>> <[hidden email]> wrote:
>>>>>>>>>>> Hello,
>>>>>>>>>>>
>>>>>>>>>>> I'd like to send emails from within a service. The view technology
>>>>>>>>>>> I
>>>>>>>>>>> naturally planned to use was GSP.
>>>>>>>>>>>
>>>>>>>>>>> How can I obtain a string from a rendered GSP page, within a
>>>>>>>>>>> service?
>>>>>>>>>>> I usually use g.render in the controller, but I never did that
>>>>>>>>>>> from
>>>>>>>>>>> within a service. I guess I need to get a reference to the GSP
>>>>>>>>>>> rendering engine.
>>>>>>>>>>>
>>>>>>>>>>> Thanks
>>>>>>>>>>>
>>>>>>>>>>> Jean-Noel
>>>>>>>>>>>
>>>>>>>>>>> ---------------------------------------------------------------------
>>>>>>>>>>> To unsubscribe from this list, please visit:
>>>>>>>>>>>
>>>>>>>>>>>   http://xircles.codehaus.org/manage_email
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> ---------------------------------------------------------------------
>>>>>>>>>> To unsubscribe from this list, please visit:
>>>>>>>>>>
>>>>>>>>>>   http://xircles.codehaus.org/manage_email
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> --
>>>>>>>>> Graeme Rocher
>>>>>>>>> Grails Project Lead
>>>>>>>>> G2One, Inc. Chief Technology Officer
>>>>>>>>> http://www.g2one.com
>>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>> --
>>>>>>>> Graeme Rocher
>>>>>>>> Grails Project Lead
>>>>>>>> G2One, Inc. Chief Technology Officer
>>>>>>>> http://www.g2one.com
>>>>>>>>
>>>>>>>> ---------------------------------------------------------------------
>>>>>>>> To unsubscribe from this list, please visit:
>>>>>>>>
>>>>>>>>   http://xircles.codehaus.org/manage_email
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>
>>>>>>
>>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> Graeme Rocher
>>>> Grails Project Lead
>>>> G2One, Inc. Chief Technology Officer
>>>> http://www.g2one.com
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe from this list, please visit:
>>>>
>>>>   http://xircles.codehaus.org/manage_email
>>>>
>>>>
>>>>
>>>
>>>
>>
>> --
>> View this message in context: http://grails.1312388.n4.nabble.com/g-render-from-a-Service-tp1352284p2964959.html
>> Sent from the Grails - user mailing list archive at Nabble.com.
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>    http://xircles.codehaus.org/manage_email
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: g.render from a Service?

elvanor
Hello,

This is interesting that this thread was recently picked up after 2
years. I just ran into this issue again today. I've noticed that
Graeme's workaround only works if there is a web request bound to the
current thread. Today I had to deal with emails that need to be sent
from a service and from a thread started by the Quartz plugin, so
there is no web request bound.

I still managed to solve my problem by looking at the Mail Plugin
source: what the author does is mocking a web request in this case.

It works but there is still a big problem (that also affects the Mail
Plugin, btw): how can I change the locale?
RequestContextUtils.getLocaleResolver(mockedRequest) returns null. So
there is no way that I see of changing the locale, which means
g.message is unavailable on the GSPs (which is problematic).

Is there a way to get out of this problem? Or do I need to file a JIRA
so that one can change the locale of a mocked web request?

Jean-Noel

On Sun, Oct 10, 2010 at 11:53 PM, James Cook <[hidden email]> wrote:

> You could use the Grails Template Engine plugin
> (http://www.grails.org/plugin/grails-template-engine).
> It probably still has the issue that it doesn't compile out of the
> bag, but it's an easy fix. We use it a lot :)
>
> James
>
> On 8 October 2010 06:22, Luke Daley <[hidden email]> wrote:
>> As far as I am aware this is currently the best solution.
>>
>> Please raise a JIRA with something like “make it easier to render a gsp template outside of a controller”.
>>
>> On 06/10/2010, at 11:34 PM, fabien7474 wrote:
>>
>>>
>>> Is there any progress on this issue OR a simpler solution than the hack
>>> described here?
>>>
>>> If not, is there an existing JIRA that I can at least vote for ?
>>>
>>> Thank you,
>>>
>>> Fabien.
>>>
>>>
>>> Jean-Noël Rivasseau wrote:
>>>>
>>>> Thanks Graeme - works perfectly. At last I got this part working right :)
>>>>
>>>> It clearly looks like a hack though... should I JIRA for an
>>>> architecture improvement that would allow more straightforward use of
>>>> the groovPagesTemplateEngine?
>>>>
>>>> Or maybe it will already be rewritten as part of 1.1 in order to make
>>>> GSP usable standalone? (I would guess this is the case).
>>>>
>>>> Jean-Noel
>>>>
>>>> On Wed, Jul 23, 2008 at 3:10 PM, Graeme Rocher <[hidden email]> wrote:
>>>>> Hmm i think i understand what the problem is. Try doing
>>>>>
>>>>> def webRequest = RequestContextHolder.currentRequestAttributes()
>>>>> def originalOut = webRequest.out
>>>>> try {
>>>>>
>>>>>  ... // create print writer pw
>>>>>  webRequest.out = pw
>>>>>  ...
>>>>>  // use groovPagesTemplateEngine
>>>>>
>>>>> } finally {
>>>>> webRequest.out = originalOut
>>>>> }
>>>>>
>>>>>
>>>>> Cheers
>>>>>
>>>>> On Wed, Jul 23, 2008 at 1:25 PM, Jean-Noël Rivasseau <[hidden email]>
>>>>> wrote:
>>>>>> Still stuck, anyone?
>>>>>>
>>>>>> On Tue, Jul 22, 2008 at 9:43 PM, Jean-Noël Rivasseau <[hidden email]>
>>>>>> wrote:
>>>>>>> Follow-up: by creating a Spring Resource and feeding it to the engine,
>>>>>>> I can get the same behavior with a newly created engine than with the
>>>>>>> default Spring one (eg, no more TemplateEngine not initialised
>>>>>>> correctly, no [resourceLoader] specified!). That does not help me much
>>>>>>> though :(
>>>>>>>
>>>>>>> On Tue, Jul 22, 2008 at 8:57 PM, Jean-Noël Rivasseau
>>>>>>> <[hidden email]> wrote:
>>>>>>>> Despite my efforts and searching through the ML I still cannot get
>>>>>>>> this to work.
>>>>>>>>
>>>>>>>> When getting a reference to the Spring configured GSP engine (obtained
>>>>>>>> in the controller via dependency injection), no matter which Writer I
>>>>>>>> use, the output is always written to the HTTP reply. This is my code:
>>>>>>>>
>>>>>>>>
>>>>>>>>               def template =
>>>>>>>> groovyPagesTemplateEngine.createTemplate("/customerOrder/_email.gsp")
>>>>>>>>
>>>>>>>>               def writeable = template.make()
>>>>>>>>               def sw = new StringWriter()
>>>>>>>>               def pw = new PrintWriter(sw)
>>>>>>>>               writeable.writeTo(pw)
>>>>>>>>               def content = sw.toString()
>>>>>>>>               println content
>>>>>>>>
>>>>>>>> content is always empty, but the result of the template goes into the
>>>>>>>> HTTP reply (the code is in service called by a controller). This is
>>>>>>>> obviously not what I need.
>>>>>>>>
>>>>>>>> Note that writeable.writeTo() would produce the same effect and not an
>>>>>>>> exception as I anticipated, strange...
>>>>>>>>
>>>>>>>> I also tried to create a new groovyPagesTemplateEngine like this:
>>>>>>>>
>>>>>>>>               GroovyPagesTemplateEngine engine = new
>>>>>>>> GroovyPagesTemplateEngine()
>>>>>>>>               def template =
>>>>>>>> engine.createTemplate("/customerOrder/_email.gsp")
>>>>>>>>
>>>>>>>>               def writeable = template.make()
>>>>>>>>               def sw = new StringWriter()
>>>>>>>>               def pw = new PrintWriter(sw)
>>>>>>>>               writeable.writeTo(pw)
>>>>>>>>               def content = sw.toString()
>>>>>>>>
>>>>>>>> I get this exception:
>>>>>>>>
>>>>>>>> java.lang.IllegalStateException: TemplateEngine not initialised
>>>>>>>> correctly, no [resourceLoader] specified!
>>>>>>>>
>>>>>>>> Any help?
>>>>>>>>
>>>>>>>> Jean-Noel
>>>>>>>>
>>>>>>>> On Fri, Jul 18, 2008 at 10:38 AM, Graeme Rocher <[hidden email]>
>>>>>>>> wrote:
>>>>>>>>> Also note you can get a reference to the Spring configured GSP engine
>>>>>>>>> using:
>>>>>>>>>
>>>>>>>>> GroovyPagesTemplateEngine groovyPagesTemplateEngine
>>>>>>>>>
>>>>>>>>> Cheers
>>>>>>>>>
>>>>>>>>> On Fri, Jul 18, 2008 at 9:36 AM, Graeme Rocher <[hidden email]>
>>>>>>>>> wrote:
>>>>>>>>>> We already have that. See
>>>>>>>>>> http://gordopazexperience.blogspot.com/2008/07/writing-groovy-server-pages-output-to.html
>>>>>>>>>> for an example
>>>>>>>>>>
>>>>>>>>>> Cheers
>>>>>>>>>>
>>>>>>>>>> On Fri, Jul 18, 2008 at 5:27 AM, Daniel Honig
>>>>>>>>>> <[hidden email]> wrote:
>>>>>>>>>>> In other frameworks, such as ATG, we used to grab the
>>>>>>>>>>> "PageCompileServlet" and simulate a request to render our
>>>>>>>>>>> templates.
>>>>>>>>>>> I think in grails it would be great to have similar functionality.
>>>>>>>>>>> If
>>>>>>>>>>> not Groovy seems to have good templating support...
>>>>>>>>>>>
>>>>>>>>>>> but I agree that what you really want is the ability to render full
>>>>>>>>>>> blown GSP's and capture the output for use as an e-mail message
>>>>>>>>>>> body
>>>>>>>>>>>
>>>>>>>>>>> On Thu, Jul 17, 2008 at 12:48 PM, Jean-Noël Rivasseau
>>>>>>>>>>> <[hidden email]> wrote:
>>>>>>>>>>>> Hello,
>>>>>>>>>>>>
>>>>>>>>>>>> I'd like to send emails from within a service. The view technology
>>>>>>>>>>>> I
>>>>>>>>>>>> naturally planned to use was GSP.
>>>>>>>>>>>>
>>>>>>>>>>>> How can I obtain a string from a rendered GSP page, within a
>>>>>>>>>>>> service?
>>>>>>>>>>>> I usually use g.render in the controller, but I never did that
>>>>>>>>>>>> from
>>>>>>>>>>>> within a service. I guess I need to get a reference to the GSP
>>>>>>>>>>>> rendering engine.
>>>>>>>>>>>>
>>>>>>>>>>>> Thanks
>>>>>>>>>>>>
>>>>>>>>>>>> Jean-Noel
>>>>>>>>>>>>
>>>>>>>>>>>> ---------------------------------------------------------------------
>>>>>>>>>>>> To unsubscribe from this list, please visit:
>>>>>>>>>>>>
>>>>>>>>>>>>   http://xircles.codehaus.org/manage_email
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>> ---------------------------------------------------------------------
>>>>>>>>>>> To unsubscribe from this list, please visit:
>>>>>>>>>>>
>>>>>>>>>>>   http://xircles.codehaus.org/manage_email
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> --
>>>>>>>>>> Graeme Rocher
>>>>>>>>>> Grails Project Lead
>>>>>>>>>> G2One, Inc. Chief Technology Officer
>>>>>>>>>> http://www.g2one.com
>>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> --
>>>>>>>>> Graeme Rocher
>>>>>>>>> Grails Project Lead
>>>>>>>>> G2One, Inc. Chief Technology Officer
>>>>>>>>> http://www.g2one.com
>>>>>>>>>
>>>>>>>>> ---------------------------------------------------------------------
>>>>>>>>> To unsubscribe from this list, please visit:
>>>>>>>>>
>>>>>>>>>   http://xircles.codehaus.org/manage_email
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>
>>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> Graeme Rocher
>>>>> Grails Project Lead
>>>>> G2One, Inc. Chief Technology Officer
>>>>> http://www.g2one.com
>>>>>
>>>>> ---------------------------------------------------------------------
>>>>> To unsubscribe from this list, please visit:
>>>>>
>>>>>   http://xircles.codehaus.org/manage_email
>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>
>>> --
>>> View this message in context: http://grails.1312388.n4.nabble.com/g-render-from-a-Service-tp1352284p2964959.html
>>> Sent from the Grails - user mailing list archive at Nabble.com.
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe from this list, please visit:
>>>
>>>    http://xircles.codehaus.org/manage_email
>>>
>>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>    http://xircles.codehaus.org/manage_email
>>
>>
>>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: g.render from a Service?

houbie
When rendering from a service, it makes IMHO more sense to explicitly put the locale in the gsp's model and to use that locale in the message tag: <g:message code="aCode" locale="${myModel.locale}" />

Grtz,
Ivo

On 13 Oct 2010, at 18:46, Jean-Noël Rivasseau wrote:

> Hello,
>
> This is interesting that this thread was recently picked up after 2
> years. I just ran into this issue again today. I've noticed that
> Graeme's workaround only works if there is a web request bound to the
> current thread. Today I had to deal with emails that need to be sent
> from a service and from a thread started by the Quartz plugin, so
> there is no web request bound.
>
> I still managed to solve my problem by looking at the Mail Plugin
> source: what the author does is mocking a web request in this case.
>
> It works but there is still a big problem (that also affects the Mail
> Plugin, btw): how can I change the locale?
> RequestContextUtils.getLocaleResolver(mockedRequest) returns null. So
> there is no way that I see of changing the locale, which means
> g.message is unavailable on the GSPs (which is problematic).
>
> Is there a way to get out of this problem? Or do I need to file a JIRA
> so that one can change the locale of a mocked web request?
>
> Jean-Noel
>
> On Sun, Oct 10, 2010 at 11:53 PM, James Cook <[hidden email]> wrote:
>> You could use the Grails Template Engine plugin
>> (http://www.grails.org/plugin/grails-template-engine).
>> It probably still has the issue that it doesn't compile out of the
>> bag, but it's an easy fix. We use it a lot :)
>>
>> James
>>
>> On 8 October 2010 06:22, Luke Daley <[hidden email]> wrote:
>>> As far as I am aware this is currently the best solution.
>>>
>>> Please raise a JIRA with something like “make it easier to render a gsp template outside of a controller”.
>>>
>>> On 06/10/2010, at 11:34 PM, fabien7474 wrote:
>>>
>>>>
>>>> Is there any progress on this issue OR a simpler solution than the hack
>>>> described here?
>>>>
>>>> If not, is there an existing JIRA that I can at least vote for ?
>>>>
>>>> Thank you,
>>>>
>>>> Fabien.
>>>>
>>>>
>>>> Jean-Noël Rivasseau wrote:
>>>>>
>>>>> Thanks Graeme - works perfectly. At last I got this part working right :)
>>>>>
>>>>> It clearly looks like a hack though... should I JIRA for an
>>>>> architecture improvement that would allow more straightforward use of
>>>>> the groovPagesTemplateEngine?
>>>>>
>>>>> Or maybe it will already be rewritten as part of 1.1 in order to make
>>>>> GSP usable standalone? (I would guess this is the case).
>>>>>
>>>>> Jean-Noel
>>>>>
>>>>> On Wed, Jul 23, 2008 at 3:10 PM, Graeme Rocher <[hidden email]> wrote:
>>>>>> Hmm i think i understand what the problem is. Try doing
>>>>>>
>>>>>> def webRequest = RequestContextHolder.currentRequestAttributes()
>>>>>> def originalOut = webRequest.out
>>>>>> try {
>>>>>>
>>>>>>  ... // create print writer pw
>>>>>>  webRequest.out = pw
>>>>>>  ...
>>>>>>  // use groovPagesTemplateEngine
>>>>>>
>>>>>> } finally {
>>>>>> webRequest.out = originalOut
>>>>>> }
>>>>>>
>>>>>>
>>>>>> Cheers
>>>>>>
>>>>>> On Wed, Jul 23, 2008 at 1:25 PM, Jean-Noël Rivasseau <[hidden email]>
>>>>>> wrote:
>>>>>>> Still stuck, anyone?
>>>>>>>
>>>>>>> On Tue, Jul 22, 2008 at 9:43 PM, Jean-Noël Rivasseau <[hidden email]>
>>>>>>> wrote:
>>>>>>>> Follow-up: by creating a Spring Resource and feeding it to the engine,
>>>>>>>> I can get the same behavior with a newly created engine than with the
>>>>>>>> default Spring one (eg, no more TemplateEngine not initialised
>>>>>>>> correctly, no [resourceLoader] specified!). That does not help me much
>>>>>>>> though :(
>>>>>>>>
>>>>>>>> On Tue, Jul 22, 2008 at 8:57 PM, Jean-Noël Rivasseau
>>>>>>>> <[hidden email]> wrote:
>>>>>>>>> Despite my efforts and searching through the ML I still cannot get
>>>>>>>>> this to work.
>>>>>>>>>
>>>>>>>>> When getting a reference to the Spring configured GSP engine (obtained
>>>>>>>>> in the controller via dependency injection), no matter which Writer I
>>>>>>>>> use, the output is always written to the HTTP reply. This is my code:
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>               def template =
>>>>>>>>> groovyPagesTemplateEngine.createTemplate("/customerOrder/_email.gsp")
>>>>>>>>>
>>>>>>>>>               def writeable = template.make()
>>>>>>>>>               def sw = new StringWriter()
>>>>>>>>>               def pw = new PrintWriter(sw)
>>>>>>>>>               writeable.writeTo(pw)
>>>>>>>>>               def content = sw.toString()
>>>>>>>>>               println content
>>>>>>>>>
>>>>>>>>> content is always empty, but the result of the template goes into the
>>>>>>>>> HTTP reply (the code is in service called by a controller). This is
>>>>>>>>> obviously not what I need.
>>>>>>>>>
>>>>>>>>> Note that writeable.writeTo() would produce the same effect and not an
>>>>>>>>> exception as I anticipated, strange...
>>>>>>>>>
>>>>>>>>> I also tried to create a new groovyPagesTemplateEngine like this:
>>>>>>>>>
>>>>>>>>>               GroovyPagesTemplateEngine engine = new
>>>>>>>>> GroovyPagesTemplateEngine()
>>>>>>>>>               def template =
>>>>>>>>> engine.createTemplate("/customerOrder/_email.gsp")
>>>>>>>>>
>>>>>>>>>               def writeable = template.make()
>>>>>>>>>               def sw = new StringWriter()
>>>>>>>>>               def pw = new PrintWriter(sw)
>>>>>>>>>               writeable.writeTo(pw)
>>>>>>>>>               def content = sw.toString()
>>>>>>>>>
>>>>>>>>> I get this exception:
>>>>>>>>>
>>>>>>>>> java.lang.IllegalStateException: TemplateEngine not initialised
>>>>>>>>> correctly, no [resourceLoader] specified!
>>>>>>>>>
>>>>>>>>> Any help?
>>>>>>>>>
>>>>>>>>> Jean-Noel
>>>>>>>>>
>>>>>>>>> On Fri, Jul 18, 2008 at 10:38 AM, Graeme Rocher <[hidden email]>
>>>>>>>>> wrote:
>>>>>>>>>> Also note you can get a reference to the Spring configured GSP engine
>>>>>>>>>> using:
>>>>>>>>>>
>>>>>>>>>> GroovyPagesTemplateEngine groovyPagesTemplateEngine
>>>>>>>>>>
>>>>>>>>>> Cheers
>>>>>>>>>>
>>>>>>>>>> On Fri, Jul 18, 2008 at 9:36 AM, Graeme Rocher <[hidden email]>
>>>>>>>>>> wrote:
>>>>>>>>>>> We already have that. See
>>>>>>>>>>> http://gordopazexperience.blogspot.com/2008/07/writing-groovy-server-pages-output-to.html
>>>>>>>>>>> for an example
>>>>>>>>>>>
>>>>>>>>>>> Cheers
>>>>>>>>>>>
>>>>>>>>>>> On Fri, Jul 18, 2008 at 5:27 AM, Daniel Honig
>>>>>>>>>>> <[hidden email]> wrote:
>>>>>>>>>>>> In other frameworks, such as ATG, we used to grab the
>>>>>>>>>>>> "PageCompileServlet" and simulate a request to render our
>>>>>>>>>>>> templates.
>>>>>>>>>>>> I think in grails it would be great to have similar functionality.
>>>>>>>>>>>> If
>>>>>>>>>>>> not Groovy seems to have good templating support...
>>>>>>>>>>>>
>>>>>>>>>>>> but I agree that what you really want is the ability to render full
>>>>>>>>>>>> blown GSP's and capture the output for use as an e-mail message
>>>>>>>>>>>> body
>>>>>>>>>>>>
>>>>>>>>>>>> On Thu, Jul 17, 2008 at 12:48 PM, Jean-Noël Rivasseau
>>>>>>>>>>>> <[hidden email]> wrote:
>>>>>>>>>>>>> Hello,
>>>>>>>>>>>>>
>>>>>>>>>>>>> I'd like to send emails from within a service. The view technology
>>>>>>>>>>>>> I
>>>>>>>>>>>>> naturally planned to use was GSP.
>>>>>>>>>>>>>
>>>>>>>>>>>>> How can I obtain a string from a rendered GSP page, within a
>>>>>>>>>>>>> service?
>>>>>>>>>>>>> I usually use g.render in the controller, but I never did that
>>>>>>>>>>>>> from
>>>>>>>>>>>>> within a service. I guess I need to get a reference to the GSP
>>>>>>>>>>>>> rendering engine.
>>>>>>>>>>>>>
>>>>>>>>>>>>> Thanks
>>>>>>>>>>>>>
>>>>>>>>>>>>> Jean-Noel
>>>>>>>>>>>>>
>>>>>>>>>>>>> ---------------------------------------------------------------------
>>>>>>>>>>>>> To unsubscribe from this list, please visit:
>>>>>>>>>>>>>
>>>>>>>>>>>>>   http://xircles.codehaus.org/manage_email
>>>>>>>>>>>>>
>>>>>>>>>>>>>
>>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>> ---------------------------------------------------------------------
>>>>>>>>>>>> To unsubscribe from this list, please visit:
>>>>>>>>>>>>
>>>>>>>>>>>>   http://xircles.codehaus.org/manage_email
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>> --
>>>>>>>>>>> Graeme Rocher
>>>>>>>>>>> Grails Project Lead
>>>>>>>>>>> G2One, Inc. Chief Technology Officer
>>>>>>>>>>> http://www.g2one.com
>>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> --
>>>>>>>>>> Graeme Rocher
>>>>>>>>>> Grails Project Lead
>>>>>>>>>> G2One, Inc. Chief Technology Officer
>>>>>>>>>> http://www.g2one.com
>>>>>>>>>>
>>>>>>>>>> ---------------------------------------------------------------------
>>>>>>>>>> To unsubscribe from this list, please visit:
>>>>>>>>>>
>>>>>>>>>>   http://xircles.codehaus.org/manage_email
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>
>>>>>>>>
>>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> --
>>>>>> Graeme Rocher
>>>>>> Grails Project Lead
>>>>>> G2One, Inc. Chief Technology Officer
>>>>>> http://www.g2one.com
>>>>>>
>>>>>> ---------------------------------------------------------------------
>>>>>> To unsubscribe from this list, please visit:
>>>>>>
>>>>>>   http://xircles.codehaus.org/manage_email
>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>
>>>> --
>>>> View this message in context: http://grails.1312388.n4.nabble.com/g-render-from-a-Service-tp1352284p2964959.html
>>>> Sent from the Grails - user mailing list archive at Nabble.com.
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe from this list, please visit:
>>>>
>>>>    http://xircles.codehaus.org/manage_email
>>>>
>>>>
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe from this list, please visit:
>>>
>>>    http://xircles.codehaus.org/manage_email
>>>
>>>
>>>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>

Ivo Houbrechts

Veldstraat 50
2460 Lichtaart
tel: +32 476 367360


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: g.render from a Service?

elvanor
Yes, this could work. But anyway I found how to change the locale on a mocked Spring web request, so I am happy. Thanks!

On Fri, Oct 15, 2010 at 11:07 AM, Ivo Houbrechts <[hidden email]> wrote:
When rendering from a service, it makes IMHO more sense to explicitly put the locale in the gsp's model and to use that locale in the message tag: <g:message code="aCode" locale="${myModel.locale}" />

Grtz,
Ivo

On 13 Oct 2010, at 18:46, Jean-Noël Rivasseau wrote:

> Hello,
>
> This is interesting that this thread was recently picked up after 2
> years. I just ran into this issue again today. I've noticed that
> Graeme's workaround only works if there is a web request bound to the
> current thread. Today I had to deal with emails that need to be sent
> from a service and from a thread started by the Quartz plugin, so
> there is no web request bound.
>
> I still managed to solve my problem by looking at the Mail Plugin
> source: what the author does is mocking a web request in this case.
>
> It works but there is still a big problem (that also affects the Mail
> Plugin, btw): how can I change the locale?
> RequestContextUtils.getLocaleResolver(mockedRequest) returns null. So
> there is no way that I see of changing the locale, which means
> g.message is unavailable on the GSPs (which is problematic).
>
> Is there a way to get out of this problem? Or do I need to file a JIRA
> so that one can change the locale of a mocked web request?
>
> Jean-Noel
>
> On Sun, Oct 10, 2010 at 11:53 PM, James Cook <[hidden email]> wrote:
>> You could use the Grails Template Engine plugin
>> (http://www.grails.org/plugin/grails-template-engine).
>> It probably still has the issue that it doesn't compile out of the
>> bag, but it's an easy fix. We use it a lot :)
>>
>> James
>>
>> On 8 October 2010 06:22, Luke Daley <[hidden email]> wrote:
>>> As far as I am aware this is currently the best solution.
>>>
>>> Please raise a JIRA with something like “make it easier to render a gsp template outside of a controller”.
>>>
>>> On 06/10/2010, at 11:34 PM, fabien7474 wrote:
>>>
>>>>
>>>> Is there any progress on this issue OR a simpler solution than the hack
>>>> described here?
>>>>
>>>> If not, is there an existing JIRA that I can at least vote for ?
>>>>
>>>> Thank you,
>>>>
>>>> Fabien.
>>>>
>>>>
>>>> Jean-Noël Rivasseau wrote:
>>>>>
>>>>> Thanks Graeme - works perfectly. At last I got this part working right :)
>>>>>
>>>>> It clearly looks like a hack though... should I JIRA for an
>>>>> architecture improvement that would allow more straightforward use of
>>>>> the groovPagesTemplateEngine?
>>>>>
>>>>> Or maybe it will already be rewritten as part of 1.1 in order to make
>>>>> GSP usable standalone? (I would guess this is the case).
>>>>>
>>>>> Jean-Noel
>>>>>
>>>>> On Wed, Jul 23, 2008 at 3:10 PM, Graeme Rocher <[hidden email]> wrote:
>>>>>> Hmm i think i understand what the problem is. Try doing
>>>>>>
>>>>>> def webRequest = RequestContextHolder.currentRequestAttributes()
>>>>>> def originalOut = webRequest.out
>>>>>> try {
>>>>>>
>>>>>>  ... // create print writer pw
>>>>>>  webRequest.out = pw
>>>>>>  ...
>>>>>>  // use groovPagesTemplateEngine
>>>>>>
>>>>>> } finally {
>>>>>> webRequest.out = originalOut
>>>>>> }
>>>>>>
>>>>>>
>>>>>> Cheers
>>>>>>
>>>>>> On Wed, Jul 23, 2008 at 1:25 PM, Jean-Noël Rivasseau <[hidden email]>
>>>>>> wrote:
>>>>>>> Still stuck, anyone?
>>>>>>>
>>>>>>> On Tue, Jul 22, 2008 at 9:43 PM, Jean-Noël Rivasseau <[hidden email]>
>>>>>>> wrote:
>>>>>>>> Follow-up: by creating a Spring Resource and feeding it to the engine,
>>>>>>>> I can get the same behavior with a newly created engine than with the
>>>>>>>> default Spring one (eg, no more TemplateEngine not initialised
>>>>>>>> correctly, no [resourceLoader] specified!). That does not help me much
>>>>>>>> though :(
>>>>>>>>
>>>>>>>> On Tue, Jul 22, 2008 at 8:57 PM, Jean-Noël Rivasseau
>>>>>>>> <[hidden email]> wrote:
>>>>>>>>> Despite my efforts and searching through the ML I still cannot get
>>>>>>>>> this to work.
>>>>>>>>>
>>>>>>>>> When getting a reference to the Spring configured GSP engine (obtained
>>>>>>>>> in the controller via dependency injection), no matter which Writer I
>>>>>>>>> use, the output is always written to the HTTP reply. This is my code:
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>               def template =
>>>>>>>>> groovyPagesTemplateEngine.createTemplate("/customerOrder/_email.gsp")
>>>>>>>>>
>>>>>>>>>               def writeable = template.make()
>>>>>>>>>               def sw = new StringWriter()
>>>>>>>>>               def pw = new PrintWriter(sw)
>>>>>>>>>               writeable.writeTo(pw)
>>>>>>>>>               def content = sw.toString()
>>>>>>>>>               println content
>>>>>>>>>
>>>>>>>>> content is always empty, but the result of the template goes into the
>>>>>>>>> HTTP reply (the code is in service called by a controller). This is
>>>>>>>>> obviously not what I need.
>>>>>>>>>
>>>>>>>>> Note that writeable.writeTo() would produce the same effect and not an
>>>>>>>>> exception as I anticipated, strange...
>>>>>>>>>
>>>>>>>>> I also tried to create a new groovyPagesTemplateEngine like this:
>>>>>>>>>
>>>>>>>>>               GroovyPagesTemplateEngine engine = new
>>>>>>>>> GroovyPagesTemplateEngine()
>>>>>>>>>               def template =
>>>>>>>>> engine.createTemplate("/customerOrder/_email.gsp")
>>>>>>>>>
>>>>>>>>>               def writeable = template.make()
>>>>>>>>>               def sw = new StringWriter()
>>>>>>>>>               def pw = new PrintWriter(sw)
>>>>>>>>>               writeable.writeTo(pw)
>>>>>>>>>               def content = sw.toString()
>>>>>>>>>
>>>>>>>>> I get this exception:
>>>>>>>>>
>>>>>>>>> java.lang.IllegalStateException: TemplateEngine not initialised
>>>>>>>>> correctly, no [resourceLoader] specified!
>>>>>>>>>
>>>>>>>>> Any help?
>>>>>>>>>
>>>>>>>>> Jean-Noel
>>>>>>>>>
>>>>>>>>> On Fri, Jul 18, 2008 at 10:38 AM, Graeme Rocher <[hidden email]>
>>>>>>>>> wrote:
>>>>>>>>>> Also note you can get a reference to the Spring configured GSP engine
>>>>>>>>>> using:
>>>>>>>>>>
>>>>>>>>>> GroovyPagesTemplateEngine groovyPagesTemplateEngine
>>>>>>>>>>
>>>>>>>>>> Cheers
>>>>>>>>>>
>>>>>>>>>> On Fri, Jul 18, 2008 at 9:36 AM, Graeme Rocher <[hidden email]>
>>>>>>>>>> wrote:
>>>>>>>>>>> We already have that. See
>>>>>>>>>>> http://gordopazexperience.blogspot.com/2008/07/writing-groovy-server-pages-output-to.html
>>>>>>>>>>> for an example
>>>>>>>>>>>
>>>>>>>>>>> Cheers
>>>>>>>>>>>
>>>>>>>>>>> On Fri, Jul 18, 2008 at 5:27 AM, Daniel Honig
>>>>>>>>>>> <[hidden email]> wrote:
>>>>>>>>>>>> In other frameworks, such as ATG, we used to grab the
>>>>>>>>>>>> "PageCompileServlet" and simulate a request to render our
>>>>>>>>>>>> templates.
>>>>>>>>>>>> I think in grails it would be great to have similar functionality.
>>>>>>>>>>>> If
>>>>>>>>>>>> not Groovy seems to have good templating support...
>>>>>>>>>>>>
>>>>>>>>>>>> but I agree that what you really want is the ability to render full
>>>>>>>>>>>> blown GSP's and capture the output for use as an e-mail message
>>>>>>>>>>>> body
>>>>>>>>>>>>
>>>>>>>>>>>> On Thu, Jul 17, 2008 at 12:48 PM, Jean-Noël Rivasseau
>>>>>>>>>>>> <[hidden email]> wrote:
>>>>>>>>>>>>> Hello,
>>>>>>>>>>>>>
>>>>>>>>>>>>> I'd like to send emails from within a service. The view technology
>>>>>>>>>>>>> I
>>>>>>>>>>>>> naturally planned to use was GSP.
>>>>>>>>>>>>>
>>>>>>>>>>>>> How can I obtain a string from a rendered GSP page, within a
>>>>>>>>>>>>> service?
>>>>>>>>>>>>> I usually use g.render in the controller, but I never did that
>>>>>>>>>>>>> from
>>>>>>>>>>>>> within a service. I guess I need to get a reference to the GSP
>>>>>>>>>>>>> rendering engine.
>>>>>>>>>>>>>
>>>>>>>>>>>>> Thanks
>>>>>>>>>>>>>
>>>>>>>>>>>>> Jean-Noel
>>>>>>>>>>>>>
>>>>>>>>>>>>> ---------------------------------------------------------------------
>>>>>>>>>>>>> To unsubscribe from this list, please visit:
>>>>>>>>>>>>>
>>>>>>>>>>>>>   http://xircles.codehaus.org/manage_email
>>>>>>>>>>>>>
>>>>>>>>>>>>>
>>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>> ---------------------------------------------------------------------
>>>>>>>>>>>> To unsubscribe from this list, please visit:
>>>>>>>>>>>>
>>>>>>>>>>>>   http://xircles.codehaus.org/manage_email
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>> --
>>>>>>>>>>> Graeme Rocher
>>>>>>>>>>> Grails Project Lead
>>>>>>>>>>> G2One, Inc. Chief Technology Officer
>>>>>>>>>>> http://www.g2one.com
>>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> --
>>>>>>>>>> Graeme Rocher
>>>>>>>>>> Grails Project Lead
>>>>>>>>>> G2One, Inc. Chief Technology Officer
>>>>>>>>>> http://www.g2one.com
>>>>>>>>>>
>>>>>>>>>> ---------------------------------------------------------------------
>>>>>>>>>> To unsubscribe from this list, please visit:
>>>>>>>>>>
>>>>>>>>>>   http://xircles.codehaus.org/manage_email
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>
>>>>>>>>
>>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> --
>>>>>> Graeme Rocher
>>>>>> Grails Project Lead
>>>>>> G2One, Inc. Chief Technology Officer
>>>>>> http://www.g2one.com
>>>>>>
>>>>>> ---------------------------------------------------------------------
>>>>>> To unsubscribe from this list, please visit:
>>>>>>
>>>>>>   http://xircles.codehaus.org/manage_email
>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>
>>>> --
>>>> View this message in context: http://grails.1312388.n4.nabble.com/g-render-from-a-Service-tp1352284p2964959.html
>>>> Sent from the Grails - user mailing list archive at Nabble.com.
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe from this list, please visit:
>>>>
>>>>    http://xircles.codehaus.org/manage_email
>>>>
>>>>
>>>
>>>
>>> ---------------------------------------------------------------------