You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Borut Bolčina <bo...@gmail.com> on 2007/10/03 20:59:57 UTC

[T5] Basic Ajax

Hello,

I am trying to create a T5 version of
http://java.sun.com/developer/technicalArticles/J2EE/AJAX/

How should a url look like to be T5-ish in this javascript function:

function validate() {
   var idField = document.getElementById("userid");
   var url = "validate?id=" + encodeURIComponent(idField.value);
   if (typeof XMLHttpRequest != "undefined") {
       req = new XMLHttpRequest();
   } else if (window.ActiveXObject) {
       req = new ActiveXObject("Microsoft.XMLHTTP");
   }
   req.open("GET", url, true);
   req.onreadystatechange = callback;
   req.send(null);
}

and how should a method in my page responsible for handling the ajax request
look like?

Thanks,
Borut

Re: [T5] Basic Ajax

Posted by Nick Westgate <ni...@key-planning.co.jp>.
You want to use an action link, not a page link.

See the example in this post:
http://article.gmane.org/gmane.comp.java.tapestry.user/49791/

You'll probably want to alter it to do something like:
     return TextStreamResponse("text/html", ...)

Cheers,
Nick.


Borut Bolčina wrote:
> I am making some progress. I my AjaxBasic.tml file I have a <script> section
> which has
> 
> var url = "ajaxbasic/" + encodeURIComponent(idField.value);
> 
> This idField gets from
> <input type="text" id="userid" name="id" onkeyup="validate();"/>
> 
> In my AjaxBasic.java I have
> public class AjaxBasic {
> 
>     private HttpServletRequest request;
>     private HttpServletResponse response;
> 
>     @Inject
>     private RequestGlobals _requestGlobals;
> 
>     private HashMap<String, String> users = new HashMap<String, String>();
> 
>     void pageLoaded() {
>         System.out.println("AjaxBasic PageLoaded");
>         users.put("greg", "account data");
>         users.put("duke", "account data");
>     }
> 
>     void onActivate(String id) throws IOException {
>         request = _requestGlobals.getHTTPServletRequest();
>         response = _requestGlobals.getHTTPServletResponse();
>         System.out.println("AjaxBasic onActivate");
>         System.out.println("id: " + id);
>         if ((id != null) && users.containsKey(id.trim())) {
>             response.setContentType("text/xml");
>             response.setHeader("Cache-Control", "no-cache");
>             response.getWriter().write("<message>valid</message>");
>         } else {
>             response.setContentType("text/xml");
>             response.setHeader("Cache-Control", "no-cache");
>             response.getWriter().write("<message>invalid</message>");
>         }
>     }
> }
> 
> The problem is, that the response is not only the <message>valid</message>
> or <message>invalid</message>
> 
> but the whole page's html is appended. How to prevent that?
> 
> Thanks,
> Borut
> 
> 
> 
> 2007/10/3, Thiago H. de Paula Figueiredo <th...@gmail.com>:
>> Em Wed, 03 Oct 2007 15:59:57 -0300, Borut Bolčina
>> <bo...@gmail.com> escreveu:
>>
>>> Hello,
>>> How should a url look like to be T5-ish in this javascript function:
>> What about . . .
>>
>>> function validate() {
>>>    var idField = document.getElementById("userid");
>>>    var url = "validate/111=" + encodeURIComponent(idField.value);
>>                          ^^^^
>>
>> This way, we could use the activation context and make it very T5-ish. :)
>>
>> Thiago
>>
>> ---------------------------------------------------------------------
>> 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: [T5] Basic Ajax

Posted by Borut Bolčina <bo...@gmail.com>.
I am making some progress. I my AjaxBasic.tml file I have a <script> section
which has

var url = "ajaxbasic/" + encodeURIComponent(idField.value);

This idField gets from
<input type="text" id="userid" name="id" onkeyup="validate();"/>

In my AjaxBasic.java I have
public class AjaxBasic {

    private HttpServletRequest request;
    private HttpServletResponse response;

    @Inject
    private RequestGlobals _requestGlobals;

    private HashMap<String, String> users = new HashMap<String, String>();

    void pageLoaded() {
        System.out.println("AjaxBasic PageLoaded");
        users.put("greg", "account data");
        users.put("duke", "account data");
    }

    void onActivate(String id) throws IOException {
        request = _requestGlobals.getHTTPServletRequest();
        response = _requestGlobals.getHTTPServletResponse();
        System.out.println("AjaxBasic onActivate");
        System.out.println("id: " + id);
        if ((id != null) && users.containsKey(id.trim())) {
            response.setContentType("text/xml");
            response.setHeader("Cache-Control", "no-cache");
            response.getWriter().write("<message>valid</message>");
        } else {
            response.setContentType("text/xml");
            response.setHeader("Cache-Control", "no-cache");
            response.getWriter().write("<message>invalid</message>");
        }
    }
}

The problem is, that the response is not only the <message>valid</message>
or <message>invalid</message>

but the whole page's html is appended. How to prevent that?

Thanks,
Borut



2007/10/3, Thiago H. de Paula Figueiredo <th...@gmail.com>:
>
> Em Wed, 03 Oct 2007 15:59:57 -0300, Borut Bolčina
> <bo...@gmail.com> escreveu:
>
> > Hello,
> > How should a url look like to be T5-ish in this javascript function:
>
> What about . . .
>
> > function validate() {
> >    var idField = document.getElementById("userid");
> >    var url = "validate/111=" + encodeURIComponent(idField.value);
>                          ^^^^
>
> This way, we could use the activation context and make it very T5-ish. :)
>
> Thiago
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

Re: [T5] Basic Ajax

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
Em Wed, 03 Oct 2007 15:59:57 -0300, Borut Bolčina  
<bo...@gmail.com> escreveu:

> Hello,
> How should a url look like to be T5-ish in this javascript function:

What about . . .

> function validate() {
>    var idField = document.getElementById("userid");
>    var url = "validate/111=" + encodeURIComponent(idField.value);
                         ^^^^

This way, we could use the activation context and make it very T5-ish. :)

Thiago

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