mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-18 05:25:50 +00:00
ownerForm and validation now working
This commit is contained in:
parent
f1508666ec
commit
41ec22b4be
6 changed files with 167 additions and 96 deletions
|
@ -0,0 +1,70 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2002-2013 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.springframework.samples.petclinic.web;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import javax.validation.Valid;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.samples.petclinic.model.Owner;
|
||||||
|
import org.springframework.samples.petclinic.service.ClinicService;
|
||||||
|
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.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Juergen Hoeller
|
||||||
|
* @author Ken Krebs
|
||||||
|
* @author Arjen Poutsma
|
||||||
|
* @author Michael Isvy
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
public class OwnerListResource {
|
||||||
|
|
||||||
|
private final ClinicService clinicService;
|
||||||
|
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public OwnerListResource(ClinicService clinicService) {
|
||||||
|
this.clinicService = clinicService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(value = "/owner/list", method = RequestMethod.GET,
|
||||||
|
produces = MediaType.APPLICATION_JSON_VALUE)
|
||||||
|
public Collection<Owner> findOwnerCollection(@RequestParam("lastName") String ownerLastName) {
|
||||||
|
|
||||||
|
if (ownerLastName == null) {
|
||||||
|
ownerLastName = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
Collection<Owner> results = this.clinicService.findOwnerByLastName(ownerLastName);
|
||||||
|
if (results.isEmpty()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -16,7 +16,6 @@
|
||||||
package org.springframework.samples.petclinic.web;
|
package org.springframework.samples.petclinic.web;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
|
|
||||||
|
@ -24,17 +23,16 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.samples.petclinic.model.Owner;
|
import org.springframework.samples.petclinic.model.Owner;
|
||||||
import org.springframework.samples.petclinic.service.ClinicService;
|
import org.springframework.samples.petclinic.service.ClinicService;
|
||||||
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.InitBinder;
|
import org.springframework.web.bind.annotation.InitBinder;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import org.springframework.web.bind.support.SessionStatus;
|
|
||||||
import org.springframework.web.servlet.ModelAndView;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
|
@ -58,69 +56,30 @@ public class OwnerResource {
|
||||||
dataBinder.setDisallowedFields("id");
|
dataBinder.setDisallowedFields("id");
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(value = "/owner/new", method = RequestMethod.GET)
|
@ModelAttribute
|
||||||
public String initCreationForm(Map<String, Object> model) {
|
public Owner retrieveOwner(@PathVariable("ownerId") int ownerId) {
|
||||||
Owner owner = new Owner();
|
|
||||||
model.put("owner", owner);
|
|
||||||
return "owner/createOrUpdateOwnerForm";
|
|
||||||
}
|
|
||||||
|
|
||||||
@RequestMapping(value = "/owner/new", method = RequestMethod.POST)
|
|
||||||
public String processCreationForm(@Valid Owner owner, BindingResult result, SessionStatus status) {
|
|
||||||
if (result.hasErrors()) {
|
|
||||||
return "owner/createOrUpdateOwnerForm";
|
|
||||||
} else {
|
|
||||||
this.clinicService.saveOwner(owner);
|
|
||||||
status.setComplete();
|
|
||||||
return "redirect:/owner/" + owner.getId();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@RequestMapping(value = "/owner/list", method = RequestMethod.GET,
|
|
||||||
produces = MediaType.APPLICATION_JSON_VALUE)
|
|
||||||
public Collection<Owner> findOwners(@RequestParam("lastName") String ownerLastName) {
|
|
||||||
|
|
||||||
if (ownerLastName == null) {
|
|
||||||
ownerLastName = "";
|
|
||||||
}
|
|
||||||
|
|
||||||
Collection<Owner> results = this.clinicService.findOwnerByLastName(ownerLastName);
|
|
||||||
if (results.isEmpty()) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return results;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@RequestMapping(value = "/owner/{ownerId}/edit", method = RequestMethod.GET)
|
|
||||||
public String initUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model model) {
|
|
||||||
Owner owner = this.clinicService.findOwnerById(ownerId);
|
|
||||||
model.addAttribute(owner);
|
|
||||||
return "owner/createOrUpdateOwnerForm";
|
|
||||||
}
|
|
||||||
|
|
||||||
@RequestMapping(value = "/owner/{ownerId}/edit", method = RequestMethod.PUT)
|
|
||||||
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:/owner/{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(value = "/owner/{ownerId}", method = RequestMethod.GET,
|
|
||||||
produces = MediaType.APPLICATION_JSON_VALUE)
|
|
||||||
public Owner showOwner(@PathVariable("ownerId") int ownerId) {
|
|
||||||
return this.clinicService.findOwnerById(ownerId);
|
return this.clinicService.findOwnerById(ownerId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: should be improved so we have a single method parameter
|
||||||
|
@RequestMapping(value = "/owner/{ownerId}", method = RequestMethod.POST)
|
||||||
|
public Owner updateOwner(@ModelAttribute Owner ownerModel, @RequestBody Owner ownerRequest) {
|
||||||
|
ownerModel.setFirstName(ownerRequest.getFirstName());
|
||||||
|
ownerModel.setLastName(ownerRequest.getLastName());
|
||||||
|
ownerModel.setCity(ownerRequest.getCity());
|
||||||
|
ownerModel.setAddress(ownerRequest.getAddress());
|
||||||
|
ownerModel.setTelephone(ownerRequest.getTelephone());
|
||||||
|
this.clinicService.saveOwner(ownerModel);
|
||||||
|
return ownerModel;
|
||||||
|
// TODO: need to handle failure
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@RequestMapping(value = "/owner/{ownerId}", method = RequestMethod.GET,
|
||||||
|
produces = MediaType.APPLICATION_JSON_VALUE)
|
||||||
|
public Owner findOwner(Owner owner) {
|
||||||
|
// Has already been retrieved from 'retrieveOwner' method
|
||||||
|
return owner;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,7 +33,7 @@ petClinicApp.config(['$routeProvider',
|
||||||
}).
|
}).
|
||||||
when('/owner/:id/edit', {
|
when('/owner/:id/edit', {
|
||||||
templateUrl: 'scripts/app/owner/ownerForm.html',
|
templateUrl: 'scripts/app/owner/ownerForm.html',
|
||||||
controller: 'ownerDetailController'
|
controller: 'ownerFormController'
|
||||||
}).
|
}).
|
||||||
when('/vets', {
|
when('/vets', {
|
||||||
templateUrl: 'scripts/app/vet/vetList.html',
|
templateUrl: 'scripts/app/vet/vetList.html',
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
angular.module('controllers').controller('ownerSearchController', ['$scope', '$rootScope', '$resource', '$location',
|
angular.module('controllers').controller('ownerSearchController', ['$scope', '$rootScope', '$resource', '$location',
|
||||||
function($scope, $rootScope, $resource, $location) {
|
function($scope, $rootScope, $resource, $location) {
|
||||||
|
|
||||||
$scope.submitOwnerFindForm = function(item, event) {
|
$scope.submitOwnerFindForm = function() {
|
||||||
|
|
||||||
var destUrl = '/petclinic/owner/list?lastName='
|
var destUrl = '/petclinic/owner/list?lastName='
|
||||||
if(angular.isDefined($scope.ownerFindForm)) {
|
if(angular.isDefined($scope.ownerFindForm)) {
|
||||||
|
@ -38,11 +38,55 @@ angular.module('controllers').controller('ownerListController', ['$scope', '$roo
|
||||||
* Owners detail (used for both Editable and non-editable pages)
|
* Owners detail (used for both Editable and non-editable pages)
|
||||||
*/
|
*/
|
||||||
angular.module('controllers').controller('ownerDetailController', ['$scope', '$resource', '$rootScope',
|
angular.module('controllers').controller('ownerDetailController', ['$scope', '$resource', '$rootScope',
|
||||||
function($scope, $resource, $rootScope) {
|
loadOwner
|
||||||
|
]);
|
||||||
|
|
||||||
|
function loadOwner($scope, $resource, $rootScope) {
|
||||||
var ownerResource = $resource('/petclinic/owner/' + $rootScope.ownerId);
|
var ownerResource = $resource('/petclinic/owner/' + $rootScope.ownerId);
|
||||||
$scope.owner = ownerResource.get();
|
$scope.owner = ownerResource.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Owner Edit Form
|
||||||
|
*/
|
||||||
|
angular.module('controllers').controller('ownerFormController', ['$scope', '$resource', '$http', '$rootScope', '$location',
|
||||||
|
function($scope, $resource, $http, $rootScope, $location) {
|
||||||
|
|
||||||
|
$scope.submitOwnerForm = function() {
|
||||||
|
var form = $scope.owner;
|
||||||
|
|
||||||
|
// Creating a Javascript object
|
||||||
|
var data = {
|
||||||
|
firstName: form.firstName,
|
||||||
|
lastName: form.lastName,
|
||||||
|
address: form.address,
|
||||||
|
city: form.city,
|
||||||
|
telephone: form.telephone
|
||||||
|
};
|
||||||
|
var restUrl = "/petclinic/owner/" + $rootScope.ownerId;
|
||||||
|
$http.post(restUrl, data);
|
||||||
|
$location.path('/owner/list');
|
||||||
|
}
|
||||||
|
|
||||||
|
loadOwner($scope, $resource, $rootScope);
|
||||||
|
|
||||||
}]);
|
}]);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,37 +1,36 @@
|
||||||
<div class="container">
|
<div class="container">
|
||||||
|
|
||||||
|
|
||||||
<h2>Owner</h2>
|
<h2>Owner</h2>
|
||||||
<form method="POST" class="form-horizontal" id="ownerForm">
|
<form class="form-horizontal" name="ownerForm" ng-controller="ownerFormController">
|
||||||
<fieldset>
|
<fieldset>
|
||||||
<div class="control-group" id="firstName">
|
<div class="control-group" id="firstName">
|
||||||
<label class="control-label">First name </label>
|
<label class="control-label">First name </label>
|
||||||
<input ng-model="owner.firstName" />
|
<input ng-model="owner.firstName" name="firstName" required/>
|
||||||
<span ng-show="owner.firstName.$error.required" class="help-inline">First name is required.</span>
|
<span ng-show="ownerForm.firstName.$error.required" class="help-inline">First name is required.</span>
|
||||||
</div>
|
</div>
|
||||||
<div class="control-group" id="lastName">
|
<div class="control-group" id="lastName">
|
||||||
<label class="control-label">Last name </label>
|
<label class="control-label">Last name </label>
|
||||||
<input ng-model="owner.lastName" />
|
<input ng-model="owner.lastName" name="lastName" required/>
|
||||||
<span ng-show="myForm.lastName.$error.required" class="help-inline">Last name is required.</span>
|
<span ng-show="ownerForm.lastName.$error.required" class="help-inline">Last name is required.</span>
|
||||||
</div>
|
</div>
|
||||||
<div class="control-group" id="address">
|
<div class="control-group" id="address">
|
||||||
<label class="control-label">Address </label>
|
<label class="control-label">Address </label>
|
||||||
<input ng-model="owner.address" />
|
<input ng-model="owner.address" name="address" required/>
|
||||||
<span ng-show="myForm.address.$error.required" class="help-inline">Address is required.</span>
|
<span ng-show="ownerForm.address.$error.required" class="help-inline">Address is required.</span>
|
||||||
</div>
|
</div>
|
||||||
<div class="control-group" id="city">
|
<div class="control-group" id="city">
|
||||||
<label class="control-label">City </label>
|
<label class="control-label">City </label>
|
||||||
<input ng-model="owner.city" />
|
<input ng-model="owner.city" name="city" required/>
|
||||||
<span ng-show="myForm.city.$error.required" class="help-inline">City is required.</span>
|
<span ng-show="ownerForm.city.$error.required" class="help-inline">City is required.</span>
|
||||||
</div>
|
</div>
|
||||||
<div class="control-group" id="telephone">
|
<div class="control-group" id="telephone">
|
||||||
<label class="control-label">Telephone </label>
|
<label class="control-label">Telephone </label>
|
||||||
<input ng-model="owner.telephone" />
|
<input ng-model="owner.telephone" name="telephone" required/>
|
||||||
<span ng-show="myForm.telephone.$error.required" class="help-inline">Telephone is required.</span>
|
<span ng-show="ownerForm.telephone.$error.required" class="help-inline">Telephone is required.</span>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="form-actions">
|
<div class="form-actions">
|
||||||
<button type="submit" ng-click="submitOwnerFindForm()">Submit</button>
|
<button type="submit" ng-click="submitOwnerForm()">Submit</button>
|
||||||
</div>
|
</div>
|
||||||
</fieldset>
|
</fieldset>
|
||||||
</form>
|
</form>
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
<h2>Find Owners</h2>
|
<h2>Find Owners</h2>
|
||||||
|
<form class="form-horizontal" id="ownerFindForm" ng-controller="ownerSearchController">
|
||||||
<form class="form-horizontal" id="ownerFindForm">
|
|
||||||
<fieldset>
|
<fieldset>
|
||||||
<div class="control-group" id="lastName">
|
<div class="control-group" id="lastName">
|
||||||
<label class="control-label">Last name </label>
|
<label class="control-label">Last name </label>
|
||||||
|
|
Loading…
Reference in a new issue