1

私のページにそのようなフォームがあります

<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>

どのボタンが押されて ToolbarAction が発生したかを確認する方法は? execToolbarAction クラスのHereメソッド。HttpServletRequest からいくつかのパラメーターを取得する必要がありますか?

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

1 に答える 1

1

解決策は、すべての要素に同じname属性を与えることです。<input>

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

各リクエストに対してユーザーが押すことができる送信ボタンは 1 つのみであるため、 という単一のリクエスト パラメータがありますsubmit。次のように取得できます

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

オブジェクトrequestはどこですか。HttpServletRequestの戻り値は要素getParametervalue属性です。<input>一連の if チェックを実行して、どれが押されたかを確認できます。

于 2013-07-25T15:00:04.543 に答える