一个form中对应多个动作

如果想要给一个form表单赋予多种操作。如:

在这个表单中,希望可以有“删除”和“移动”两种动作。但是一个form只有一个action处理

要怎样才能用一个action应对两种不同的操作呢?

在servlet的参数HttpServletRequest req中,有一个函数,名为getParameterNames()。

这个参数的含义是:

Returns an Enumeration of String objects containing the names of the parameters contained in this request. If the request has no parameters, the method returns an empty Enumeration.
Returns:
an Enumeration of String objects, each String containing the name of a request parameter; or an empty Enumeration if the request has no parameters

也就是说它可以返回request对象里面的所有参数的名字。

这样一来,我们可以通过赋予不同的按钮以不同的名字来区分我们点击的是哪个操作。

如:

在servlet中打印输出getParameterNames()函数返回的数组,可以看到:

当我们点击“删除”按钮时,输出的内容是:

当我们点击“移动”按钮时,输出的内容是:

也就是说各个操作只会出现一个,因此只要进行逻辑判断就可以知道选择的那些checkbox是希望进行什么操作的了。

原文地址:https://www.cnblogs.com/elaron/p/2719635.html