You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Tony Nelson <tn...@starpoint.com> on 2013/04/02 16:03:04 UTC

Ajax Parameter Handling

Given a simple contrived example like this:

            $('#button1').click(function (event) {
                $.get(specs.button1_url, {  a: 'b', c: 'd'})
                    .done(function(data) {
                        $("#responseDiv").text(data.response);
                    })
            })

I know I can handle that in my controller in at least these two ways:

    JSONObject onButton1Press() {
        logger.info("a: " + request.getParameter("a") + " c: " + request.getParameter("c"));
        logger.info(request.getParameterNames().toString());
        return new JSONObject("response", "ok1");
    }

    JSONObject onButton1Press(@RequestParameter("a") String a, @RequestParameter("c") String c) {
        logger.info("a: " + a + " c: " + c);
        return new JSONObject("response", "ok2");
    }

Ideally, I'd like to handle it like this:

    JSONObject onButton1Press(SimplePojo pojo) {
        logger.info(pojo.toString());
        return new JSONObject("response", "ok3");
    }

I thought maybe I could contribute a ValueEncoder but that only sees individual parameters, not the whole request.

Even this might work:

    JSONObject onButton1Press(JSONObject jsonObject) {
        SimplePojo pojo = SimplePojo.build(jsonObject);
        logger.info(pojo.toString());
        return new JSONObject("response", "ok4");
    }


But I Tapestry doesn't seem to know how to handle that. I even tried changed the ajax params to: {params:  {  a: 'b', c: 'd'} }

I supposed I could build the SimplePojo from the Request, but that just doesn't feel as clean to me.

Any suggestions would be appreciated.

Thanks
Tony Nelson



Since 1982, Starpoint Solutions has been a trusted source of human capital and solutions. We are committed to our clients, employees, environment, community and social concerns.  We foster an inclusive culture based on trust, respect, honesty and solid performance. Learn more about Starpoint and our social responsibility at http://www.starpoint.com/social_responsibility

This email message from Starpoint Solutions LLC is for the sole use of  the intended recipient(s) and may contain confidential and privileged  information.  Any unauthorized review, use, disclosure or distribution is prohibited.  If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message.  Opinions, conclusions and other information in this message that do not relate to the official business of Starpoint Solutions shall be understood as neither given nor endorsed by it.

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


Re: Ajax Parameter Handling

Posted by Paul Stanton <pa...@mapshed.com.au>.
FYI

https://issues.apache.org/jira/browse/TAP5-2021

On 3/04/2013 1:32 AM, Tony Nelson wrote:
>>> I thought maybe I could contribute a ValueEncoder but that only sees
>>> individual parameters, not the whole request.
>>>
>>> Even this might work:
>>>
>>>     JSONObject onButton1Press(JSONObject jsonObject) {
>>>         SimplePojo pojo = SimplePojo.build(jsonObject);
>>>         logger.info(pojo.toString());
>>>         return new JSONObject("response", "ok4");
>>>     }
>> Why don't you use a query parameter to pass a String containing a
>> JSON-encoded object and then use the JSONObject(String string) to parse it?
>
> This worked like a charm!
>
>              $('#button1').click(function (event) {
>                  $.get(specs.button1_url, {params: JSON.stringify({  a: 'b', c: 'd'}) })
>                      .done(function(data) {
>                          $("#responseDiv").text(data.response);
>                      })
>              })
>
>
>      JSONObject onButton1Press(@RequestParameter("params") String jsonString) {
>          SimplePojo pojo = SimplePojo.build(new JSONObject(jsonString));
>          logger.info(pojo.toString());
>          return new JSONObject("response", "ok5");
>      }
>
>
> Thank you very much Theo!  If you see any further enhancements you might make to that, I'd be very interested in hearing them.
>
> Tony
>
>
> Since 1982, Starpoint Solutions has been a trusted source of human capital and solutions. We are committed to our clients, employees, environment, community and social concerns.  We foster an inclusive culture based on trust, respect, honesty and solid performance. Learn more about Starpoint and our social responsibility at http://www.starpoint.com/social_responsibility
>
> This email message from Starpoint Solutions LLC is for the sole use of  the intended recipient(s) and may contain confidential and privileged  information.  Any unauthorized review, use, disclosure or distribution is prohibited.  If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message.  Opinions, conclusions and other information in this message that do not relate to the official business of Starpoint Solutions shall be understood as neither given nor endorsed by it.
>
> ---------------------------------------------------------------------
> 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: Ajax Parameter Handling

Posted by Tony Nelson <tn...@starpoint.com>.
>
>> I thought maybe I could contribute a ValueEncoder but that only sees
>> individual parameters, not the whole request.
>>
>> Even this might work:
>>
>>    JSONObject onButton1Press(JSONObject jsonObject) {
>>        SimplePojo pojo = SimplePojo.build(jsonObject);
>>        logger.info(pojo.toString());
>>        return new JSONObject("response", "ok4");
>>    }
>
> Why don't you use a query parameter to pass a String containing a
> JSON-encoded object and then use the JSONObject(String string) to parse it?


This worked like a charm!

            $('#button1').click(function (event) {
                $.get(specs.button1_url, {params: JSON.stringify({  a: 'b', c: 'd'}) })
                    .done(function(data) {
                        $("#responseDiv").text(data.response);
                    })
            })


    JSONObject onButton1Press(@RequestParameter("params") String jsonString) {
        SimplePojo pojo = SimplePojo.build(new JSONObject(jsonString));
        logger.info(pojo.toString());
        return new JSONObject("response", "ok5");
    }


Thank you very much Theo!  If you see any further enhancements you might make to that, I'd be very interested in hearing them.

Tony


Since 1982, Starpoint Solutions has been a trusted source of human capital and solutions. We are committed to our clients, employees, environment, community and social concerns.  We foster an inclusive culture based on trust, respect, honesty and solid performance. Learn more about Starpoint and our social responsibility at http://www.starpoint.com/social_responsibility

This email message from Starpoint Solutions LLC is for the sole use of  the intended recipient(s) and may contain confidential and privileged  information.  Any unauthorized review, use, disclosure or distribution is prohibited.  If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message.  Opinions, conclusions and other information in this message that do not relate to the official business of Starpoint Solutions shall be understood as neither given nor endorsed by it.

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


Re: Ajax Parameter Handling

Posted by Thiago H de Paula Figueiredo <th...@gmail.com>.
On Tue, 02 Apr 2013 11:03:04 -0300, Tony Nelson <tn...@starpoint.com>  
wrote:

>     JSONObject onButton1Press(SimplePojo pojo) {
>         logger.info(pojo.toString());
>         return new JSONObject("response", "ok3");
>     }

For this kind of parameter in an event handler method, Tapestry expects it  
to be in the event's activation context. I don't know of a JS  
implementation of Tapestry's URLEncoder, so I prefer to use query  
parameters in this case. In addition, ValueEncoder is usually (but not  
obligatorily) used for mapping objects to its ids, not to a String  
describing each of field values.

> I thought maybe I could contribute a ValueEncoder but that only sees  
> individual parameters, not the whole request.
>
> Even this might work:
>
>     JSONObject onButton1Press(JSONObject jsonObject) {
>         SimplePojo pojo = SimplePojo.build(jsonObject);
>         logger.info(pojo.toString());
>         return new JSONObject("response", "ok4");
>     }

Why don't you use a query parameter to pass a String containing a  
JSON-encoded object and then use the JSONObject(String string) to parse it?

-- 
Thiago H. de Paula Figueiredo

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