Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Struts2表单有两个按钮--操作未正确处理_Java_Jakarta Ee_Struts2 - Fatal编程技术网

Java Struts2表单有两个按钮--操作未正确处理

Java Struts2表单有两个按钮--操作未正确处理,java,jakarta-ee,struts2,Java,Jakarta Ee,Struts2,我试着在论坛上搜索这个问题的答案,但没有成功。我有一个带有两个按钮的Struts2表单,我希望action类根据按下的按钮执行不同的操作。那是不会发生的。有人能帮我吗?这是我的表格,后面是动作课 <s:form action="ApproveBulletin" method="post"> <table> <tr> <td colspan="2"><b>Fr

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

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

发件人:
主题:
日期:

公共字符串execute(){ BulletinDAO BulletinDAO实例=新BulletinDAO(); 如果(“批准”。等于(按钮名称)){ if(bulletinDAOInstance.approveBulletin(id)=true){ 返回“重定向”; } } 如果(“拒绝”。等于(按钮名称)){ if(bulletinDAOInstance.denyBulletin(id)=true){ 返回“重定向”; } } 返回“失败”; }
我认为您应该在“struts.xml”文件中为这两个按钮执行两种不同的操作。 在jsp中,您可以通过javascript为相应的按钮设置表单操作

在Jsp上:

对于“Approve Button”,通过javascript将操作设置为“ApproveAction”,对于“Deny Button”,将操作设置为“DenyAction”

示例Struts.xml

。。。。
/pages/Approve.jsp
/pages/Deny.jsp
...

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

以下是我提出的解决方案的操作代码

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

我想我已经发现了另一种不用JavaScript的方法,不过还是要谢谢你。嘿,你能用另一种方法分享吗?
    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";