【问题标题】:GWT form pass a int to a doGet() servletGWT 表单将 int 传递给 doGet() servlet
【发布时间】:2019-06-18 14:53:44
【问题描述】:

我使用 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() 。谢谢,我被难住了

【问题讨论】:

    标签: java servlets gwt


    【解决方案1】:

    操作字段中的查询参数被忽略 (submitting a GET form with query string params and hidden params disappear)。您应该将其添加为隐藏参数 (how can i add hidden data on my formPanel in gwt):

    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中传输。

    【讨论】:

    • 这很好用。感谢您花时间帮助我。非常感谢。我从来没有在我的任何搜索中看到过这个。你搞定了。谢谢
    • 此解决方案会导致问题。 “com.google.gwt.event.shared.UmbrellaException:异常捕获:SimplePanel 只能包含一个子小部件”我有一个下拉菜单,当我选择一个项目时,我将 id 作为隐藏传递。因此,每次我进行新选择时都会调用此表单。我注释掉了隐藏的并且没有错误。有没有办法清除它然后重新添加它?
    • 我最终做的是创建一个新的隐藏然后附加它,然后在发送表单后我将其删除。现在似乎可以工作了。
    • 哦,是的,您需要添加一个容器面板,例如 FlowPanel。稍后我会更新示例。
    猜你喜欢
    • 2015-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-07
    • 2014-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多