Skip to content

Commit

Permalink
Going stateless
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Dubois committed Mar 1, 2013
1 parent b2d6283 commit 32b1457
Show file tree
Hide file tree
Showing 16 changed files with 46 additions and 17 deletions.
Expand Up @@ -45,4 +45,20 @@ public boolean isNew() {
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;
}
}
Expand Up @@ -23,7 +23,6 @@
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
Expand All @@ -36,7 +35,6 @@
* @author Michael Isvy
*/
@Controller
@SessionAttributes(types = Owner.class)
public class OwnerController {

private final ClinicService clinicService;
Expand All @@ -60,12 +58,11 @@ public String initCreationForm(Model model) {
}

@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()) {
return "owners/createOrUpdateOwnerForm";
} else {
this.clinicService.saveOwner(owner);
status.setComplete();
return "redirect:/owners/" + owner.getId();
}
}
Expand Down Expand Up @@ -110,12 +107,12 @@ public String initUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model mo
}

@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()) {
return "owners/createOrUpdateOwnerForm";
} else {
owner.setId(ownerId);
this.clinicService.saveOwner(owner);
status.setComplete();
return "redirect:/owners/{ownerId}";
}
}
Expand Down
Expand Up @@ -25,7 +25,6 @@
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;

import java.util.Collection;

Expand All @@ -35,7 +34,6 @@
* @author Arjen Poutsma
*/
@Controller
@SessionAttributes("pet")
public class PetController {

private final ClinicService clinicService;
Expand Down Expand Up @@ -66,13 +64,14 @@ public String initCreationForm(@PathVariable("ownerId") int ownerId, Model model
}

@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);
if (result.hasErrors()) {
return "pets/createOrUpdatePetForm";
} else {
this.clinicService.savePet(pet);
status.setComplete();
return "redirect:/owners/{ownerId}";
}
}
Expand All @@ -85,14 +84,20 @@ public String initUpdateForm(@PathVariable("petId") int petId, Model model) {
}

@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
new PetValidator().validate(pet, result);
if (result.hasErrors()) {
return "pets/createOrUpdatePetForm";
} else {
this.clinicService.savePet(pet);
status.setComplete();
return "redirect:/owners/{ownerId}";
}
}
Expand Down
Expand Up @@ -24,7 +24,6 @@
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
Expand All @@ -36,7 +35,6 @@
* @author Michael Isvy
*/
@Controller
@SessionAttributes("visit")
public class VisitController {

private final ClinicService clinicService;
Expand All @@ -62,12 +60,13 @@ public String initNewVisitForm(@PathVariable("petId") int petId, Model model) {
}

@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()) {
return "pets/createOrUpdateVisitForm";
} else {
this.clinicService.saveVisit(visit);
status.setComplete();
this.clinicService.saveVisit(visit);;
return "redirect:/owners/{ownerId}";
}
}
Expand Down
1 change: 1 addition & 0 deletions src/main/webapp/WEB-INF/jsp/exception.jsp 100644 → 100755
@@ -1,3 +1,4 @@
<%@page session="false" %>
<html lang="en">
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Expand Down
1 change: 1 addition & 0 deletions src/main/webapp/WEB-INF/jsp/fragments/bodyHeader.jsp 100644 → 100755
@@ -1,3 +1,4 @@
<%@page session="false" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

Expand Down
1 change: 1 addition & 0 deletions src/main/webapp/WEB-INF/jsp/fragments/footer.jsp 100644 → 100755
@@ -1,3 +1,4 @@
<%@page session="false" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>

<table class="footer">
Expand Down
1 change: 1 addition & 0 deletions src/main/webapp/WEB-INF/jsp/fragments/headTag.jsp 100644 → 100755
@@ -1,3 +1,4 @@
<%@page session="false" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>

<!--
Expand Down
@@ -1,3 +1,4 @@
<%@page session="false" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Expand Down
1 change: 1 addition & 0 deletions src/main/webapp/WEB-INF/jsp/owners/findOwners.jsp
@@ -1,3 +1,4 @@
<%@page session="false" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
Expand Down
1 change: 1 addition & 0 deletions src/main/webapp/WEB-INF/jsp/owners/ownerDetails.jsp
@@ -1,3 +1,4 @@
<%@page session="false" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
Expand Down
1 change: 1 addition & 0 deletions src/main/webapp/WEB-INF/jsp/owners/ownersList.jsp
@@ -1,3 +1,4 @@
<%@page session="false" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Expand Down
1 change: 1 addition & 0 deletions src/main/webapp/WEB-INF/jsp/pets/createOrUpdatePetForm.jsp
@@ -1,3 +1,4 @@
<%@page session="false" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
Expand Down
@@ -1,3 +1,4 @@
<%@page session="false" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
Expand Down
1 change: 1 addition & 0 deletions src/main/webapp/WEB-INF/jsp/vets/vetList.jsp
@@ -1,3 +1,4 @@
<%@page session="false" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Expand Down
1 change: 1 addition & 0 deletions src/main/webapp/WEB-INF/jsp/welcome.jsp 100644 → 100755
@@ -1,3 +1,4 @@
<%@page session="false" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

Expand Down

0 comments on commit 32b1457

Please sign in to comment.