FormBuilder that does just that.
The idea: use "invisible (Display: none)" FormPanel and fill it
correctly and post it to the backend.
RequestBuilder is nice, but not if you need a synchronized call to
the backend like a FORM POST that will return a PDF/HTML stream that
the user downloads/opens.
Below the FormBuilder class.
Usage example (send html plus style to the backend and return PDF from
it:
----
final FormBuilder builder = new FormBuilder(Method.POST,
GWT.getHostPageBaseURL() + "report",
getCmsFacade().pdfDownloadTarget());
builder.addValue(ConstantsServicePlus.HTTP_REQ_HTML_SRC,
html.getHtml());
builder.addValue(ConstantsServicePlus.HTTP_REQ_HTML_CSS,
html.getStyle());
builder.addValue(ConstantsServicePlus.HTTP_REQ_CLIENT_NR,
getCustomerNumber());
...
builder.submit();
----
I use it for some time now, and works very well.
I hope it helps you,
- Ed
-----
public final class FormBuilder {
private FormPanel formPanel;
public FormBuilder() {
}
public FormBuilder(final Method method, final String action, final
String target) {
setMethod(method);
setAction(action);
setTarget(target);
}
public void submit() {
try {
RootPanel.get().add(getEnsureFormPanel());
getEnsureFormPanel().submit();
}
finally {
RootPanel.get().remove(getEnsureFormPanel());
}
}
public void setAcceptedCharset(final String charsets) {
getEnsureFormPanel().getElement().setAttribute("accept-charset",
charsets);
}
public void setAction(final String action) {
getEnsureFormPanel().setAction(action);
}
public void setMethod(final Method method) {
getEnsureFormPanel().setMethod(method.getValue());
}
public void setTarget(final String target) {
getEnsureFormPanel().getElement().setAttribute("target", target);
}
public void addValue(final String name, final String value) {
final InputElement el = Document.get().createTextInputElement();
getEnsureFormPanel().getElement().appendChild(el);
el.setName(name);
el.setValue(value);
}
public void removeAllValues() {
final int size = getEnsureFormPanel().getElement().getChildCount();
if (size > 0) {
for (int i = size - 1; i >= 0; i--) {
getEnsureFormPanel().getElement().getChild(i).removeFromParent();
}
}
}
//
//
private FormPanel getEnsureFormPanel() {
if (this.formPanel == null) {
this.formPanel = new FormPanel();
this.formPanel.getElement().getStyle().setDisplay(Display.NONE);
}
return this.formPanel;
}
/**
*/
public enum Method {
POST("POST"), GET("GET");
private final String value;
private Method(final String methodName) {
this.value = methodName;
}
public String getValue() {
return this.value;
}
}
}
--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
No comments:
Post a Comment