具有两个按钮的 Struts2 表单 — 操作未正确处理

发布于 2024-10-20 00:31:04 字数 1680 浏览 4 评论 0原文

我尝试在论坛中搜索这个问题的答案,但没有成功。我有一个带有两个按钮的 Struts2 表单,我希望操作类根据按下的按钮执行不同的操作。那并没有发生。谁能帮我解决这个问题吗?这是我的表格,后面是动作类。

    <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'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";
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迎风吟唱 2024-10-27 00:31:04

我认为您应该在“struts.xml”文件中为这两个按钮设置两个不同的操作。
在jsp中,可以通过javascript设置对应按钮的form action。

在 Jsp 上:

对于“批准按钮”,将操作设置为“ApproveAction”,对于“拒绝按钮”,通过 javascript 将操作设置为“DenyAction”

示例 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>
...

在“YourActionClass”中,然后您可以用两种不同的方法编写代码,即“批准”和“拒绝”。

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

别在捏我脸啦 2024-10-27 00:31:04

这是我想出的解决方案的操作代码。

    BulletinDAO bulletinDAOInstance = new BulletinDAO();

    if (approve != null) {
        if (bulletinDAOInstance.approveBulletin(id) == true) {
            HttpSession session = (HttpSession) request.getSession();
            session.setAttribute("confirmation",
                    "Your bulletin has been approved.");
            return "success";
        }
    }

    if (deny != null) {
        if (bulletinDAOInstance.denyBulletin(id) == true) {
            HttpSession session = (HttpSession) request.getSession();
            session.setAttribute("confirmation",
                    "Your bulletin has been denied.");
            return "success";
        }
    }

    return "failure";

Here is the action code of the solution I came up with.

    BulletinDAO bulletinDAOInstance = new BulletinDAO();

    if (approve != null) {
        if (bulletinDAOInstance.approveBulletin(id) == true) {
            HttpSession session = (HttpSession) request.getSession();
            session.setAttribute("confirmation",
                    "Your bulletin has been approved.");
            return "success";
        }
    }

    if (deny != null) {
        if (bulletinDAOInstance.denyBulletin(id) == true) {
            HttpSession session = (HttpSession) request.getSession();
            session.setAttribute("confirmation",
                    "Your bulletin has been denied.");
            return "success";
        }
    }

    return "failure";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文