1 package org.springframework.samples.petclinic.web; 2 3 import java.util.HashMap ; 4 import java.util.Map ; 5 6 import javax.servlet.ServletException ; 7 import javax.servlet.http.HttpServletRequest ; 8 9 import org.springframework.samples.petclinic.Pet; 10 import org.springframework.samples.petclinic.PetType; 11 import org.springframework.samples.petclinic.util.EntityUtils; 12 import org.springframework.web.bind.RequestUtils; 13 import org.springframework.web.servlet.ModelAndView; 14 15 20 public class EditPetForm extends AbstractClinicForm { 21 22 public EditPetForm() { 23 setSessionForm(true); 25 setBindOnNewForm(true); 27 } 28 29 protected Map referenceData(HttpServletRequest request) throws ServletException { 30 Map refData = new HashMap (); 31 refData.put("types", getClinic().getPetTypes()); 32 return refData; 33 } 34 35 protected Object formBackingObject(HttpServletRequest request) throws ServletException { 36 return getClinic().loadPet(RequestUtils.getRequiredIntParameter(request, "petId")); 38 } 39 40 protected void onBind(HttpServletRequest request, Object command) throws ServletException { 41 Pet pet = (Pet) command; 42 int typeId = RequestUtils.getRequiredIntParameter(request, "typeId"); 43 pet.setType((PetType) EntityUtils.getById(getClinic().getPetTypes(), PetType.class, typeId)); 44 } 45 46 47 protected ModelAndView onSubmit(Object command) throws ServletException { 48 Pet pet = (Pet) command; 49 getClinic().storePet(pet); 51 return new ModelAndView(getSuccessView(), "ownerId", pet.getOwner().getId()); 52 } 53 54 } 55 | Popular Tags |