My Jsp
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="spring" %> <spring:form method="post" modelAttribute="myObject"> <spring:input type="hidden" path="id"/> <div class="type-text"> <label for="myLabel">My Label</label> <input type="text" path="firstName"/> </div> .... // here We have Partial Form of myObject </spring:form>
Controller Class
@ModelAttribute("myObject") public MyObject beforBinding(@RequestParam(required=false) Long id){ if (id != null) { return myObjectDao.findById(id); } return null; } @RequestMapping(value = "/view", method = RequestMethod.GET) public void viewMyPersistenObject(ModelMap model) { model.addAttribute("myObject", myObjectDao.findById(123)); }; @RequestMapping(value = "/save", method = RequestMethod.POST) public void saveMyPersistenObject(@ModelAttribute("myObject") MyObject mergeObject) { // Here mergeObject is a merge between myObject return by beforeBinding method // and my Post Form myObjectDao.saveOrUpdate(mergeObject) };
Grâce à cet article, j’ai pu apprendre et mieux maîtriser mes objets. Merci.