You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Muhammad Gelbana <m....@gmail.com> on 2013/05/30 18:26:37 UTC

[t5] Is it possible to return a component's generated HTML ?

Hopefully I'll explain this clearly.

I'm willing to integrate my tapestry web application with another web
application. That other web application will request ajax urls from my
application.

In my application should reply in HTML. So it would be much easier to be
able to use the regular tapestry component\page tml to construct the HTML
response. So that I can easily bind the tml to my component\page and let
tapestry do it's magic on generating the HTML.

Is this possible in tapestry ?

Re: [t5] Is it possible to return a component's generated HTML ?

Posted by Dmitry Gusev <dm...@gmail.com>.
You can put your component to a block and render this block.

Have a look at this thread:
http://mail-archives.apache.org/mod_mbox/tapestry-users/201304.mbox/%3cCA+v6cT9Um-Li7VXvaFem+abwrxF-B9aR3t+x40EeR971ZbLk5A@mail.gmail.com%3e

On Thu, May 30, 2013 at 8:26 PM, Muhammad Gelbana <m....@gmail.com>wrote:

> Hopefully I'll explain this clearly.
>
> I'm willing to integrate my tapestry web application with another web
> application. That other web application will request ajax urls from my
> application.
>
> In my application should reply in HTML. So it would be much easier to be
> able to use the regular tapestry component\page tml to construct the HTML
> response. So that I can easily bind the tml to my component\page and let
> tapestry do it's magic on generating the HTML.
>
> Is this possible in tapestry ?
>



-- 
Dmitry Gusev

AnjLab Team
http://anjlab.com

Re: [t5] Is it possible to return a component's generated HTML ?

Posted by Dmitry Gusev <dm...@gmail.com>.
If you only need to get HTML on the client and not on the server-side,
then you may just use Tapestry's built-in logic for handling AJAX requests.

On the server side create eventLink:

Link link = resources.createEventLink(eventType, contextValues);
return link.toAbsoluteURI();

Pass this link to client side during rendering and when needed do the AJAX
calls using, say, jQuery.ajax.

You may handle ajax request on the server side the same way I wrote in my
example above --
return block from event handler or use ajaxResponseRenderer.addRender().
On the client-side response will be the same JSON and you may extract
content from it.

Note that in both cases it is possible to render multiple blocks by one
request -- this is if you call
ajaxResponseRenderer.addRender() multiple times.


On Thu, Jun 6, 2013 at 1:12 AM, Muhammad Gelbana <m....@gmail.com>wrote:

> Actually Lance's second approach is what I need to do. And yes it should be
> very simple, I will only have to display regular html without controls at
> all (Hopefully it will stay this way and I actually think it will)
>
> Regards
>
>
> On Wed, Jun 5, 2013 at 8:00 PM, Dmitry Gusev <dm...@gmail.com>
> wrote:
>
> > The entry point is:
> >
> > String rawAddress = renderer.render(new RenderEvent(
> >                 "internal/companyblocks", "companyAddress", company));
> >
> >
> > You tell here that onCompanyAddress method (or method with
> > @Event("companyAddress")) should be invoked on the internal/companyblocks
> > page
> > class and if that method returns a Block - it will be rendered to a
> string.
> > company - is a parameter to that method that you may use to initialize
> > rendering context for the block.
> > You can also not return Block from the method by just addRenderer:
> >
> > ajaxResponseRenderer.addRender(addressBlockZone);
> >
> > And the addressBlockZone will be rendered to as string in this case.
> >
> > Resulting string will be serialized json object, so you can inspect its
> > structure and take the content you want like:
> >
> >         String htmlAddress = new
> > JSONObject(rawAddress).getString("content");
> >
> >
> > On Wed, Jun 5, 2013 at 8:14 PM, Muhammad Gelbana <m.gelbana@gmail.com
> > >wrote:
> >
> > > Honestly, I can't figure out anything from these 2 resources ! It all
> > looks
> > > so complicated to me.
> > >
> > > @Dmitry
> > > What is the entry point of your code ? When an ajaxrequest is sent to
> my
> > > application, doesn't it reach your "CompanyBlocks" page ? Then when
> does
> > > all the other services begin working ?!
> > >
> > > @Lanve
> > > And I thought Dimitry's code was complicated :D
> > > Your code was even harder to look at, I also can't understand the entry
> > > point and the flow of data starting when I receive an ajax request
> till I
> > > respond with a string representation of a component's template file.
> > >
> > > Thank you all for your help :)
> > >
> > >
> > > On Thu, May 30, 2013 at 6:41 PM, Lance Java <lance.java@googlemail.com
> > > >wrote:
> > >
> > > > I'm not 100% sure what your implementation will be but I've done some
> > > work
> > > > in this area.
> > > >
> > > > Take a look at PDFLink.getFoAsString() where I convert a
> RenderCommand
> > > to a
> > > > String of markup (XML in the example)
> > > >
> > > > http://tapestry-stitch.uklance.cloudbees.net/pdflinkdemo
> > > >
> > > > Also, take a look at this wiki entry
> > > >
> > > >
> > > >
> > >
> >
> http://wiki.apache.org/tapestry/Tapestry5HowToGetAnHTMLStringFromARenderCommandParameter
> > > >
> > > > Note that a Block can be type coerced to a RenderCommand
> > > >
> > >
> >
> >
> >
> > --
> > Dmitry Gusev
> >
> > AnjLab Team
> > http://anjlab.com
> >
>



-- 
Dmitry Gusev

AnjLab Team
http://anjlab.com

Re: [t5] Is it possible to return a component's generated HTML ?

Posted by Lance Java <la...@googlemail.com>.
I started to extract the logic to a separate project here
https://github.com/uklance/tapestry-offline but never got around to testing
it.

The main functionality is in this class
https://github.com/uklance/tapestry-offline/blob/master/src/main/java/org/lazan/t5/offline/services/internal/OfflineComponentRendererImpl.java

Let me know if it's useful, pull requests welcome :)

Re: [t5] Is it possible to return a component's generated HTML ?

Posted by Rural Hunter <ru...@gmail.com>.
Yes. I'm already using freemarker. Just want to use less dependencies 
and get all things unified since we already have one(Tapestry) right?

于 2013/6/7 21:04, Thiago H de Paula Figueiredo 写道:
> On Fri, 07 Jun 2013 06:45:32 -0300, Rural Hunter 
> <ru...@gmail.com> wrote:
>
>> cool, thanks! I am trying to implement a similar requirement as the 
>> OP which is to send html formatted mails in a background service of 
>> my webapp while there is no actual request/response involved. I'm 
>> trying to figure out how to use Tapestry template as the mail 
>> template in some way.
>
> Wouldn't a general-purpose templating entigne like FreeMarker be a 
> better choice for this scenario (e-mails)? Is there anything from the 
> webapp that you could reuse here?
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: [t5] Is it possible to return a component's generated HTML ?

Posted by Thiago H de Paula Figueiredo <th...@gmail.com>.
On Fri, 07 Jun 2013 06:45:32 -0300, Rural Hunter <ru...@gmail.com>  
wrote:

> cool, thanks! I am trying to implement a similar requirement as the OP  
> which is to send html formatted mails in a background service of my  
> webapp while there is no actual request/response involved. I'm trying to  
> figure out how to use Tapestry template as the mail template in some way.

Wouldn't a general-purpose templating entigne like FreeMarker be a better  
choice for this scenario (e-mails)? Is there anything from the webapp that  
you could reuse here?

-- 
Thiago H. de Paula Figueiredo

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: [t5] Is it possible to return a component's generated HTML ?

Posted by Rural Hunter <ru...@gmail.com>.
cool, thanks! I am trying to implement a similar requirement as the OP 
which is to send html formatted mails in a background service of my 
webapp while there is no actual request/response involved. I'm trying to 
figure out how to use Tapestry template as the mail template in some way.

于 2013/6/7 17:27, Lance Java 写道:
> AFAIK tapx-templating will not work with the normal tapestry web jars on
> the classpath and must be a separate app.
>
> I have written a class which can render components offline within a
> tapestry webapp for tapestry-cometd. The code can be found here
>
> https://github.com/uklance/tapestry-cometd/blob/master/src/main/java/org/lazan/t5/cometd/services/internal/ComponentJSONRendererImpl.java
>
>
> Note: It's a bit of a hack!
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: [t5] Is it possible to return a component's generated HTML ?

Posted by Lance Java <la...@googlemail.com>.
AFAIK tapx-templating will not work with the normal tapestry web jars on
the classpath and must be a separate app.

I have written a class which can render components offline within a
tapestry webapp for tapestry-cometd. The code can be found here

https://github.com/uklance/tapestry-cometd/blob/master/src/main/java/org/lazan/t5/cometd/services/internal/ComponentJSONRendererImpl.java


Note: It's a bit of a hack!

Re: [t5] Is it possible to return a component's generated HTML ?

Posted by Rural Hunter <ru...@gmail.com>.
dose tapx-templating work in a tapestry webapp now?

于 2013/6/6 15:31, Lance Java 写道:
> Have you considered tapx-templating and a single webapp?
>
> https://github.com/hlship/tapx
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: [t5] Is it possible to return a component's generated HTML ?

Posted by Muhammad Gelbana <m....@gmail.com>.
I strongly believe that Dmitry's suggestion is the simplest and most
efficient so far, thanks a lot for that :)

This would send an ajax request acceptable by tapestry:
ajax = new XMLHttpRequest();
ajax.open("POST", "http://localhost:8080/index:myevent/0/0/0", true);
ajax.setRequestHeader("X-Requested-With", "XMLHttpRequest"); //This is
necessary
ajax.send(null);

Thank you all :)


On Thu, Jun 6, 2013 at 9:31 AM, Lance Java <la...@googlemail.com>wrote:

> Have you considered tapx-templating and a single webapp?
>
> https://github.com/hlship/tapx
>

Re: [t5] Is it possible to return a component's generated HTML ?

Posted by Lance Java <la...@googlemail.com>.
Have you considered tapx-templating and a single webapp?

https://github.com/hlship/tapx

Re: [t5] Is it possible to return a component's generated HTML ?

Posted by Muhammad Gelbana <m....@gmail.com>.
Actually Lance's second approach is what I need to do. And yes it should be
very simple, I will only have to display regular html without controls at
all (Hopefully it will stay this way and I actually think it will)

Regards


On Wed, Jun 5, 2013 at 8:00 PM, Dmitry Gusev <dm...@gmail.com> wrote:

> The entry point is:
>
> String rawAddress = renderer.render(new RenderEvent(
>                 "internal/companyblocks", "companyAddress", company));
>
>
> You tell here that onCompanyAddress method (or method with
> @Event("companyAddress")) should be invoked on the internal/companyblocks
> page
> class and if that method returns a Block - it will be rendered to a string.
> company - is a parameter to that method that you may use to initialize
> rendering context for the block.
> You can also not return Block from the method by just addRenderer:
>
> ajaxResponseRenderer.addRender(addressBlockZone);
>
> And the addressBlockZone will be rendered to as string in this case.
>
> Resulting string will be serialized json object, so you can inspect its
> structure and take the content you want like:
>
>         String htmlAddress = new
> JSONObject(rawAddress).getString("content");
>
>
> On Wed, Jun 5, 2013 at 8:14 PM, Muhammad Gelbana <m.gelbana@gmail.com
> >wrote:
>
> > Honestly, I can't figure out anything from these 2 resources ! It all
> looks
> > so complicated to me.
> >
> > @Dmitry
> > What is the entry point of your code ? When an ajaxrequest is sent to my
> > application, doesn't it reach your "CompanyBlocks" page ? Then when does
> > all the other services begin working ?!
> >
> > @Lanve
> > And I thought Dimitry's code was complicated :D
> > Your code was even harder to look at, I also can't understand the entry
> > point and the flow of data starting when I receive an ajax request till I
> > respond with a string representation of a component's template file.
> >
> > Thank you all for your help :)
> >
> >
> > On Thu, May 30, 2013 at 6:41 PM, Lance Java <lance.java@googlemail.com
> > >wrote:
> >
> > > I'm not 100% sure what your implementation will be but I've done some
> > work
> > > in this area.
> > >
> > > Take a look at PDFLink.getFoAsString() where I convert a RenderCommand
> > to a
> > > String of markup (XML in the example)
> > >
> > > http://tapestry-stitch.uklance.cloudbees.net/pdflinkdemo
> > >
> > > Also, take a look at this wiki entry
> > >
> > >
> > >
> >
> http://wiki.apache.org/tapestry/Tapestry5HowToGetAnHTMLStringFromARenderCommandParameter
> > >
> > > Note that a Block can be type coerced to a RenderCommand
> > >
> >
>
>
>
> --
> Dmitry Gusev
>
> AnjLab Team
> http://anjlab.com
>

Re: [t5] Is it possible to return a component's generated HTML ?

Posted by Dmitry Gusev <dm...@gmail.com>.
The entry point is:

String rawAddress = renderer.render(new RenderEvent(
                "internal/companyblocks", "companyAddress", company));


You tell here that onCompanyAddress method (or method with
@Event("companyAddress")) should be invoked on the internal/companyblocks page
class and if that method returns a Block - it will be rendered to a string.
company - is a parameter to that method that you may use to initialize
rendering context for the block.
You can also not return Block from the method by just addRenderer:

ajaxResponseRenderer.addRender(addressBlockZone);

And the addressBlockZone will be rendered to as string in this case.

Resulting string will be serialized json object, so you can inspect its
structure and take the content you want like:

        String htmlAddress = new JSONObject(rawAddress).getString("content");


On Wed, Jun 5, 2013 at 8:14 PM, Muhammad Gelbana <m....@gmail.com>wrote:

> Honestly, I can't figure out anything from these 2 resources ! It all looks
> so complicated to me.
>
> @Dmitry
> What is the entry point of your code ? When an ajaxrequest is sent to my
> application, doesn't it reach your "CompanyBlocks" page ? Then when does
> all the other services begin working ?!
>
> @Lanve
> And I thought Dimitry's code was complicated :D
> Your code was even harder to look at, I also can't understand the entry
> point and the flow of data starting when I receive an ajax request till I
> respond with a string representation of a component's template file.
>
> Thank you all for your help :)
>
>
> On Thu, May 30, 2013 at 6:41 PM, Lance Java <lance.java@googlemail.com
> >wrote:
>
> > I'm not 100% sure what your implementation will be but I've done some
> work
> > in this area.
> >
> > Take a look at PDFLink.getFoAsString() where I convert a RenderCommand
> to a
> > String of markup (XML in the example)
> >
> > http://tapestry-stitch.uklance.cloudbees.net/pdflinkdemo
> >
> > Also, take a look at this wiki entry
> >
> >
> >
> http://wiki.apache.org/tapestry/Tapestry5HowToGetAnHTMLStringFromARenderCommandParameter
> >
> > Note that a Block can be type coerced to a RenderCommand
> >
>



-- 
Dmitry Gusev

AnjLab Team
http://anjlab.com

Re: [t5] Is it possible to return a component's generated HTML ?

Posted by Lance Java <la...@googlemail.com>.
I think I need to understand how it's going to work before I go any further.



Here's one approach

1. The browser makes a request to the "other" webapp

2. The other webapp makes request(s) to the tapestry app

3. The other webapp merges it's own html with the tapestry html to create a
page

4. The page is returned to the user



Here's another approach

1. The browser makes a request to the "other" webapp

2. The other webapp returns a page with some empty divs

3. Some javascript runs on page load

4. The browser makes requests to the tapestry app

5. The tapestry app returns responses to the browser

6. Some javascript in the browser updates the innerHTML of some div's



In both approaches, you are going to be fighting an uphill battle if you
want to handle javascript and eventlink / actionlinks in your tapestry
components. If you simply want to render html fragments, without links or
javascript we might be able to help.

Re: [t5] Is it possible to return a component's generated HTML ?

Posted by Muhammad Gelbana <m....@gmail.com>.
Honestly, I can't figure out anything from these 2 resources ! It all looks
so complicated to me.

@Dmitry
What is the entry point of your code ? When an ajaxrequest is sent to my
application, doesn't it reach your "CompanyBlocks" page ? Then when does
all the other services begin working ?!

@Lanve
And I thought Dimitry's code was complicated :D
Your code was even harder to look at, I also can't understand the entry
point and the flow of data starting when I receive an ajax request till I
respond with a string representation of a component's template file.

Thank you all for your help :)


On Thu, May 30, 2013 at 6:41 PM, Lance Java <la...@googlemail.com>wrote:

> I'm not 100% sure what your implementation will be but I've done some work
> in this area.
>
> Take a look at PDFLink.getFoAsString() where I convert a RenderCommand to a
> String of markup (XML in the example)
>
> http://tapestry-stitch.uklance.cloudbees.net/pdflinkdemo
>
> Also, take a look at this wiki entry
>
>
> http://wiki.apache.org/tapestry/Tapestry5HowToGetAnHTMLStringFromARenderCommandParameter
>
> Note that a Block can be type coerced to a RenderCommand
>

Re: [t5] Is it possible to return a component's generated HTML ?

Posted by Lance Java <la...@googlemail.com>.
I'm not 100% sure what your implementation will be but I've done some work
in this area.

Take a look at PDFLink.getFoAsString() where I convert a RenderCommand to a
String of markup (XML in the example)

http://tapestry-stitch.uklance.cloudbees.net/pdflinkdemo

Also, take a look at this wiki entry

http://wiki.apache.org/tapestry/Tapestry5HowToGetAnHTMLStringFromARenderCommandParameter

Note that a Block can be type coerced to a RenderCommand