You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Serban Balamaci <se...@asf.ro> on 2009/07/31 15:34:26 UTC

Wicket, Ajax and JSON

Hello. I'm trying to create a wicket component out of FlexiGrid
http://www.flexigrid.info/ .

The javascript for the component requires an address from which it will
receive data in the form of a JSON response.

 

I'm not sure what is the best way to go about it. Simple and most ugly
approach would be to have something of a "Controller Page"

a page that takes parameters such as the entity that is being retrieved and
also paging info, sort order, etc and returns the JSON response.

I do not like this aproach as, most obvious reason is that it would not be
obvious from the component what you need to do in order to get it to work.

 

I would much more preffer to having a callback method that returns the json.
So I have created a

 

AbstractDefaultAjaxBehavior clientConnectBehavior = new
AbstractDefaultAjaxBehavior() {

 

            protected void respond(AjaxRequestTarget ajaxRequestTarget) {

                   ajaxRequestTarget.prependJavascript("{page: 1, total: 239
}");

 

            }

and used clientConnectBehavior.getCallbackUrl() as the url passed to the
flexigrid.

 

Unfortunately the problem is the response when invoked is wrapped in a
<ajax-response><evaluate encoding="wicket1">

<?xml version="1.0" encoding="UTF-8"?>

<ajax-response><evaluate encoding="wicket1"><![CDATA[{page: 1,total:
239}]]></evaluate></ajax-response>

 

So my question is how to get only the JSON part without the <?xml version.>,
<ajax-response> . and <CDATA> decoration. Can it be bypassed? Shoul I
override something in AbstractDefaultAjaxBehavior? 

Or any other ideea to go about the problem.

 

Thank you,

 

Serban


Re: Wicket, Ajax and JSON

Posted by John Armstrong <si...@siberian.org>.
Maybe someone can document a workable solution in the Wiki for future
generations?
Tx
J

On Fri, Jul 31, 2009 at 10:22 AM, francisco treacy <
francisco.treacy@gmail.com> wrote:

> That's cool.
>
> I'm still surprised to see that whenever something remotely
> ressembling stateless/REST comes up in the mailing list, the gut
> reaction is: servlets. It might be simpler for a few usecases, but
> normally you also want to take advantage of other Wicket features -
> precisely the reason why Serban insists with the Ajax behaviour.
>
> If it is feasible already, what I really would like is to have better
> support for doing REST. Of course you don't want to "drive screw into
> a board with a hammer" but for many cases simple stateless suffices,
> where Jersey would be an overkill and servlets out of the picture.
> Those usecases include sites where you want to expose some
> functionality as stateless (including Ajax) and the rest are heavily
> interactive pages (like an admin or wizards) where Wicket excels.
>
> I just recall the interest there was when the creator of
> http://www.fitcomplex.sk/ mentioned what he was doing with jQuery.
>
> Just a general feeling I'm expressing here.
>
> Thanks,
>
> Francisco
>
>
>
> 2009/7/31 richardwilko <ri...@gmail.com>:
> >
> > Hi,
> >
> > Instead of using AbstractDefaultAjaxBehavior you can use its superclass
> > AbstractAjaxBehavior as follows:
> >
> >        AbstractAjaxBehavior behaviour = new AbstractAjaxBehavior()
> >        {
> >            private static final long serialVersionUID = 1L;
> >
> >            @SuppressWarnings("unchecked")
> >            public void onRequest()
> >            {
> >                //get parameters
> >                final RequestCycle requestCycle = RequestCycle.get();
> >
> >                final PageParameters pageParameters = new
> > PageParameters(requestCycle.getRequest().getParameterMap());
> >
> >                //do something using nice json library to produce a string
> > of json
> >                String json = .......
> >
> >
> >                requestCycle.setRequestTarget(new
> > StringRequestTarget("application/json", "utf-8", data));
> >            }
> >
> >        };
> >        add(behaviour);
> >
> > I can't see anything wrong with the servlet example, but this has the
> > advantage of being in a Wicket component, so you can easily access your
> > Wicket session, and the user wont have to remember to map extra servlets.
> >
> > I have also tried making a component out of flexigrid, but ran into
> issues
> > (which they may have fixed now), for example, I was getting rendering
> issues
> > in the grid when trying to resize the columns.
> >
> > I then moved onto another jQuery grid called jqGrid
> > (http://www.trirand.com/blog/) which I felt had more features and was
> more
> > robust.
> >
> > At jWeekend (http://www.jweekend.com) have almost completed the Wicket
> > mapping of this grid and we hope to release this in the future when we
> are
> > satisfied with our testing and API.  This will be released as a plugin to
> > the wiQuery project (http://code.google.com/p/wiquery/), which basically
> > means it will depend on wiQuery.
> >
> >
> > Regards - Richard Wilkinson
> > Developer
> > jWeekend: OO & Java Technologies - Development and Training
> > http://jWeekend.com
> >
> >
> >
> >
> >
> >
> > Serban Balamaci wrote:
> >>
> >> Hello. I'm trying to create a wicket component out of FlexiGrid
> >> http://www.flexigrid.info/ .
> >>
> >> The javascript for the component requires an address from which it will
> >> receive data in the form of a JSON response.
> >>
> >>
> >>
> >> I'm not sure what is the best way to go about it. Simple and most ugly
> >> approach would be to have something of a "Controller Page"
> >>
> >> a page that takes parameters such as the entity that is being retrieved
> >> and
> >> also paging info, sort order, etc and returns the JSON response.
> >>
> >> I do not like this aproach as, most obvious reason is that it would not
> be
> >> obvious from the component what you need to do in order to get it to
> work.
> >>
> >>
> >>
> >> I would much more preffer to having a callback method that returns the
> >> json.
> >> So I have created a
> >>
> >>
> >>
> >> AbstractDefaultAjaxBehavior clientConnectBehavior = new
> >> AbstractDefaultAjaxBehavior() {
> >>
> >>
> >>
> >>             protected void respond(AjaxRequestTarget ajaxRequestTarget)
> {
> >>
> >>                    ajaxRequestTarget.prependJavascript("{page: 1, total:
> >> 239
> >> }");
> >>
> >>
> >>
> >>             }
> >>
> >> and used clientConnectBehavior.getCallbackUrl() as the url passed to the
> >> flexigrid.
> >>
> >>
> >>
> >> Unfortunately the problem is the response when invoked is wrapped in a
> >> <ajax-response><evaluate encoding="wicket1">
> >>
> >> <?xml version="1.0" encoding="UTF-8"?>
> >>
> >> <ajax-response><evaluate encoding="wicket1"><![CDATA[{page: 1,total:
> >> 239}]]></evaluate></ajax-response>
> >>
> >>
> >>
> >> So my question is how to get only the JSON part without the <?xml
> >> version.>,
> >> <ajax-response> . and <CDATA> decoration. Can it be bypassed? Shoul I
> >> override something in AbstractDefaultAjaxBehavior?
> >>
> >> Or any other ideea to go about the problem.
> >>
> >>
> >>
> >> Thank you,
> >>
> >>
> >>
> >> Serban
> >>
> >>
> >>
> >
> >
> > -----
> > http://richard-wilkinson.co.uk My blog: http://richard-wilkinson.co.uk
> > --
> > View this message in context:
> http://www.nabble.com/Wicket%2C-Ajax-and-JSON-tp24756591p24757846.html
> > Sent from the Wicket - User mailing list archive at Nabble.com.
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> > For additional commands, e-mail: users-help@wicket.apache.org
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: Wicket, Ajax and JSON

Posted by se...@asf.ro.
Thanks guys, loved the AbstractAjaxBehavior solution, and by the way
jqgrid seems to be more active and the way to go(thanks for pointing it
Richard). Keeping fingers crossed for wiQuery and if they even bring the
flexi or jqgrid to wicket it's gonna prove you can have awesome visual
components in Wicket too.


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


Re: Wicket, Ajax and JSON

Posted by francisco treacy <fr...@gmail.com>.
That's cool.

I'm still surprised to see that whenever something remotely
ressembling stateless/REST comes up in the mailing list, the gut
reaction is: servlets. It might be simpler for a few usecases, but
normally you also want to take advantage of other Wicket features -
precisely the reason why Serban insists with the Ajax behaviour.

If it is feasible already, what I really would like is to have better
support for doing REST. Of course you don't want to "drive screw into
a board with a hammer" but for many cases simple stateless suffices,
where Jersey would be an overkill and servlets out of the picture.
Those usecases include sites where you want to expose some
functionality as stateless (including Ajax) and the rest are heavily
interactive pages (like an admin or wizards) where Wicket excels.

I just recall the interest there was when the creator of
http://www.fitcomplex.sk/ mentioned what he was doing with jQuery.

Just a general feeling I'm expressing here.

Thanks,

Francisco



2009/7/31 richardwilko <ri...@gmail.com>:
>
> Hi,
>
> Instead of using AbstractDefaultAjaxBehavior you can use its superclass
> AbstractAjaxBehavior as follows:
>
>        AbstractAjaxBehavior behaviour = new AbstractAjaxBehavior()
>        {
>            private static final long serialVersionUID = 1L;
>
>            @SuppressWarnings("unchecked")
>            public void onRequest()
>            {
>                //get parameters
>                final RequestCycle requestCycle = RequestCycle.get();
>
>                final PageParameters pageParameters = new
> PageParameters(requestCycle.getRequest().getParameterMap());
>
>                //do something using nice json library to produce a string
> of json
>                String json = .......
>
>
>                requestCycle.setRequestTarget(new
> StringRequestTarget("application/json", "utf-8", data));
>            }
>
>        };
>        add(behaviour);
>
> I can't see anything wrong with the servlet example, but this has the
> advantage of being in a Wicket component, so you can easily access your
> Wicket session, and the user wont have to remember to map extra servlets.
>
> I have also tried making a component out of flexigrid, but ran into issues
> (which they may have fixed now), for example, I was getting rendering issues
> in the grid when trying to resize the columns.
>
> I then moved onto another jQuery grid called jqGrid
> (http://www.trirand.com/blog/) which I felt had more features and was more
> robust.
>
> At jWeekend (http://www.jweekend.com) have almost completed the Wicket
> mapping of this grid and we hope to release this in the future when we are
> satisfied with our testing and API.  This will be released as a plugin to
> the wiQuery project (http://code.google.com/p/wiquery/), which basically
> means it will depend on wiQuery.
>
>
> Regards - Richard Wilkinson
> Developer
> jWeekend: OO & Java Technologies - Development and Training
> http://jWeekend.com
>
>
>
>
>
>
> Serban Balamaci wrote:
>>
>> Hello. I'm trying to create a wicket component out of FlexiGrid
>> http://www.flexigrid.info/ .
>>
>> The javascript for the component requires an address from which it will
>> receive data in the form of a JSON response.
>>
>>
>>
>> I'm not sure what is the best way to go about it. Simple and most ugly
>> approach would be to have something of a "Controller Page"
>>
>> a page that takes parameters such as the entity that is being retrieved
>> and
>> also paging info, sort order, etc and returns the JSON response.
>>
>> I do not like this aproach as, most obvious reason is that it would not be
>> obvious from the component what you need to do in order to get it to work.
>>
>>
>>
>> I would much more preffer to having a callback method that returns the
>> json.
>> So I have created a
>>
>>
>>
>> AbstractDefaultAjaxBehavior clientConnectBehavior = new
>> AbstractDefaultAjaxBehavior() {
>>
>>
>>
>>             protected void respond(AjaxRequestTarget ajaxRequestTarget) {
>>
>>                    ajaxRequestTarget.prependJavascript("{page: 1, total:
>> 239
>> }");
>>
>>
>>
>>             }
>>
>> and used clientConnectBehavior.getCallbackUrl() as the url passed to the
>> flexigrid.
>>
>>
>>
>> Unfortunately the problem is the response when invoked is wrapped in a
>> <ajax-response><evaluate encoding="wicket1">
>>
>> <?xml version="1.0" encoding="UTF-8"?>
>>
>> <ajax-response><evaluate encoding="wicket1"><![CDATA[{page: 1,total:
>> 239}]]></evaluate></ajax-response>
>>
>>
>>
>> So my question is how to get only the JSON part without the <?xml
>> version.>,
>> <ajax-response> . and <CDATA> decoration. Can it be bypassed? Shoul I
>> override something in AbstractDefaultAjaxBehavior?
>>
>> Or any other ideea to go about the problem.
>>
>>
>>
>> Thank you,
>>
>>
>>
>> Serban
>>
>>
>>
>
>
> -----
> http://richard-wilkinson.co.uk My blog: http://richard-wilkinson.co.uk
> --
> View this message in context: http://www.nabble.com/Wicket%2C-Ajax-and-JSON-tp24756591p24757846.html
> Sent from the Wicket - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

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


Re: Wicket, Ajax and JSON

Posted by richardwilko <ri...@gmail.com>.
Hi,

Instead of using AbstractDefaultAjaxBehavior you can use its superclass
AbstractAjaxBehavior as follows:

        AbstractAjaxBehavior behaviour = new AbstractAjaxBehavior()
        {
            private static final long serialVersionUID = 1L;

            @SuppressWarnings("unchecked")
            public void onRequest()
            {
                //get parameters
                final RequestCycle requestCycle = RequestCycle.get();

                final PageParameters pageParameters = new
PageParameters(requestCycle.getRequest().getParameterMap());
               
                //do something using nice json library to produce a string
of json
                String json = .......
               
               
                requestCycle.setRequestTarget(new
StringRequestTarget("application/json", "utf-8", data));
            }

        };
        add(behaviour);

I can't see anything wrong with the servlet example, but this has the
advantage of being in a Wicket component, so you can easily access your
Wicket session, and the user wont have to remember to map extra servlets.

I have also tried making a component out of flexigrid, but ran into issues
(which they may have fixed now), for example, I was getting rendering issues
in the grid when trying to resize the columns.

I then moved onto another jQuery grid called jqGrid
(http://www.trirand.com/blog/) which I felt had more features and was more
robust.

At jWeekend (http://www.jweekend.com) have almost completed the Wicket
mapping of this grid and we hope to release this in the future when we are
satisfied with our testing and API.  This will be released as a plugin to
the wiQuery project (http://code.google.com/p/wiquery/), which basically
means it will depend on wiQuery.


Regards - Richard Wilkinson
Developer
jWeekend: OO & Java Technologies - Development and Training
http://jWeekend.com






Serban Balamaci wrote:
> 
> Hello. I'm trying to create a wicket component out of FlexiGrid
> http://www.flexigrid.info/ .
> 
> The javascript for the component requires an address from which it will
> receive data in the form of a JSON response.
> 
>  
> 
> I'm not sure what is the best way to go about it. Simple and most ugly
> approach would be to have something of a "Controller Page"
> 
> a page that takes parameters such as the entity that is being retrieved
> and
> also paging info, sort order, etc and returns the JSON response.
> 
> I do not like this aproach as, most obvious reason is that it would not be
> obvious from the component what you need to do in order to get it to work.
> 
>  
> 
> I would much more preffer to having a callback method that returns the
> json.
> So I have created a
> 
>  
> 
> AbstractDefaultAjaxBehavior clientConnectBehavior = new
> AbstractDefaultAjaxBehavior() {
> 
>  
> 
>             protected void respond(AjaxRequestTarget ajaxRequestTarget) {
> 
>                    ajaxRequestTarget.prependJavascript("{page: 1, total:
> 239
> }");
> 
>  
> 
>             }
> 
> and used clientConnectBehavior.getCallbackUrl() as the url passed to the
> flexigrid.
> 
>  
> 
> Unfortunately the problem is the response when invoked is wrapped in a
> <ajax-response><evaluate encoding="wicket1">
> 
> <?xml version="1.0" encoding="UTF-8"?>
> 
> <ajax-response><evaluate encoding="wicket1"><![CDATA[{page: 1,total:
> 239}]]></evaluate></ajax-response>
> 
>  
> 
> So my question is how to get only the JSON part without the <?xml
> version.>,
> <ajax-response> . and <CDATA> decoration. Can it be bypassed? Shoul I
> override something in AbstractDefaultAjaxBehavior? 
> 
> Or any other ideea to go about the problem.
> 
>  
> 
> Thank you,
> 
>  
> 
> Serban
> 
> 
> 


-----
http://richard-wilkinson.co.uk My blog: http://richard-wilkinson.co.uk 
-- 
View this message in context: http://www.nabble.com/Wicket%2C-Ajax-and-JSON-tp24756591p24757846.html
Sent from the Wicket - User mailing list archive at Nabble.com.


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


RE: Wicket, Ajax and JSON

Posted by Serban Balamaci <se...@asf.ro>.
Hello John.

This I would put in the same category as the "Controller Page" solution. And
well, that kind of separation will not make a nice reusable component. I
want in the end to get maybe even with something of a model behind it and
not have such a separation, although I guess the model of the component
would know to pass the parameters to the JSON servlet. And to get over the
problem of authentication and injection of other beans that can even be
coded as a Wicket Page.

Curently I'm looking in the direction of not extending
AbstractDefaultAjaxBehavior but looking more at the base classes in
AbstractDefaultAjaxBehavior and implement something like

public final void respond(final RequestCycle requestCycle)
	{
		final Application app = Application.get();

		// Determine encoding
		final String encoding =
app.getRequestCycleSettings().getResponseRequestEncoding();

		// Set content type based on markup type for page
		final WebResponse response =
(WebResponse)requestCycle.getResponse();
		response.setCharacterEncoding(encoding);
		response.setContentType("text/xml; charset=" + encoding);

		response.write(json data);
 }



-----Original Message-----
From: John Armstrong [mailto:siberian@siberian.org] 
Sent: 31 iulie 2009 17:33
To: users@wicket.apache.org
Subject: Re: Wicket, Ajax and JSON

Maybe you are trying to drive screw into a board with a hammer?
I recommend just creating servlet to serve up these json style responses and
then use the excellent json.org libraries(instead of hand constructing json
transform your data structures into json automatically). The only issue
you'll have here is authentication so if you need a unified auth system you
will need to get that up and running in your json responder (wicket would do
it automatically).

 <filter-mapping>

    <filter-name>WebApplication</filter-name>

    <url-pattern>/*</url-pattern>

  </filter-mapping>



 <servlet>

    <description>This is the JSON Gatewayn</description>

    <display-name>JSON Gateway</display-name>

    <servlet-name>JsonGateway</servlet-name>

    <servlet-class>your.package.json.JsonGateway</servlet-class>

  </servlet>

<servlet-mapping>

    <servlet-name>getSomeData</servlet-name>

    <url-pattern>/json/getSomeData</url-pattern>

  </servlet-mapping>


John-

On Fri, Jul 31, 2009 at 6:34 AM, Serban Balamaci
<se...@asf.ro>wrote:

> Hello. I'm trying to create a wicket component out of FlexiGrid
> http://www.flexigrid.info/ .
>
> The javascript for the component requires an address from which it will
> receive data in the form of a JSON response.
>
>
>
> I'm not sure what is the best way to go about it. Simple and most ugly
> approach would be to have something of a "Controller Page"
>
> a page that takes parameters such as the entity that is being retrieved
and
> also paging info, sort order, etc and returns the JSON response.
>
> I do not like this aproach as, most obvious reason is that it would not be
> obvious from the component what you need to do in order to get it to work.
>
>
>
> I would much more preffer to having a callback method that returns the
> json.
> So I have created a
>
>
>
> AbstractDefaultAjaxBehavior clientConnectBehavior = new
> AbstractDefaultAjaxBehavior() {
>
>
>
>            protected void respond(AjaxRequestTarget ajaxRequestTarget) {
>
>                   ajaxRequestTarget.prependJavascript("{page: 1, total:
239
> }");
>
>
>
>            }
>
> and used clientConnectBehavior.getCallbackUrl() as the url passed to the
> flexigrid.
>
>
>
> Unfortunately the problem is the response when invoked is wrapped in a
> <ajax-response><evaluate encoding="wicket1">
>
> <?xml version="1.0" encoding="UTF-8"?>
>
> <ajax-response><evaluate encoding="wicket1"><![CDATA[{page: 1,total:
> 239}]]></evaluate></ajax-response>
>
>
>
> So my question is how to get only the JSON part without the <?xml
> version.>,
> <ajax-response> . and <CDATA> decoration. Can it be bypassed? Shoul I
> override something in AbstractDefaultAjaxBehavior?
>
> Or any other ideea to go about the problem.
>
>
>
> Thank you,
>
>
>
> Serban
>
>


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


Re: Wicket, Ajax and JSON

Posted by John Armstrong <si...@siberian.org>.
Maybe you are trying to drive screw into a board with a hammer?
I recommend just creating servlet to serve up these json style responses and
then use the excellent json.org libraries(instead of hand constructing json
transform your data structures into json automatically). The only issue
you'll have here is authentication so if you need a unified auth system you
will need to get that up and running in your json responder (wicket would do
it automatically).

 <filter-mapping>

    <filter-name>WebApplication</filter-name>

    <url-pattern>/*</url-pattern>

  </filter-mapping>



 <servlet>

    <description>This is the JSON Gatewayn</description>

    <display-name>JSON Gateway</display-name>

    <servlet-name>JsonGateway</servlet-name>

    <servlet-class>your.package.json.JsonGateway</servlet-class>

  </servlet>

<servlet-mapping>

    <servlet-name>getSomeData</servlet-name>

    <url-pattern>/json/getSomeData</url-pattern>

  </servlet-mapping>


John-

On Fri, Jul 31, 2009 at 6:34 AM, Serban Balamaci <se...@asf.ro>wrote:

> Hello. I'm trying to create a wicket component out of FlexiGrid
> http://www.flexigrid.info/ .
>
> The javascript for the component requires an address from which it will
> receive data in the form of a JSON response.
>
>
>
> I'm not sure what is the best way to go about it. Simple and most ugly
> approach would be to have something of a "Controller Page"
>
> a page that takes parameters such as the entity that is being retrieved and
> also paging info, sort order, etc and returns the JSON response.
>
> I do not like this aproach as, most obvious reason is that it would not be
> obvious from the component what you need to do in order to get it to work.
>
>
>
> I would much more preffer to having a callback method that returns the
> json.
> So I have created a
>
>
>
> AbstractDefaultAjaxBehavior clientConnectBehavior = new
> AbstractDefaultAjaxBehavior() {
>
>
>
>            protected void respond(AjaxRequestTarget ajaxRequestTarget) {
>
>                   ajaxRequestTarget.prependJavascript("{page: 1, total: 239
> }");
>
>
>
>            }
>
> and used clientConnectBehavior.getCallbackUrl() as the url passed to the
> flexigrid.
>
>
>
> Unfortunately the problem is the response when invoked is wrapped in a
> <ajax-response><evaluate encoding="wicket1">
>
> <?xml version="1.0" encoding="UTF-8"?>
>
> <ajax-response><evaluate encoding="wicket1"><![CDATA[{page: 1,total:
> 239}]]></evaluate></ajax-response>
>
>
>
> So my question is how to get only the JSON part without the <?xml
> version.>,
> <ajax-response> . and <CDATA> decoration. Can it be bypassed? Shoul I
> override something in AbstractDefaultAjaxBehavior?
>
> Or any other ideea to go about the problem.
>
>
>
> Thank you,
>
>
>
> Serban
>
>