Struts2 with two buttons and mdash; the action is not properly processed

advertisements

I've tried searching the forums for the answer to this, and I've been unsuccessful. I have a Struts2 form with two buttons, and I want the action class to perform a different action depending on which button was pressed. That's not happening. Can anyone help me with this? Here is my form, followed by the action class.

    <s:form action="ApproveBulletin" method="post">
        <table>
            <tr>
                <td colspan="2"><b>From:</b> <s:property value="name" /></td>
            </tr>
            <tr>
                <td colspan="2"><b>Subject:</b> <s:property value="subject" /></td>
            </tr>
            <tr>
                <td colspan="2"><b>Date:</b> <s:property value="date" /> <br>
                </td>
            </tr>
            <tr>
                <td colspan="2"><s:property value="note" />
                    <s:hidden name="id" value="%{id}" /></td>
            </tr>
            <tr>
                <td><s:submit type="button" name="approve" value="approve" label="Approve" /></td>
                <td><s:submit type="button" name="deny" value="deny" label="Deny" /></td>
            </tr>
        </table>
        <br />
    </s:form>

public String execute() {
    BulletinDAO bulletinDAOInstance = new BulletinDAO();

    if ("Approve".equals(buttonName)) {
        if (bulletinDAOInstance.approveBulletin(id) == true) {
            return "redirect";
        }
    }

    if ("Deny".equals(buttonName)) {
        if (bulletinDAOInstance.denyBulletin(id) == true) {
            return "redirect";
        }
    }

    return "failure";
}


I think you should have 2 different actions in the "struts.xml" file for the 2 buttons. In jsp, you can set the form action for the corresponding button through javascript.

On Jsp:

For "Approve Button" set action as "ApproveAction", for "Deny Button" set action as "DenyAction" through javascript

Sample Struts.xml

....
<action name="ApproveAction" class="com.package.YourActionClass" method="approve">
<result>/pages/Approve.jsp</result>
</action>

<action name="DenyAction" class="com.package.YourActionClass" method="deny">
<result>/pages/Deny.jsp</result>
</action>
...

In "YourActionClass", you can then write your code in two different method i.e "approve" and "deny".