【问题标题】:Can I have two submit buttons in a jsp to two different controllers?我可以在jsp中有两个提交按钮到两个不同的控制器吗?
【发布时间】:2011-07-10 05:15:53
【问题描述】:

我正在做一个项目,其中必须有一个功能允许用户更新和删除将动态显示给他们的表格的某些行。

用户将单击一个单选按钮来选择他想要更新或删除的行,然后单击更新或提交按钮。

根据他选择的update还是delete,我要把选中行的内容传给2个servlet。 现在,用于更新的 servlet 与删除的 servlet 不同。 我不能在表单的 action 属性中提及 url 模式,因为我需要根据用户的选择将值传输到 2 个不同的 servlet。

有可能实现吗?

请给我一些解决这个问题的方法。

【问题讨论】:

  • 为什么需要两个 servlet?为什么不只使用一个 servlet,而是根据按钮分派给不同的处理程序?这更符合 servlet 设计。
  • 但是如果更新和删除都是提交按钮,那么有没有办法找出哪个按钮被点击了?
  • 我真的没有太多关于这方面的知识。因此,如果您能提供一些想法,那将是非常可观的

标签: forms jsp servlets


【解决方案1】:

如果您单击该按钮提交表单,提交按钮的名称和值的属性也将被发布。在servlet中,你可以查看是否可以获取这些参数来知道点击了哪个按钮。

例如,假设您有两个按钮,一个用于更新,一个用于删除

<input type="submit" name="update" value="Update Button">
<input type="submit" name="delete" value="Delete Button">

如果点击更新按钮,它将发布变量update=Update Button 如果单击删除按钮,它将发布变量delete=Delete Button

然后在servlet中:

    if (request.getParameter("update") != null) {
        //update button is clicked
        //Do the update action or forward the request to the servlet to do update action 

    } else if (request.getParameter("delete") != null) {
          //delete button is clicked
          //Do the delete action or forward the request to the servlet to do delete action
    }

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多