Going stateless

This commit is contained in:
Julien Dubois 2013-03-01 16:19:49 +01:00
parent b2d628354f
commit 32b14575c0
16 changed files with 46 additions and 17 deletions

View file

@ -45,4 +45,20 @@ public class BaseEntity {
return (this.id == null); return (this.id == null);
} }
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
BaseEntity that = (BaseEntity) o;
if (id != null ? !id.equals(that.id) : that.id != null) return false;
return true;
}
@Override
public int hashCode() {
return id != null ? id.hashCode() : 0;
}
} }

View file

@ -23,7 +23,6 @@ import org.springframework.ui.Model;
import org.springframework.validation.BindingResult; import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.ModelAndView;
import javax.validation.Valid; import javax.validation.Valid;
@ -36,7 +35,6 @@ import java.util.Collection;
* @author Michael Isvy * @author Michael Isvy
*/ */
@Controller @Controller
@SessionAttributes(types = Owner.class)
public class OwnerController { public class OwnerController {
private final ClinicService clinicService; private final ClinicService clinicService;
@ -60,12 +58,11 @@ public class OwnerController {
} }
@RequestMapping(value = "/owners/new", method = RequestMethod.POST) @RequestMapping(value = "/owners/new", method = RequestMethod.POST)
public String processCreationForm(@Valid Owner owner, BindingResult result, SessionStatus status) { public String processCreationForm(@Valid Owner owner, BindingResult result) {
if (result.hasErrors()) { if (result.hasErrors()) {
return "owners/createOrUpdateOwnerForm"; return "owners/createOrUpdateOwnerForm";
} else { } else {
this.clinicService.saveOwner(owner); this.clinicService.saveOwner(owner);
status.setComplete();
return "redirect:/owners/" + owner.getId(); return "redirect:/owners/" + owner.getId();
} }
} }
@ -110,12 +107,12 @@ public class OwnerController {
} }
@RequestMapping(value = "/owners/{ownerId}/edit", method = RequestMethod.PUT) @RequestMapping(value = "/owners/{ownerId}/edit", method = RequestMethod.PUT)
public String processUpdateOwnerForm(@Valid Owner owner, BindingResult result, SessionStatus status) { public String processUpdateOwnerForm(@PathVariable("ownerId") int ownerId, @Valid Owner owner, BindingResult result) {
if (result.hasErrors()) { if (result.hasErrors()) {
return "owners/createOrUpdateOwnerForm"; return "owners/createOrUpdateOwnerForm";
} else { } else {
owner.setId(ownerId);
this.clinicService.saveOwner(owner); this.clinicService.saveOwner(owner);
status.setComplete();
return "redirect:/owners/{ownerId}"; return "redirect:/owners/{ownerId}";
} }
} }

View file

@ -25,7 +25,6 @@ import org.springframework.ui.Model;
import org.springframework.validation.BindingResult; import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;
import java.util.Collection; import java.util.Collection;
@ -35,7 +34,6 @@ import java.util.Collection;
* @author Arjen Poutsma * @author Arjen Poutsma
*/ */
@Controller @Controller
@SessionAttributes("pet")
public class PetController { public class PetController {
private final ClinicService clinicService; private final ClinicService clinicService;
@ -66,13 +64,14 @@ public class PetController {
} }
@RequestMapping(value = "/owners/{ownerId}/pets/new", method = RequestMethod.POST) @RequestMapping(value = "/owners/{ownerId}/pets/new", method = RequestMethod.POST)
public String processCreationForm(@ModelAttribute("pet") Pet pet, BindingResult result, SessionStatus status) { public String processCreationForm(@PathVariable("ownerId") int ownerId, @ModelAttribute("pet") Pet pet, BindingResult result) {
Owner owner = this.clinicService.findOwnerById(ownerId);
owner.addPet(pet);
new PetValidator().validate(pet, result); new PetValidator().validate(pet, result);
if (result.hasErrors()) { if (result.hasErrors()) {
return "pets/createOrUpdatePetForm"; return "pets/createOrUpdatePetForm";
} else { } else {
this.clinicService.savePet(pet); this.clinicService.savePet(pet);
status.setComplete();
return "redirect:/owners/{ownerId}"; return "redirect:/owners/{ownerId}";
} }
} }
@ -85,14 +84,20 @@ public class PetController {
} }
@RequestMapping(value = "/owners/{ownerId}/pets/{petId}/edit", method = {RequestMethod.PUT, RequestMethod.POST}) @RequestMapping(value = "/owners/{ownerId}/pets/{petId}/edit", method = {RequestMethod.PUT, RequestMethod.POST})
public String processUpdateForm(@ModelAttribute("pet") Pet pet, BindingResult result, SessionStatus status) { public String processUpdateForm(@PathVariable("ownerId") int ownerId,
@PathVariable("petId") int petId,
@ModelAttribute("pet") Pet pet,
BindingResult result) {
pet.setId(petId);
Owner owner = this.clinicService.findOwnerById(ownerId);
owner.addPet(pet);
// we're not using @Valid annotation here because it is easier to define such validation rule in Java // we're not using @Valid annotation here because it is easier to define such validation rule in Java
new PetValidator().validate(pet, result); new PetValidator().validate(pet, result);
if (result.hasErrors()) { if (result.hasErrors()) {
return "pets/createOrUpdatePetForm"; return "pets/createOrUpdatePetForm";
} else { } else {
this.clinicService.savePet(pet); this.clinicService.savePet(pet);
status.setComplete();
return "redirect:/owners/{ownerId}"; return "redirect:/owners/{ownerId}";
} }
} }

View file

@ -24,7 +24,6 @@ import org.springframework.ui.Model;
import org.springframework.validation.BindingResult; import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.ModelAndView;
import javax.validation.Valid; import javax.validation.Valid;
@ -36,7 +35,6 @@ import javax.validation.Valid;
* @author Michael Isvy * @author Michael Isvy
*/ */
@Controller @Controller
@SessionAttributes("visit")
public class VisitController { public class VisitController {
private final ClinicService clinicService; private final ClinicService clinicService;
@ -62,12 +60,13 @@ public class VisitController {
} }
@RequestMapping(value = "/owners/{ownerId}/pets/{petId}/visits/new", method = RequestMethod.POST) @RequestMapping(value = "/owners/{ownerId}/pets/{petId}/visits/new", method = RequestMethod.POST)
public String processNewVisitForm(@Valid Visit visit, BindingResult result, SessionStatus status) { public String processNewVisitForm(@PathVariable("petId") int petId, @Valid Visit visit, BindingResult result) {
Pet pet = this.clinicService.findPetById(petId);
visit.setPet(pet);
if (result.hasErrors()) { if (result.hasErrors()) {
return "pets/createOrUpdateVisitForm"; return "pets/createOrUpdateVisitForm";
} else { } else {
this.clinicService.saveVisit(visit); this.clinicService.saveVisit(visit);;
status.setComplete();
return "redirect:/owners/{ownerId}"; return "redirect:/owners/{ownerId}";
} }
} }

1
src/main/webapp/WEB-INF/jsp/exception.jsp Normal file → Executable file
View file

@ -1,3 +1,4 @@
<%@page session="false" %>
<html lang="en"> <html lang="en">
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

1
src/main/webapp/WEB-INF/jsp/fragments/bodyHeader.jsp Normal file → Executable file
View file

@ -1,3 +1,4 @@
<%@page session="false" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

1
src/main/webapp/WEB-INF/jsp/fragments/footer.jsp Normal file → Executable file
View file

@ -1,3 +1,4 @@
<%@page session="false" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<table class="footer"> <table class="footer">

1
src/main/webapp/WEB-INF/jsp/fragments/headTag.jsp Normal file → Executable file
View file

@ -1,3 +1,4 @@
<%@page session="false" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<!-- <!--

View file

@ -1,3 +1,4 @@
<%@page session="false" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

View file

@ -1,3 +1,4 @@
<%@page session="false" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

View file

@ -1,3 +1,4 @@
<%@page session="false" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

View file

@ -1,3 +1,4 @@
<%@page session="false" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

View file

@ -1,3 +1,4 @@
<%@page session="false" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

View file

@ -1,3 +1,4 @@
<%@page session="false" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

View file

@ -1,3 +1,4 @@
<%@page session="false" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

1
src/main/webapp/WEB-INF/jsp/welcome.jsp Normal file → Executable file
View file

@ -1,3 +1,4 @@
<%@page session="false" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>