java GWT:如何从 FormPanel 中获取提交结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/950949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
GWT: How to get a submission result out of FormPanel
提问by ciukes
The example from FormPanel's javadocsays:
"...Assuming the service returned a response of type text/html, we can get the result text here (see the FormPanel documentation for further explanation)..."
“...假设服务返回了一个 text/html 类型的响应,我们可以在此处获取结果文本(请参阅 FormPanel 文档以获得进一步的解释)...”
However the javadoc doesn't explain a bit about this topic. Has anyone found how to get the HTML response sent back from server after a form submission?
然而,javadoc 并没有解释这个主题。有没有人找到如何在表单提交后从服务器发回 HTML 响应?
回答by Robert Munteanu
Add a FormHandler to your FormPanel, and in onSubmitComplete you will receive a FormSubmitCompleteEvent. Invoke its getResults() method to obtain the result.
将 FormHandler 添加到您的 FormPanel,然后在 onSubmitComplete 中您将收到一个 FormSubmitCompleteEvent。调用其 getResults() 方法以获取结果。
form.addFormHandler(new FormHandler() {
public void onSubmit(FormSubmitEvent event) { // validation etc }
public void onSubmitComplete(FormSubmitCompleteEvent event} {
Window.alert(event.getResults()); // display the result
}
};
回答by Ilya Basin
This problem only happens in a debug mode. Check this out: http://code.google.com/p/google-web-toolkit/issues/detail?id=3832
此问题仅在调试模式下发生。看看这个:http: //code.google.com/p/google-web-toolkit/issues/detail?id=3832
回答by Roman M
Following the answear from "Robert Munteanu" you should look at:
按照“Robert Munteanu”的回答,你应该看看:
And there you can see :
在那里你可以看到:
getResults
获取结果
public java.lang.String getResults()
公共 java.lang.String getResults()
Gets the result text of the form submission.
获取表单提交的结果文本。
Returns:
返回:
the result html, or null if there was an error reading it
结果 html,如果读取时出错,则为 null
Tip:
小费:
The result html can be null as a result of submitting a form to a different domain.
由于将表单提交到不同的域,结果 html 可能为 null。
回答by Charilaos
//=========== in the client side:
//============ 在客户端:
SubmitCompleteHandler sch = new SubmitCompleteHandler()
{
public void onSubmitComplete(SubmitCompleteEvent event)
{
//get back the data results that had input the .xml
String dpsString = event.getResults();
//And do your wanted action with the result
System.out.println(dpsString);
}
};
uploadForm.addSubmitCompleteHandler(sch);
//=========== in the server side:
//========== 在服务器端:
// parse and handle file, e.g. if there is an xml file
...
InputStream fileImputStream = uploadItem.getInputStream();
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(fileInputStream);
doc.getDocumentElement().normalize();
System.out.println("Root element of the doc is " + doc.getDocumentElement().getNodeName());
...
//Response to the request with the result
dpsString = doc.getDocumentElement().getNodeName();
response.getWriter().write(new String(dpsString));
回答by Rubber Duck
I tried replacing local host with machine name it didn't help [some people say it helped them], but it is a cross domain java-script issue so I tried just using the URL pattern assigned in the web xml and I got a String result. for details look up rubber duck answer in this link
我尝试用机器名替换本地主机它没有帮助[有人说它帮助了他们],但这是一个跨域的java脚本问题,所以我尝试只使用web xml中分配的URL模式,我得到了一个字符串结果。有关详细信息,请在此链接中查找橡皮鸭答案
回答by mrh
I was able to get result message on DEV mode with the following code:
我能够使用以下代码在 DEV 模式下获得结果消息:
client side:
客户端:
FormPanel formPanel = new FormPanel();
FormElement.as(formPanel.getElement()).setAcceptCharset("UTF-8");
formPanel.setAction(sb.toString());
formPanel.setMethod(FormPanel.METHOD_POST);
formPanel.setVisible(false);
RootPanel.get().add(formPanel);
formPanel.submit();
formPanel.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
@Override
public void onSubmitComplete(SubmitCompleteEvent event) {
String results = event.getResults();
}
});
server side:
服务器端:
response.setContentType("text/html");
response.setHeader("Pragma", "No-cache");
response.setDateHeader("Expires", 0);
response.setHeader("Cache-Control", "no-cache");
response.getWriter().write(e.getMessage());
Browser: Firefox 24.3.0, GWT Version: 2.5.1
浏览器:Firefox 24.3.0,GWT 版本:2.5.1
回答by Sebastian
For me the problem was that I constructed my FormPanelwith the FormPanel(String)constructor. With the default constructor, my SubmitCompleteHandlerjust worked.
对我来说,问题是我FormPanel用FormPanel(String)构造函数构造了我的。使用默认构造函数,我SubmitCompleteHandler刚刚工作。

