mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-04-24 11:22:48 +00:00
owner pages
This commit is contained in:
parent
d4fc5b036b
commit
090136418a
16 changed files with 190 additions and 37 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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
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());
|
||||
}
|
||||
|
||||
}
|
|
@ -8,6 +8,6 @@ public interface OwnerRepository {
|
|||
|
||||
Owner getOwner(Long id);
|
||||
|
||||
void saveOwner(Owner owner);
|
||||
Long saveOwner(Owner owner);
|
||||
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
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);
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
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();
|
||||
|
||||
}
|
|
@ -1,9 +0,0 @@
|
|||
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>
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
<%@ 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>
|
|
@ -0,0 +1,11 @@
|
|||
<%@ 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>
|
|
@ -1,2 +1,2 @@
|
|||
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
|
||||
<h2>Search Owners</h2>
|
||||
<h2>Owner Details</h2>
|
|
@ -0,0 +1,12 @@
|
|||
<%@ 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>
|
|
@ -0,0 +1,2 @@
|
|||
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
|
||||
<h2>Search Results</h2>
|
|
@ -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="Owner Search" type="string" />
|
||||
<put-attribute name="content" value="/WEB-INF/owners/search.jsp" type="template" />
|
||||
</definition>
|
||||
<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="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>
|
Loading…
Reference in a new issue