Last active
February 29, 2016 21:56
-
-
Save philwebb/2122299178d698db6773 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Controller | |
@SessionAttributes(types = Owner.class) | |
public class OwnerController { | |
private final ClinicService clinicService; | |
@Autowired | |
public OwnerController(ClinicService clinicService) { | |
this.clinicService = clinicService; | |
} | |
@InitBinder | |
public void setAllowedFields(WebDataBinder dataBinder) { | |
dataBinder.setDisallowedFields("id"); | |
} | |
@Get("/owners/new") | |
public String initCreationForm(Map<String, Object> model) { | |
Owner owner = new Owner(); | |
model.put("owner", owner); | |
return "owners/createOrUpdateOwnerForm"; | |
} | |
@Post("/owners/new") | |
public String processCreationForm(@Valid Owner owner, BindingResult result, | |
SessionStatus status) { | |
if (result.hasErrors()) { | |
return "owners/createOrUpdateOwnerForm"; | |
} | |
else { | |
this.clinicService.saveOwner(owner); | |
status.setComplete(); | |
return "redirect:/owners/" + owner.getId(); | |
} | |
} | |
@Get("/owners/find") | |
public String initFindForm(Map<String, Object> model) { | |
model.put("owner", new Owner()); | |
return "owners/findOwners"; | |
} | |
@Get("/owners") | |
public String processFindForm(Owner owner, BindingResult result, | |
Map<String, Object> model) { | |
// allow parameterless GET request for /owners to return all records | |
if (owner.getLastName() == null) { | |
owner.setLastName(""); // empty string signifies broadest possible search | |
} | |
// find owners by last name | |
Collection<Owner> results = this.clinicService.findOwnerByLastName(owner | |
.getLastName()); | |
if (results.size() < 1) { | |
// no owners found | |
result.rejectValue("lastName", "notFound", "not found"); | |
return "owners/findOwners"; | |
} | |
if (results.size() > 1) { | |
// multiple owners found | |
model.put("selections", results); | |
return "owners/ownersList"; | |
} | |
else { | |
// 1 owner found | |
owner = results.iterator().next(); | |
return "redirect:/owners/" + owner.getId(); | |
} | |
} | |
@Get("/owners/{ownerId}/edit") | |
public String initUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model model) { | |
Owner owner = this.clinicService.findOwnerById(ownerId); | |
model.addAttribute(owner); | |
return "owners/createOrUpdateOwnerForm"; | |
} | |
@Put("/owners/{ownerId}/edit") | |
public String processUpdateOwnerForm(@Valid Owner owner, BindingResult result, | |
SessionStatus status) { | |
if (result.hasErrors()) { | |
return "owners/createOrUpdateOwnerForm"; | |
} | |
else { | |
this.clinicService.saveOwner(owner); | |
status.setComplete(); | |
return "redirect:/owners/{ownerId}"; | |
} | |
} | |
/** | |
* Custom handler for displaying an owner. | |
* | |
* @param ownerId the ID of the owner to display | |
* @return a ModelMap with the model attributes for the view | |
*/ | |
@RequestMapping("/owners/{ownerId}") | |
public ModelAndView showOwner(@PathVariable("ownerId") int ownerId) { | |
ModelAndView mav = new ModelAndView("owners/ownerDetails"); | |
mav.addObject(this.clinicService.findOwnerById(ownerId)); | |
return mav; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Controller | |
@SessionAttributes(types = Owner.class) | |
public class OwnerController { | |
private final ClinicService clinicService; | |
@Autowired | |
public OwnerController(ClinicService clinicService) { | |
this.clinicService = clinicService; | |
} | |
@InitBinder | |
public void setAllowedFields(WebDataBinder dataBinder) { | |
dataBinder.setDisallowedFields("id"); | |
} | |
@GetRequestMapping("/owners/new") | |
public String initCreationForm(Map<String, Object> model) { | |
Owner owner = new Owner(); | |
model.put("owner", owner); | |
return "owners/createOrUpdateOwnerForm"; | |
} | |
@PostRequestMapping("/owners/new") | |
public String processCreationForm(@Valid Owner owner, BindingResult result, | |
SessionStatus status) { | |
if (result.hasErrors()) { | |
return "owners/createOrUpdateOwnerForm"; | |
} | |
else { | |
this.clinicService.saveOwner(owner); | |
status.setComplete(); | |
return "redirect:/owners/" + owner.getId(); | |
} | |
} | |
@GetRequestMapping("/owners/find") | |
public String initFindForm(Map<String, Object> model) { | |
model.put("owner", new Owner()); | |
return "owners/findOwners"; | |
} | |
@GetRequestMapping("/owners") | |
public String processFindForm(Owner owner, BindingResult result, | |
Map<String, Object> model) { | |
// allow parameterless GET request for /owners to return all records | |
if (owner.getLastName() == null) { | |
owner.setLastName(""); // empty string signifies broadest possible search | |
} | |
// find owners by last name | |
Collection<Owner> results = this.clinicService.findOwnerByLastName(owner | |
.getLastName()); | |
if (results.size() < 1) { | |
// no owners found | |
result.rejectValue("lastName", "notFound", "not found"); | |
return "owners/findOwners"; | |
} | |
if (results.size() > 1) { | |
// multiple owners found | |
model.put("selections", results); | |
return "owners/ownersList"; | |
} | |
else { | |
// 1 owner found | |
owner = results.iterator().next(); | |
return "redirect:/owners/" + owner.getId(); | |
} | |
} | |
@GetRequestMapping("/owners/{ownerId}/edit") | |
public String initUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model model) { | |
Owner owner = this.clinicService.findOwnerById(ownerId); | |
model.addAttribute(owner); | |
return "owners/createOrUpdateOwnerForm"; | |
} | |
@PutRequestMapping("/owners/{ownerId}/edit") | |
public String processUpdateOwnerForm(@Valid Owner owner, BindingResult result, | |
SessionStatus status) { | |
if (result.hasErrors()) { | |
return "owners/createOrUpdateOwnerForm"; | |
} | |
else { | |
this.clinicService.saveOwner(owner); | |
status.setComplete(); | |
return "redirect:/owners/{ownerId}"; | |
} | |
} | |
/** | |
* Custom handler for displaying an owner. | |
* | |
* @param ownerId the ID of the owner to display | |
* @return a ModelMap with the model attributes for the view | |
*/ | |
@RequestMapping("/owners/{ownerId}") | |
public ModelAndView showOwner(@PathVariable("ownerId") int ownerId) { | |
ModelAndView mav = new ModelAndView("owners/ownerDetails"); | |
mav.addObject(this.clinicService.findOwnerById(ownerId)); | |
return mav; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.springframework.samples.petclinic.web; | |
import java.util.Collection; | |
import java.util.Map; | |
import javax.validation.Valid; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.samples.petclinic.model.Owner; | |
import org.springframework.samples.petclinic.service.ClinicService; | |
import org.springframework.stereotype.Controller; | |
import org.springframework.ui.Model; | |
import org.springframework.validation.BindingResult; | |
import org.springframework.web.bind.WebDataBinder; | |
import org.springframework.web.bind.annotation.InitBinder; | |
import org.springframework.web.bind.annotation.PathVariable; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RequestMethod; | |
import org.springframework.web.bind.annotation.SessionAttributes; | |
import org.springframework.web.bind.support.SessionStatus; | |
import org.springframework.web.servlet.ModelAndView; | |
/** | |
* @author Juergen Hoeller | |
* @author Ken Krebs | |
* @author Arjen Poutsma | |
* @author Michael Isvy | |
*/ | |
@Controller | |
@SessionAttributes(types = Owner.class) | |
public class OwnerController { | |
private final ClinicService clinicService; | |
@Autowired | |
public OwnerController(ClinicService clinicService) { | |
this.clinicService = clinicService; | |
} | |
@InitBinder | |
public void setAllowedFields(WebDataBinder dataBinder) { | |
dataBinder.setDisallowedFields("id"); | |
} | |
@RequestMapping(method = RequestMethod.GET, path = "/owners/new") | |
public String initCreationForm(Map<String, Object> model) { | |
Owner owner = new Owner(); | |
model.put("owner", owner); | |
return "owners/createOrUpdateOwnerForm"; | |
} | |
@RequestMapping(method = RequestMethod.POST, path = "/owners/new") | |
public String processCreationForm(@Valid Owner owner, BindingResult result, | |
SessionStatus status) { | |
if (result.hasErrors()) { | |
return "owners/createOrUpdateOwnerForm"; | |
} | |
else { | |
this.clinicService.saveOwner(owner); | |
status.setComplete(); | |
return "redirect:/owners/" + owner.getId(); | |
} | |
} | |
@RequestMapping(method = RequestMethod.GET, path = "/owners/find") | |
public String initFindForm(Map<String, Object> model) { | |
model.put("owner", new Owner()); | |
return "owners/findOwners"; | |
} | |
@RequestMapping(method = RequestMethod.GET, path = "/owners") | |
public String processFindForm(Owner owner, BindingResult result, | |
Map<String, Object> model) { | |
// allow parameterless GET request for /owners to return all records | |
if (owner.getLastName() == null) { | |
owner.setLastName(""); // empty string signifies broadest possible search | |
} | |
// find owners by last name | |
Collection<Owner> results = this.clinicService.findOwnerByLastName(owner | |
.getLastName()); | |
if (results.size() < 1) { | |
// no owners found | |
result.rejectValue("lastName", "notFound", "not found"); | |
return "owners/findOwners"; | |
} | |
if (results.size() > 1) { | |
// multiple owners found | |
model.put("selections", results); | |
return "owners/ownersList"; | |
} | |
else { | |
// 1 owner found | |
owner = results.iterator().next(); | |
return "redirect:/owners/" + owner.getId(); | |
} | |
} | |
@RequestMapping(method = RequestMethod.GET, path = "/owners/{ownerId}/edit") | |
public String initUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model model) { | |
Owner owner = this.clinicService.findOwnerById(ownerId); | |
model.addAttribute(owner); | |
return "owners/createOrUpdateOwnerForm"; | |
} | |
@RequestMapping(method = RequestMethod.PUT, path = "/owners/{ownerId}/edit") | |
public String processUpdateOwnerForm(@Valid Owner owner, BindingResult result, | |
SessionStatus status) { | |
if (result.hasErrors()) { | |
return "owners/createOrUpdateOwnerForm"; | |
} | |
else { | |
this.clinicService.saveOwner(owner); | |
status.setComplete(); | |
return "redirect:/owners/{ownerId}"; | |
} | |
} | |
/** | |
* Custom handler for displaying an owner. | |
* | |
* @param ownerId the ID of the owner to display | |
* @return a ModelMap with the model attributes for the view | |
*/ | |
@RequestMapping("/owners/{ownerId}") | |
public ModelAndView showOwner(@PathVariable("ownerId") int ownerId) { | |
ModelAndView mav = new ModelAndView("owners/ownerDetails"); | |
mav.addObject(this.clinicService.findOwnerById(ownerId)); | |
return mav; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Controller | |
@SessionAttributes(types = Owner.class) | |
public class OwnerController { | |
private final ClinicService clinicService; | |
@Autowired | |
public OwnerController(ClinicService clinicService) { | |
this.clinicService = clinicService; | |
} | |
@InitBinder | |
public void setAllowedFields(WebDataBinder dataBinder) { | |
dataBinder.setDisallowedFields("id"); | |
} | |
@GetMapping("/owners/new") | |
public String initCreationForm(Map<String, Object> model) { | |
Owner owner = new Owner(); | |
model.put("owner", owner); | |
return "owners/createOrUpdateOwnerForm"; | |
} | |
@PostMapping("/owners/new") | |
public String processCreationForm(@Valid Owner owner, BindingResult result, | |
SessionStatus status) { | |
if (result.hasErrors()) { | |
return "owners/createOrUpdateOwnerForm"; | |
} | |
else { | |
this.clinicService.saveOwner(owner); | |
status.setComplete(); | |
return "redirect:/owners/" + owner.getId(); | |
} | |
} | |
@GetMapping("/owners/find") | |
public String initFindForm(Map<String, Object> model) { | |
model.put("owner", new Owner()); | |
return "owners/findOwners"; | |
} | |
@GetMapping("/owners") | |
public String processFindForm(Owner owner, BindingResult result, | |
Map<String, Object> model) { | |
// allow parameterless GET request for /owners to return all records | |
if (owner.getLastName() == null) { | |
owner.setLastName(""); // empty string signifies broadest possible search | |
} | |
// find owners by last name | |
Collection<Owner> results = this.clinicService.findOwnerByLastName(owner | |
.getLastName()); | |
if (results.size() < 1) { | |
// no owners found | |
result.rejectValue("lastName", "notFound", "not found"); | |
return "owners/findOwners"; | |
} | |
if (results.size() > 1) { | |
// multiple owners found | |
model.put("selections", results); | |
return "owners/ownersList"; | |
} | |
else { | |
// 1 owner found | |
owner = results.iterator().next(); | |
return "redirect:/owners/" + owner.getId(); | |
} | |
} | |
@GetMapping("/owners/{ownerId}/edit") | |
public String initUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model model) { | |
Owner owner = this.clinicService.findOwnerById(ownerId); | |
model.addAttribute(owner); | |
return "owners/createOrUpdateOwnerForm"; | |
} | |
@PutMapping("/owners/{ownerId}/edit") | |
public String processUpdateOwnerForm(@Valid Owner owner, BindingResult result, | |
SessionStatus status) { | |
if (result.hasErrors()) { | |
return "owners/createOrUpdateOwnerForm"; | |
} | |
else { | |
this.clinicService.saveOwner(owner); | |
status.setComplete(); | |
return "redirect:/owners/{ownerId}"; | |
} | |
} | |
/** | |
* Custom handler for displaying an owner. | |
* | |
* @param ownerId the ID of the owner to display | |
* @return a ModelMap with the model attributes for the view | |
*/ | |
@RequestMapping("/owners/{ownerId}") | |
public ModelAndView showOwner(@PathVariable("ownerId") int ownerId) { | |
ModelAndView mav = new ModelAndView("owners/ownerDetails"); | |
mav.addObject(this.clinicService.findOwnerById(ownerId)); | |
return mav; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment