java - 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() 。谢谢,因为我很难过
解决方案
操作字段中的查询参数被忽略(提交带有查询字符串参数和隐藏参数的 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 中传输。
推荐阅读
- python - tkinter GUI 不会每 10 秒更新一次标签并且不会关闭
- fortran - 如何正确地将 C 字符串读入未指定长度的 Fortran 字符串?
- date - 日期格式“月,YYYY”有名称吗?例如“2021 年 8 月”
- wordpress - 我的 IP 地址出现异常活动,网站访问被阻止
- sql-server - SSRS 参数未填充
- python - Pandas:如何将两个不完整的数据帧合并或合并成一个完整的数据帧
- reactjs - React:通过包装器/便利组件上的道具
- django - 从邮局发送 Excel 文件
- azure - 不接受 Blob 存储中 Azure 应用程序的重定向 URL
- c++ - 使用 C++ 在 OpenGL 中渲染纹理时的问题