Skip to content
Snippets Groups Projects
Commit 09013641 authored by Keith Donald's avatar Keith Donald Committed by Mic
Browse files

owner pages

parent d4fc5b03
No related branches found
No related tags found
No related merge requests found
Showing
with 189 additions and 36 deletions
......@@ -14,12 +14,57 @@ public class Owner {
private String telephone;
public Owner() {
}
public Owner(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
public String getName() {
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
}
package org.springframework.samples.petclinic.owners;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.samples.petclinic.util.ResponseContext;
import org.springframework.samples.petclinic.util.ExternalContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
......@@ -18,19 +20,20 @@ public class OwnerController {
}
@RequestMapping(method=RequestMethod.GET)
public Owner get(Long owner) {
return repository.getOwner(owner);
public String get(@PathVariable Long owner, Model model) {
model.addAttribute(repository.getOwner(owner));
return "owner";
}
@RequestMapping(value="/edit", method=RequestMethod.GET)
public Owner getEditForm(Long owner) {
public Owner getEditForm(@PathVariable Long owner) {
return repository.getOwner(owner);
}
@RequestMapping(method = RequestMethod.PUT)
public void put(Owner owner, ResponseContext response) {
public void put(Owner owner, ExternalContext response) {
repository.saveOwner(owner);
response.redirect(owner.getName());
response.redirect(owner.getId());
}
}
\ No newline at end of file
......@@ -8,6 +8,6 @@ public interface OwnerRepository {
Owner getOwner(Long id);
void saveOwner(Owner owner);
Long saveOwner(Owner owner);
}
\ No newline at end of file
package org.springframework.samples.petclinic.owners;
public class OwnerSearchForm {
private String lastName;
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
......@@ -3,7 +3,6 @@ package org.springframework.samples.petclinic.owners;
import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.samples.petclinic.util.ResponseContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
......@@ -21,8 +20,8 @@ public class OwnersController {
}
@RequestMapping(method = RequestMethod.GET)
public void get() {
public OwnerSearchForm get() {
return new OwnerSearchForm();
}
@RequestMapping(value="/search", method = RequestMethod.GET)
......@@ -36,9 +35,10 @@ public class OwnersController {
}
@RequestMapping(method = RequestMethod.POST)
public void post(Owner owner, ResponseContext response) {
repository.saveOwner(owner);
response.redirect(owner.getName());
public String post(Owner owner) {
Long ownerId = repository.saveOwner(owner);
// TODO simplify this since /owners is the current resource already?
return "redirect:/owners/" + ownerId;
}
}
......@@ -12,10 +12,11 @@ public class StubOwnerRepository implements OwnerRepository {
}
public Owner getOwner(Long id) {
return null;
return new Owner(id);
}
public void saveOwner(Owner owner) {
public Long saveOwner(Owner owner) {
return 1L;
}
}
package org.springframework.samples.petclinic.owners.pets;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.samples.petclinic.util.ResponseContext;
import org.springframework.samples.petclinic.util.ExternalContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
......@@ -28,13 +28,13 @@ public class PetController {
}
@RequestMapping(method = RequestMethod.PUT)
public void put(Pet pet, ResponseContext response) {
public void put(Pet pet, ExternalContext response) {
repository.savePet(pet);
response.redirect(pet.getName());
}
@RequestMapping(method = RequestMethod.DELETE)
public void delete(Long owner, String pet, ResponseContext context) {
public void delete(Long owner, String pet, ExternalContext context) {
context.forResource("owners").redirect(owner);
}
......
package org.springframework.samples.petclinic.util;
import org.springframework.ui.Model;
// This is an idea as a context object to make avail to @Controller methods to interact with Dispatcher
public interface ExternalContext {
Model getModel();
void selectView(String viewName);
void renderFragment(String fragment);
void redirect(Object resource);
ExternalContext forResource(Object resource);
Object getNativeRequest();
Object getNativeResponse();
}
package org.springframework.samples.petclinic.util;
public interface ResponseContext {
void redirect(Object resource);
ResponseContext forResource(Object resource);
}
......@@ -22,9 +22,9 @@
</ul>
<div id="nav">
<ul>
<li><a href="<c:url value="/"/>">Home</a></li>
<li><a href="appointments">Appointments</a></li>
<li><a href="owners">Owners</a></li>
<li><a href="${pageContext.request.contextPath}">Home</a></li>
<li><a href="${pageContext.request.contextPath}/appointments">Appointments</a></li>
<li><a href="${pageContext.request.contextPath}/owners">Owners</a></li>
</ul>
</div>
</div>
......
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<h2>Add New Owner</h2>
<form:form id="addNewForm" action="${pageContext.request.contextPath}/owners" modelAttribute="owner" method="post">
<form:label for="firstName" path="firstName">
First Name
<form:input path="firstName" />
</form:label>
<form:label for="lastName" path="lastName">
Last Name
<form:input path="lastName" />
</form:label>
<input type="submit" value="Add" />
</form:form>
\ No newline at end of file
<%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %>
<div id="sidebar">
<ul id="sub-nav">
<li><a href="${pageContext.request.contextPath}/owners">Search</a></li>
<li><a href="${pageContext.request.contextPath}/owners/new">Add New</a></li>
</ul>
</div>
<div id="main">
<tiles:insertAttribute name="main" />
</div>
\ No newline at end of file
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<h2>Search Owners</h2>
\ No newline at end of file
<h2>Owner Details</h2>
\ No newline at end of file
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<h2>Search Owners</h2>
<form:form id="searchForm" action="owners/search" modelAttribute="ownerSearchForm" method="get">
<form:label for="lastName" path="lastName">
Last Name
<form:input path="lastName" />
</form:label>
<input type="submit" value="Search" />
</form:form>
\ No newline at end of file
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<h2>Search Results</h2>
\ No newline at end of file
......@@ -18,14 +18,50 @@
<!-- APPOINTMENTS PAGES -->
<definition name="appointments" extends="page">
<put-attribute name="title" value="Appointment" type="string" />
<put-attribute name="title" value="Appointments" type="string" />
<put-attribute name="content" value="/WEB-INF/appointments/calendar.jsp" type="template" />
</definition>
<!-- OWNERS PAGES -->
<!-- SEARCH PAGE -->
<definition name="owners" extends="page">
<put-attribute name="title" value="Owners" type="string" />
<put-attribute name="content" value="owners.content" type="definition" />
</definition>
<definition name="owners.content" template="/WEB-INF/owners/content.jsp">
<put-attribute name="main" value="/WEB-INF/owners/searchForm.jsp" type="template" />
</definition>
<!-- ADD NEW PAGE -->
<definition name="owners/new" extends="page">
<put-attribute name="title" value="Owners" type="string" />
<put-attribute name="content" value="owners/new.content" type="definition" />
</definition>
<definition name="owners/new.content" template="/WEB-INF/owners/content.jsp">
<put-attribute name="main" value="/WEB-INF/owners/addNewForm.jsp" type="template" />
</definition>
<!-- SEARCH RESULTS PAGE -->
<definition name="owners/search" extends="page">
<put-attribute name="title" value="Owner Search" type="string" />
<put-attribute name="content" value="/WEB-INF/owners/search.jsp" type="template" />
</definition>
<put-attribute name="content" value="owners/search.content" type="definition" />
</definition>
<definition name="owners/search.content" template="/WEB-INF/owners/content.jsp">
<put-attribute name="main" value="/WEB-INF/owners/searchResults.jsp" type="template" />
</definition>
<!-- OWNER DETAIL PAGE -->
<definition name="owner" extends="page">
<put-attribute name="title" value="Owner" type="string" />
<put-attribute name="content" value="owner.content" type="definition" />
</definition>
<definition name="owner.content" template="/WEB-INF/owners/content.jsp">
<put-attribute name="main" value="/WEB-INF/owners/owner.jsp" type="template" />
</definition>
</tiles-definitions>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment