我有一个带有2个参数的Spring 3 MVC表单,我试图将其发送到 Controller 方法,但出现404错误。这个问题的转折点在于该表单具有2个提交按钮,并且单击的提交按钮决定了其中一个参数的值。这是我的表格。

    <form:form action="/approve/${bulletin.id}" method="post">
        <table>
            <tr>
                <td colspan="2"><b>From:</b> <c:out value="${bulletin.name}" /></td>
            </tr>
            <tr>
                <td colspan="2"><b>Subject:</b> <c:out
                        value="${bulletin.subject}" /></td>
            </tr>
            <tr>
                <td colspan="2"><b>Date:</b> <c:out value="${bulletin.date}" />
                    <br></td>
            </tr>
            <tr>
                <td colspan="2"><t:notePrint note="${bulletin.note}" /> <input
                    type="hidden" name="id" value="${bulletin.id}" /></td>
            </tr>
            <tr>
                <td><input type="submit" name="approve" value="Approve" /></td>
                <td><input type="submit" name="deny" value="Deny" /></td>
            </tr>
        </table>
        <br />
    </form:form>

这是我的 Controller 表格。
@RequestMapping(value = "/approve/{id}", method = RequestMethod.POST)
public String approveBulletin(@RequestParam int id,
        @RequestParam(required = false, value = "approve") String approve,
        @RequestParam(required = false, value = "deny") String deny, Model model) {
    try {
        if (approve.equalsIgnoreCase("approve")) {
            bulletinDAO.approveBulletin(id);
            model.addAttribute("approval",
                    "Your bulletin has been approved.");
        }
        if (deny.equalsIgnoreCase("deny")) {
            bulletinDAO.denyBulletin(id);
            model.addAttribute("approval", "Your bulletin has been denied.");
        }

        List<Bulletin> bulletins = bulletinDAO.getApprovedBulletins();
        model.addAttribute("bulletins", bulletins);
    } catch (Exception e) {
        System.out.println(e.getMessage());
        return "FailurePage";
    }

    return "ApproveBulletin";
}

最佳答案

我已经解决了自己的问题。我发布我的代码是为了让其他遇到相同问题的线程受益。这是我的表格。

<form:form action="approve" method="post">
    <table>
        <tr>
            <td colspan="2"><b>From:</b> <c:out value="${bulletin.name}" /></td>
        </tr>
        <tr>
            <td colspan="2"><b>Subject:</b> <c:out
                    value="${bulletin.subject}" /></td>
        </tr>
        <tr>
            <td colspan="2"><b>Date:</b> <c:out value="${bulletin.date}" />
                <br></td>
        </tr>
        <tr>
            <td colspan="2"><t:notePrint note="${bulletin.note}" /> <input
                type="hidden" name="id" value="${bulletin.id}" /></td>
        </tr>
        <tr>
            <td><input type="submit" name="approve" value="Approve" /></td>
            <td><input type="submit" name="deny" value="Deny" /></td>
        </tr>
    </table>
    <br />
</form:form>

这是我的 Controller 方法。
@RequestMapping(value = "/approve", method = RequestMethod.POST, params = { "approve" })
public String approve(@RequestParam int id, @RequestParam String approve, Model model) {
    try {
        bulletinDAO.approveBulletin(id);
        model.addAttribute("approval", "Your bulletin has been approved.");

        List<Bulletin> bulletins = bulletinDAO.getApprovedBulletins();
        model.addAttribute("bulletins", bulletins);
    } catch (Exception e) {
        System.out.println(e.getMessage());
        return "FailurePage";
    }

    return "ApproveBulletin";
}

@RequestMapping(value = "/approve", method = RequestMethod.POST, params = { "deny" })
public String deny(@RequestParam int id, @RequestParam String deny, Model model) {
    try {
        bulletinDAO.denyBulletin(id);
        model.addAttribute("approval", "Your bulletin has been denied.");

        List<Bulletin> bulletins = bulletinDAO.getApprovedBulletins();
        model.addAttribute("bulletins", bulletins);
    } catch (Exception e) {
        System.out.println(e.getMessage());
        return "FailurePage";
    }

    return "ApproveBulletin";
}

关于spring-mvc - Spring 3-带有2个按钮的表单,向 Controller 方法发送2个参数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16514501/

10-12 22:46