📄 addpetform.java
字号:
package org.springframework.samples.petclinic.web;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.samples.petclinic.Owner;
import org.springframework.samples.petclinic.Pet;
import org.springframework.samples.petclinic.PetType;
import org.springframework.samples.petclinic.util.EntityUtils;
import org.springframework.validation.BindException;
import org.springframework.web.bind.RequestUtils;
import org.springframework.web.servlet.ModelAndView;
/**
* JavaBean form controller that is used to add a new <code>Pet</code> to the system.
*
* @author Ken Krebs
*/
public class AddPetForm extends AbstractClinicForm {
public AddPetForm() {
// need a session to hold the formBackingObject
setSessionForm(true);
}
protected Map referenceData(HttpServletRequest request) throws ServletException {
Map refData = new HashMap();
refData.put("types", getClinic().getPetTypes());
return refData;
}
protected Object formBackingObject(HttpServletRequest request) throws ServletException {
Owner owner = getClinic().loadOwner(RequestUtils.getRequiredIntParameter(request, "ownerId"));
Pet pet = new Pet();
owner.addPet(pet);
return pet;
}
protected void onBindAndValidate(HttpServletRequest request, Object command, BindException errors) {
Pet pet = (Pet) command;
long typeId = Long.parseLong(request.getParameter("typeId"));
pet.setType((PetType) EntityUtils.getById(getClinic().getPetTypes(), PetType.class, typeId));
}
/** Method inserts a new <code>Pet</code>. */
protected ModelAndView onSubmit(Object command) throws ServletException {
Pet pet = (Pet) command;
// delegate the insert to the Business layer
getClinic().storePet(pet);
return new ModelAndView(getSuccessView(), "ownerId", Long.toString(pet.getOwner().getId()));
}
protected ModelAndView handleInvalidSubmit(HttpServletRequest request, HttpServletResponse response)
throws Exception {
return disallowDuplicateFormSubmission(request, response);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -