문제

I have the following Spring 3.2 form. It has two buttons, and I want to perform a different action depending on which button is clicked.

<form:form action="approve" method="post">
    <%
        List<Bulletin> bulletins = (List<Bulletin>) request.getAttribute("bulletins");
        for (Bulletin bulletin : bulletins) {
            bulletin.setNote(bulletin.getNote().replace("\n\n", "\n "));
            String[] bulletinArray = bulletin.getNote().split("\n");
            out.println("<b>Name:</b> " + bulletin.getName() + "<br>");
            out.println("<b>Date:</b> " + bulletin.getDate() + "<br>");
            out.print("<b>Comment:</b> ");
            for (int i = 0; i < bulletinArray.length; i++) {
                if (i == bulletinArray.length - 1) {
                    out.println(bulletinArray[i]);
                } else {
                    out.println(bulletinArray[i] + "<br />");
                }
            }
            out.println("<br><br>");
        }
    %>
            <td><input type="submit" name="approve" value="Approve" /></td>
            <td><input type="submit" name="deny" value="Deny" /></td>
            <br />
        </form:form>

I have the following methods in my 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 "redirect:/waitingBulletins";
}

@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 "redirect:/waitingBulletins";
}

Whichever button I click on, I get an HTTP Status 400 error with the description "The request sent by the client was syntactically incorrect." As you can see, each method has a parameter of type int. It seems to me the problem is that I haven't accounted for that method in the form in my JSP, but I'm not clear about how to do that. Can anyone help?

도움이 되었습니까?

해결책

I don't know where the @RequestParam should come from. I think that's the crux of the problem.

Yes, absolutely. The @RequestParam javadoc states

Annotation which indicates that a method parameter should be bound to a web request parameter.

It can't do that if you don't have an appropriate request parameter. In your case, it doesn't seem like you have a request parameter for

@RequestParam int id

where the name of the request parameter will be id since you have provided a value attribute to @RequestParam. You need to provide such a request parameter, possibly with an <input> element.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top