user771912
user771912

Reputation: 411

form with multiple submit buttons that execute different actions

I'm missing something fundamental when it comes to mapping a view to a controller's action and hoping someone can point me in the right direction. I'm working on an existing project and still familiarizing myself with the language and the way it was configured. I have a form that will resolve a qaCase (question answer case) through the resolveForm action and qaCase/resolve view. below is a simplified version of what I have (please let me know if I need to include more information).

QaCaseController

@RequestMapping(value="/resolve/{id}/**", method=RequestMethod.GET)
public String resolveForm(@PathVariable("id") Integer id, Model model) {
    QaCase qaCase = qaCaseDAO.findById(id);

    // Load the backing objects into the session
    model.addAttribute("qaCase", qaCase);       
    model.addAttribute("users", userDAO.findAll());
    model.addAttribute("exams", examDAO.findAll());

    return "qacases/resolve";
}

qaCase/resolve.jsp

the resolve view has a form that will accept text input and a resolve button.

<sf:form method="POST" modelAttribute="qaCase" onsubmit="return isValid()">
    // some input fields
    <input type="submit" name="submitted"  value="resolve" />
</sf:form>

when submit button is clicked, the following query string is created

http://localhost:8080/qacases/resolve/<id>/<location>/<name>/<created by>

What I'd like to do is add an additional input field and button to the existing form so I can optionally add comments instead of resolving a case.

<sf:form method="POST" modelAttribute="qaCase" onsubmit="return isValid()">
    // some input fields
    <input type="submit" name="submitted"  value="resolve" />
</sf:form>
<sf:form method="POST" modelAttribute="qaCase" action="addComment">
    // optionally Add comment
    <input type="submit" name="submitted" value="addComment" />
</sf:form>

If addComment is clicked then I want the query string to be created.

http://localhost:8080/qacases/addComment

Instead, I get the following query string with a 400 status code.

http://localhost:8080/qacases/resolve/<id>/<location>/<name>/<created by>/addComment

I've been going through configuration files to find how the mapping is being set but haven't had any luck. Not sure if this is an answer that can be answered without someone going through the project and determining how it's configured. Appreciate any advice and/or answers.

Upvotes: 0

Views: 309

Answers (1)

InsFi
InsFi

Reputation: 1318

when you are using action = "addComment" without "/" before "addComment" in <form>
that means you are posting your form to current_url_that_invokes_view/addComment
if add "/" to action = "/addComment" you will go to localhost:8080/addComment
so if you need http://localhost:8080/qacases/addComment
type action = "/qacases/addComment" and pay attantion to "/" before qacases to direct root url

Upvotes: 1

Related Questions