List

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)
};

One Response to “Spring MVC – How to load persitent object before binding”

  1. BAS Apside

    Grâce à cet article, j’ai pu apprendre et mieux maîtriser mes objets. Merci.

Répondre à BAS Apside Annuler la réponse.

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *