給提交按鈕一個名稱和值通常的方式。
<input type="submit" name="action" value="action1">
...
<input type="submit" name="action" value="action2">
...
<input type="submit" name="action" value="action3">
按下按鈕也可以作爲請求參數。
String action = request.getParameter("action");
if ("action1".equals(action)) {
// action1 button is pressed.
} else ("action2".equals(action)) {
// action2 button is pressed.
} else ("action3".equals(action)) {
// action3 button is pressed.
}
必要時,可以給他們一個不同的名稱,而不是再nullcheck每個請求的參數。
<input type="submit" name="action1" value="This is more i18n friendly">
...
<input type="submit" name="action2" value="Blah">
...
<input type="submit" name="action3" value="More blah">
與
if (request.getParameter("action1") != null) {
// action1 button is pressed.
} else (request.getParameter("action2") != null) {
// action2 button is pressed.
} else (request.getParameter("action3") != null) {
// action3 button is pressed.
}
或者,如果他們其實都在自己<form>
,那麼你也可以一起傳遞一個隱藏的輸入。
<form>
<input type="hidden" name="action" value="action1">
...
</form>
<form>
<input type="hidden" name="action" value="action2">
...
</form>
<form>
<input type="hidden" name="action" value="action3">
...
</form>
具有與第一個例子中相同的服務器端處理。
你能有點模糊嗎? – Pointy 2010-12-01 01:44:58