[T5] Basic Ajax

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

[T5] Basic Ajax

Borut Bolčina
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
Reply | Threaded
Open this post in threaded view
|

Re: [T5] Basic Ajax

Thiago H de Paula Figueiredo
Em Wed, 03 Oct 2007 15:59:57 -0300, Borut Bolčina  
<[hidden email]> 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: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [T5] Basic Ajax

Borut Bolčina
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 <[hidden email]>:

>
> Em Wed, 03 Oct 2007 15:59:57 -0300, Borut Bolčina
> <[hidden email]> 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: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
Reply | Threaded
Open this post in threaded view
|

Re: [T5] Basic Ajax

Nick Westgate
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 <[hidden email]>:
>> Em Wed, 03 Oct 2007 15:59:57 -0300, Borut Bolčina
>> <[hidden email]> 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: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]