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?
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
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();
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.
Related
I'm trying to get a Contact Form 7 form to work in a Featherlight.js lightbox. I've created a page at mydomain.com/contact and set the link to open mydomain.com/contact #main article.
Featherlight does open the form, but when I submit the form, the lightbox closes and the url resolves to mydomain.com/contact/#wpcf7-f262-p11-o1. It doesn't matter if it's successfully submitted or there are validation errors, the lightbox still closes (to be clear, the form does actually work—I receive the email).
If I open the entire page (mydomain.com/contact/), the lightbox doesn't close on submission, which leads me to believe that perhaps there's an AJAX conflict.
That said, I don't receive any errors in the console.
Any help in resolving the issue would be appreciated!
Thanks.
I've got it working, thanks to the second part of the accepted answer here (the Documentation example from the jQuery website).
jQuery's submit() function didn't work for me—I'm guessing it's a version issue? In any case, this is my final code:
/* attach a submit handler to the form */
$( "body" ).on( "submit", ".wpcf7-form", function(event) {
/* stop form from submitting normally */
event.preventDefault();
/* get some values from elements on the page: */
var $form = $( this ),
url = $form.attr( 'action' );
/* Send the data using post */
var posting = $.post( url, $form.serialize() );
/* Put the results in a div */
posting.done(function(data) {
var content = $(data).find('.wpcf7-form');
$('.featherlight .wpcf7').empty().append(content);
});
});
I created an ordinary link, nothing fancy:
Link<Integer>link=new Link<Integer>("link") {
#Override
public void onClick() {
setResponsePage(MyTarget.class);
}
};
In HTML I see:
<a wicket:id="link" href="./?0-3.ILinkListener-link">link</a>
When I click the link, I see in the URL field:
http://localhost:8080/wicket/bookmarkable/my.test.own.wicket_quickstart.MyTarget
So are my links always bookmarkable?
Wicket links are mostly internal links and you cannot be sure about the generated URL. If you want to get an bookmarkable link, use Bookmarkable link instead. http://ci.apache.org/projects/wicket/apidocs/6.x/org/apache/wicket/markup/html/link/BookmarkablePageLink.html
Link<Integer>link = new Bookmarkable<Integer>("link", MyTarget.class);
If you want to have a nice URL, use page mounting / request mappin, see https://cwiki.apache.org/confluence/display/WICKET/Request+mapping
No.
Links will redirect you somewhere. If you use a Bookmarkable Page and setResponsePage you will get a nice redirected url.
You can also use a link to change some model value
Link<Integer>link=new Link<Integer>("link") {
#Override
public void onClick() {
myModel.setObject("Great success!");
}
};
In this case you will be taken back to your page.
I use the HtmlUnit to test login, and succeed in some websites.But I fail when encounter some websites that redirects after login.For example:
public static void main(String[] args) throws Exception{
WebClient webClient = new WebClient(BrowserVersion.FIREFOX_24);
webClient.getOptions().setJavaScriptEnabled(true);
webClient.getOptions().setRedirectEnabled(true);
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
HtmlPage page = webClient.getPage("http://www.javaprogrammingforums.com/forum.php");
HtmlTextInput user = (HtmlTextInput) page.getElementById("navbar_username");
user.type("willow");
HtmlPasswordInput password = (HtmlPasswordInput) page.getElementById("navbar_password");
password.click();
password.type("Willow1234");
HtmlSubmitInput submit = page.getFirstByXPath("//input[#value='Log in']");
page = (HtmlPage) submit.click();
Thread.sleep(3000);
System.out.println(page.asText());
webClient.closeAllWindows();
}
Then I got this:
Java Programming Forums - The Java Community
Redirecting...
Thank you for logging in, willow.
Click here if your browser does not automatically redirect you.
I don't want this.How can I get the page after redirecting?
And I know that I can click "Click here if your browser does not automatically redirect you." to get the page.But there are websites that have no "Click here...".They just give you a "Redircting..." message.How can I get the page in that case?
I am new to HtmlUnit.
Thank you very much!
I'm trying to implement a browser auto-complete feature for my application Login view. However it seems that the only solution is through FormPanel. The problem with this is that it is intended to be used with standard servlet; in this case I will need to rewrite my "login" code, since what I have is Login RPC. Is there a way to do browser login form auto-complete with using GWT RPC for the Login service?
EDIT:
I tried this code:
FormPanel form = FormPanel.wrap(Document.get().getElementById("login-input"), true);
form.setAction("javascript:;");
form.addFormPanel(new FormPanel() { // EDIT: method undefined?
public void onSubmit(FormSubmitEvent event) {
// do some validation before submitting (non-empty fields)
// and call event.setCancelled(true) if needed.
// get the fields values and do your GWT-RPC call or
// RequestBuilder thing here.
}
public void onSubmitComplete(FormSubmitCompleteEvent event) {
// will never be called.
}
});
However, form.addFormPanel method is undefined.
It's possible with GWT-RPC too (but you still need the FormPanel for similar <form>): https://groups.google.com/d/msg/google-web-toolkit/KyzgtqqoJGE/5bqvG8pBSRYJ
According to GWT FormPanel Javadoc :
http://google-web-toolkit.googlecode.com/svn/javadoc/latest/com/google/gwt/user/client/ui/FormPanel.html
replace form.addFormPanel with :
the deprecated method form.addFormHandler
which is now replaced with
form.addSubmitCompleteHandler and form.addSubmitHandler
I am trying to use GWT to download the source code of web pages, but i do not know where to start, can anyone gives me some key word that i can search on google, or gives me some links from tutorials.
Thanks!!
In JavaScript, this is typically done with an XMLHttpRequest. GWT's analog to XMLHttpRequest is RequestBuilder, which can be used like so:
new RequestBuilder("GET", "http://example.com/page.html").sendRequest("", new RequestCallback() {
#Override
public void onResponseReceived(Request request, Response response) {
String src = response.getText();
// do things with the source
}
#Override
public void onError(Request request, Throwable throwable) {
// handle the error
}
});
Some GWT manual about cross-site scripting
https://developers.google.com/web-toolkit/doc/latest/tutorial/Xsite
And here some discussion about using RequestBuilder and JSNI
GWT RequestBuilder - Cross Site Requests
As alternative you can do a page download on the server-side...