Tom
Tom

Reputation: 13

GWT, navigate to page using POST request

I want to navigate away from my GWT app using a POST request. If it was a GET I could just use Window.Location and if I didn't need it to be dynamic I could hardcode a Form and submit it. The FormPanel seems to be the answer for creating and submitting forms, but it does it asynchronously, and I want the user's browser to follow the form submit, navigating away from my app, rather than just displaying the results.

Anybody know how to do this in Google Web Toolkit?

Upvotes: 1

Views: 2971

Answers (3)

Tom
Tom

Reputation: 46

I thought your FormElement idea would work, but unfortunately it still sends it async. Both the following successfully send a request and get a response, but alas, the page doesn't change.

_tmp.addClickHandler(new ClickHandler() 
{
    @Override
    public void onClick(ClickEvent event_)
    {
        doPost();
    }

    public native void doPost() /*-{
        var form = document.createElement("form");
        form.setAttribute("method", "GET");
        form.setAttribute("action", "http://www.google.com");
        document.body.appendChild(form);    
        form.submit();
    }-*/;
});

and

public void onClick(ClickEvent event_)
{
    final FormPanel form = new FormPanel();
    form.setAction("http://www.google.com");
    form.setMethod(FormPanel.METHOD_GET);
    RootPanel.get("main").add(form);
    FormElement formElement = FormElement.as(form.getElement());
    formElement.submit();
}

I realize that I've used GET methods in my examples above. This is purely because Google only accepts GET. I had the same result trying POST on my own servlets.

There must be a way of doing this.

Upvotes: 0

Tom
Tom

Reputation: 46

Ok, got it!

Passing null to the String constructor of the FormPanel effectively says "replace the current page":

new FormPanel((String)null);

This forum thread was useful: http://www.coderanch.com/t/120264/GWT/GWT-HTTP-post-requests

Upvotes: 3

pathed
pathed

Reputation: 669

Havn't done this myself but I think you should be able to create a FormPanel and then cast its element to FormElement and call submit on the FormElement.

FormPanel formPanel = new ...
FormElement form = FormElement.as(formPanel.getElement());
form.submit();

Upvotes: 0

Related Questions