Jiachang  Yang
Jiachang Yang

Reputation: 200

Spring MVC, two buttons work with one form, how they work together?

enter image description here

What I am doing here is when I enter a bookname in textbook and click edit button. The textboxes below will show this book's all information. Then I can edit information in the same textboxes. In the end, I click update and the new information will be updated.I have edit part done, I can edit all information by given certain bookname. Now I am confused how to do update part, because both edit and update will use the same form(those textboxes), then how to handle post or get request? Someone can explain to me? Thank you.

controller:

@RequestMapping(value = "/edit", method=RequestMethod.GET)
public String getEditBookForm(@ModelAttribute("editBook") Book editBook, HttpServletRequest request) {

        return "editbook";
    }

@RequestMapping(value = "/edit", method = RequestMethod.POST)
public ModelAndView processEditBookForm(@ModelAttribute("editBook") Book editBook ){

    ModelAndView mv = new ModelAndView("editbook");
    mv.addObject("resulteditBook",bookService.getBookByName(editBook.getBookName()) );
    return mv;
}

jsp:

<form:form modelAttribute="editBook" >

        <div>Enter BOOK NAME: <input type="text" name=bookName
                style="width: 100">

            <input type="submit" value="Edit" name="edit">
    </div>
    </form:form>

<form:form modelAttribute="updateBook" >
    ......
    <p>
        <input type="submit" value="Update" name="update">
    </p>
    </form:form>

Upvotes: 0

Views: 880

Answers (2)

Master Slave
Master Slave

Reputation: 28519

You can have something like

<input type="submit" class="button" name="edit" value="edit"/>
<input type="submit" class="button" name="update" value="update"/>

and distinguish your conroller methods based on the param e.g. for edit

@RequestMapping(value = "/edit", method = RequestMethod.POST, params = "edit")
public String edit() {
...  
}

and for update

@RequestMapping(value = "/edit", method = RequestMethod.POST, params = "update")
public String update() {
...  
}

Upvotes: 1

Thomas
Thomas

Reputation: 508

Try This way

jsp

<form:form commandName="editBook" method="post" action="edit.do" id="editForm" >

        <div>Enter BOOK NAME: <input type="text" name=bookName
                style="width: 100">

            <input type="submit" value="Edit" name="edit"   onClick="editFormsubmit();">">
    </div>
    </form:form>

<form:form commandName="updateBook" method="post" action="update.do" id="updateForm"  >
    ......
    <p>
        <input type="submit" value="Update" name="update" onClick="updateFormsubmit();">
    </p>
    </form:form>

javascript

<script type="text/javascript" language="JavaScript">
function updateFormsubmit() {
     document.getElementById("updateForm").submit(); 
}

function editFormsubmit() {
     document.getElementById("editForm").submit(); 
}
</script>

Controller

@RequestMapping(value = "/edit", method=RequestMethod.GET)
public String getEditBookForm(@ModelAttribute("editBook") Book editBook, HttpServletRequest request) {

        return "editbook";
    }

@RequestMapping(value = "/update", method = RequestMethod.POST)
public ModelAndView processEditBookForm(@ModelAttribute("updateBook") Book editBook ){
    ModelAndView mv = new ModelAndView("editbook");
    mv.addObject("resulteditBook",bookService.getBookByName(editBook.getBookName()) );
    return mv;
}

Upvotes: 0

Related Questions