使用URL标识所选按钮

时间:2011-06-09 14:42:13

标签: parsing url servlets

我有一个包含多个按钮的页面,每个按钮都有一个名称和值属性。单击按钮后,页面将被重定向到另一个页面,该页面在具有所选按钮名称和值的表单的操作属性中给出。

我想要检索下一页中附加了网址的所选按钮名称或ID。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:1)

如果您为按钮指定名称,则它仅作为请求参数提供。

E.g。

<input type="submit" name="add" value="Add" />
<input type="submit" name="edit" value="Edit" />
<input type="submit" name="delete" value="Delete" />

with servlet

if (request.getParameter("add") != null) {
    // Add button was pressed.
} else if (request.getParameter("edit") != null) {
    // Edit button was pressed.
} else if (request.getParameter("delete") != null) {
    // Delete button was pressed.
} else {
    // Handle exceptional/alternative case yourself.
}

或在JSP

<c:choose>
    <c:when test="${not empty param.add}">
        Add button was pressed.
    </c:when>
    <c:when test="${not empty param.edit}">
        Edit button was pressed.
    </c:when>
    <c:when test="${not empty param.delete}">
        Delete button was pressed.
    </c:when>
    <c:otherwise>
        Handle exceptional/alternative case yourself.
    </c:otherwise>
</c:choose>