You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Arthur Clement <ac...@gmail.com> on 2011/03/22 23:11:36 UTC

Calling a server side service in ajax

Hi,

What would be the best approach to have a service render html as a response
to an ajax request?
Basically I'm using a markup editor called markitup that provides an preview
feature. The preview feature needs an url to send the content that has been
inserted (in the markdown syntax) so that it gets processed server side,
sending back an html response that can be used as a preview.
I have seen that tapestry has the StreamReponse class but I cant figure out
what url I should provide to make the call...
Should I implement a component for this? I dont want to duplicate code in
all the pages I would be using this, it has to be reusable.
I could do a simple servlet I guess or even use restful webservices as
described here http://stackoverflow.com/questions/2765178/tapestry-restwhich
seems a bit overkill but I was hoping there was a tapestry way of
doing this. If it can be useful I'm using the 5.2.4 version.
Thanks,

Arthur

Re: Calling a server side service in ajax

Posted by Arthur Clement <ac...@gmail.com>.
Thanks for your response.
It oriented me in the right direction for further research but in the
end I found a much simpler solution (actually I found it somewhere
while googling around and I think it is you that was talking about it
already) :
I just created a page that transforms the markdown to html in its
onActivateMethod as follows :

public class MarkdownToHtml {
	@Inject
	private MarkdownService markdownService;
	
	public StreamResponse onActivate(@RequestParameter("data") String markdown){
		return new TextStreamResponse("text/html", markdownService.getHtml(markdown));
	}	
}

I then placed an actionLink to this page on the Index page just to see
what the generated url was and use that url in my javascript library.
I can call this url from anywhere in my application without having to
duplicate nothing which is what I wanted. It is working fine.
In final my url is very simple and looks like this :
http://localhost:8080/appname/markdowntohtml. Of course I only use a
relative url in the js so that it will work not only during
development.

Thanks,

Arthur

On 23 March 2011 09:42, Thiago H. de Paula Figueiredo
<th...@gmail.com> wrote:
> On Tue, 22 Mar 2011 19:11:36 -0300, Arthur Clement <ac...@gmail.com>
> wrote:
>
>> Hi,
>
> Hi!
>
>> What would be the best approach to have a service render html as a
>> response to an ajax request?
>
>> I have seen that tapestry has the StreamReponse class
>
> You're in the right path. ;)
>
>> but I cant figure out what url I should provide to make the call...
>
> Define an event name, @Inject ComponentResources and use its
> createEventLink() method. It returns a Link. To get an absolute URL out of
> it, invoke its toAbsoluteURI() method. Now just pass this URL to your AJAX
> code, most probably using JavascriptSupport.addScript().
>
>> Should I implement a component for this? I dont want to duplicate code in
>> all the pages I would be using this, it has to be reusable.
>
> That's a very good idea. And, if you can, please share the code with us. :D
> The method I described above is absolutely the same regardless if it's
> inside a page, component or mixin.
>
> --
> Thiago H. de Paula Figueiredo
> Independent Java, Apache Tapestry 5 and Hibernate consultant, developer, and
> instructor
> Owner, Ars Machina Tecnologia da Informação Ltda.
> http://www.arsmachina.com.br
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

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


Re: Calling a server side service in ajax

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Tue, 22 Mar 2011 19:11:36 -0300, Arthur Clement <ac...@gmail.com>  
wrote:

> Hi,

Hi!

> What would be the best approach to have a service render html as a  
> response to an ajax request?

> I have seen that tapestry has the StreamReponse class

You're in the right path. ;)

> but I cant figure out what url I should provide to make the call...

Define an event name, @Inject ComponentResources and use its  
createEventLink() method. It returns a Link. To get an absolute URL out of  
it, invoke its toAbsoluteURI() method. Now just pass this URL to your AJAX  
code, most probably using JavascriptSupport.addScript().

> Should I implement a component for this? I dont want to duplicate code in
> all the pages I would be using this, it has to be reusable.

That's a very good idea. And, if you can, please share the code with us. :D
The method I described above is absolutely the same regardless if it's  
inside a page, component or mixin.

-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

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