首页 > 解决方案 > GWT 表单将 int 传递给 doGet() servlet

问题描述

我使用 doGet() 方法向 servlet 提交表单。我需要的是通过 doGet() 将 id 传递给 servlet 并在该方法中检索它。

到目前为止我尝试了什么:添加一个 id 作为查询字符串并在 doGet 中使用 request.getParameter()。我在 doPost() 及其工作中使用了相同的方法。

客户端代码

downloadPanel = new FormPanel();
downloadPanel.setEncoding(FormPanel.ENCODING_MULTIPART);
downloadPanel.setMethod(FormPanel.METHOD_GET);

downloadPanel.setAction(GWT.getModuleBaseURL()+"downloadfile" + "?entityId="+ 101);
downloadPanel.submit();  

服务器端 servlet

public class FileDownload extends HttpServlet {

private String entityId;

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

entityId = request.getParameter("entityId");

entityId 为空。如何将 Id 传递给 doGet() 请求?至于在线查看示例,这应该可以正常工作,因为它适用于 doPost() 。谢谢,因为我很难过

标签: javaservletsgwt

解决方案


操作字段中的查询参数被忽略(提交带有查询字符串参数和隐藏参数的 GET 表单消失)。您应该将其添加为隐藏参数(如何在 gwt 的 formPanel 上添加隐藏数据):

FormPanel form = new FormPanel();
form.setEncoding(FormPanel.ENCODING_URLENCODED); // use urlencoded
form.setMethod(FormPanel.METHOD_GET);
FlowPanel fields = new FlowPanel(); // FormPanel only accept one widget
fields.add(new Hidden("entityId", "101")); // add it as hidden
form.setWidget(fields); 
form.setAction(GWT.getModuleBaseURL() + "downloadfile");
form.submit(); // then the browser will add it as query param!

如果你不使用urlencoded它,它也可以使用request.getParameter(…),但它会在正文而不是 URL 中传输。


推荐阅读