GWT 2.5.1 / App Engine SDK 1.8.9
我正在尝试将.CVS文件上传到要使用FormPanel解析的servlet,但是我没有收到来自servlet的响应。
// Create a FormPanel and point it at a service
final FormPanel form = new FormPanel();
form.setAction(GWT.getModuleBaseURL() + "uploadServlet");
System.out.println(GWT.getModuleBaseURL() + "uploadServlet");
//gives me http:// . . . /healthybc/uploadServlet
// set form to use the POST method, and multipart MIME encoding
form.setEncoding(FormPanel.ENCODING_MULTIPART);
form.setMethod(FormPanel.METHOD_POST);
form.setWidget(buttonPanel);
final FileUpload fileUpload = new FileUpload();
fileUpload.setName("Browse");
buttonPanel.add(fileUpload);
// Add a 'submit' button.
Button submit = new Button("Submit");
submit.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
form.submit();
}
});
buttonPanel.add(submit);
// Add an event handler to the form.
form.addSubmitHandler(new FormPanel.SubmitHandler() {
public void onSubmit(SubmitEvent event) {
// This event is fired just before the form is submitted. We can take
// this opportunity to perform validation.
if (fileUpload.getFilename().length() == 0) {
Window.alert("The text box must not be empty");
event.cancel();
}
else if (!fileUpload.getFilename().endsWith(".csv")){
Window.alert("Can only upload .csv files");
event.cancel();
}
}
});
form.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
@Override
public void onSubmitComplete(SubmitCompleteEvent event) {
// When the form submission is successfully completed, this event is
// fired. Assuming the service returned a response of type text/html,
// we can get the result text here (see the FormPanel documentation for
// further explanation).
Window.alert(event.getResults());
}
});
我从onSubmit()获取窗口,但是onSubmitComplete没有响应。
抱怨web.xml代码
<servlet>
<servlet-name>uploadedCSVParser</servlet-name>
<servlet-class>ca.ubc.cs310.gwt.healthybc.server.UploadedCSVParser
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>uploadedCSVParser</servlet-name>
<url-pattern>/healthybc/uploadServlet</url-pattern>
</servlet-mapping>
public class UploadedCSVParser extends HttpServlet{
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title> servlet title</title>");
out.println("</head>");
out.println("<body>");
out.println("</body>");
out.println("TEXT GOES HERE");
out.println("</html>");
out.close();
}
现在服务器代码就在那里,看看我是否真的得到了回复。 直接在我的浏览器中键入URL ...“/ healthybc / uploadServlet”(我使用的是firefox)给了我预期的“TEXT GOES HERE”输出,但我没有收到任何迹象表明onSubmitComplete在表单提交时运行。我开始认为信息永远不会到达servlet。
我做错了吗?有没有简单的方法让我检查servlet是否真的被诱发?
答案 0 :(得分:0)
我以某种方式解决了它。我从一个已知的工作模板中重写了客户端,我认为它的顺序可能与我最初的模式略有不同。
final FormPanel form = new FormPanel();
form.setMethod(FormPanel.METHOD_POST);
form.setEncoding(FormPanel.ENCODING_MULTIPART);
form.setAction("/healthybc/uploadServlet");
// then there's a button they can click which calls form.submit();
form.setWidget(new Button("Submit", (new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
form.submit();
}
})));
buttonPanel.add(form);
final FileUpload fileUpload = new FileUpload();
fileUpload.setName("Browse");
buttonPanel.add(clinicFileUpload);
// Add an event handler to the form.
form.addSubmitHandler(new FormPanel.SubmitHandler() {
public void onSubmit(SubmitEvent event) {
// This event is fired just before the form is submitted. We can take
// this opportunity to perform validation.
if (fileUpload.getFilename().length() == 0) {
Window.alert("The text box must not be empty");
event.cancel();
}
else if (!fileUpload.getFilename().endsWith(".csv")){
Window.alert("Can only upload .csv files");
event.cancel();
}
}
});
form.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
@Override
public void onSubmitComplete(SubmitCompleteEvent event) {
// When the form submission is successfully completed, this event is
// fired. Assuming the service returned a response of type text/html,
// we can get the result text here (see the FormPanel documentation for
// further explanation).
Window.alert(event.getResults());
}
});