lapots
lapots

Reputation: 13395

check what button on form caused action

I've got such form on my page

<form action="ToolbarAction.do" method="POST">
    <div id="toolbar"
        class="ui-widget-header ui-corner-all ui-widget-content">
        <input style="font-family: times new roman;" type="button" id="item0"
            value="<fmt:message key='main'/>" /> <input
            style="font-family: times new roman;" type="button" id="item1"
            value="<fmt:message key='prices'/>" />
        <c:choose>
            <c:when test="${enterAttr == false || enterAttr == null }">
                <input style="font-family: times new roman;" type="submit"
                    id="item2" value="<fmt:message key='book'/>" />
            </c:when>
            <c:when test="${enterAttr == true }">
                <input style="font-family: times new roman;" type="submit"
                    id="item2" value="<fmt:message key='check'/>" />
            </c:when>
        </c:choose>
        <input style="font-family: times new roman;" type="submit" id="item3"
            value="<fmt:message key='contacts'/>" /> <input
            style="font-family: times new roman;" type="submit" id="item4"
            value="<fmt:message key='service'/>" />
    </div>
</form>

How to check what button was pressed and caused the ToolbarAction? Here exec method in ToolbarAction classs. I should get some parameters from HttpServletRequest?

public String exec(HttpServletRequest req, HttpServletResponse resp) {
    // TODO Auto-generated method stub
    return null;
}

Upvotes: 1

Views: 967

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279970

The solution is to give the same name attribute to all your <input> elements.

<input name="submit" style="font-family: times new roman;" type="submit"
                id="item2" value="<fmt:message key='check'/>" />

Since only one submit button can be pressed by the user for each request, you will have a single request parameter called submit. You can retrieve it like

String value = request.getParameter("submit");

where request is the HttpServletRequest object. The return value of getParameter is the value attribute of the <input> element. You can do a bunch of if checks to see which was pressed.

Upvotes: 1

Related Questions