ownerForm and validation now working

This commit is contained in:
michaelisvy 2015-06-14 20:50:22 +08:00
parent f1508666ec
commit 41ec22b4be
6 changed files with 167 additions and 96 deletions

View file

@ -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;
}
}
}

View file

@ -16,7 +16,6 @@
package org.springframework.samples.petclinic.web;
import java.util.Collection;
import java.util.Map;
import javax.validation.Valid;
@ -24,17 +23,16 @@ 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.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
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.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;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;
/**
* @author Juergen Hoeller
@ -57,70 +55,31 @@ public class OwnerResource {
public void setAllowedFields(WebDataBinder dataBinder) {
dataBinder.setDisallowedFields("id");
}
@RequestMapping(value = "/owner/new", method = RequestMethod.GET)
public String initCreationForm(Map<String, Object> model) {
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) {
@ModelAttribute
public Owner retrieveOwner(@PathVariable("ownerId") int 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;
}
}

View file

@ -33,7 +33,7 @@ petClinicApp.config(['$routeProvider',
}).
when('/owner/:id/edit', {
templateUrl: 'scripts/app/owner/ownerForm.html',
controller: 'ownerDetailController'
controller: 'ownerFormController'
}).
when('/vets', {
templateUrl: 'scripts/app/vet/vetList.html',

View file

@ -5,14 +5,14 @@
*/
angular.module('controllers').controller('ownerSearchController', ['$scope', '$rootScope', '$resource', '$location',
function($scope, $rootScope, $resource, $location) {
$scope.submitOwnerFindForm = function(item, event) {
$scope.submitOwnerFindForm = function() {
var destUrl = '/petclinic/owner/list?lastName='
if(angular.isDefined($scope.ownerFindForm)) {
destUrl += $scope.ownerFindForm.lastName;
}
var ownerResource = $resource(destUrl);
$rootScope.owners = ownerResource.query();
$location.path('/owner/list');
@ -26,23 +26,67 @@ angular.module('controllers').controller('ownerListController', ['$scope', '$roo
if ($rootScope.owners!=null){
$scope.ownerList = $rootScope.owners;
}
$scope.displayOwnerDetail = function(id) {
var url = "owner/" + id + "/view";
$rootScope.ownerId = id;
$location.path(url);
}
}
}]);
/*
* Owners detail (used for both Editable and non-editable pages)
*/
angular.module('controllers').controller('ownerDetailController', ['$scope', '$resource', '$rootScope',
function($scope, $resource, $rootScope) {
var ownerResource = $resource('/petclinic/owner/' + $rootScope.ownerId);
$scope.owner = ownerResource.get();
loadOwner
]);
function loadOwner($scope, $resource, $rootScope) {
var ownerResource = $resource('/petclinic/owner/' + $rootScope.ownerId);
$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);
}]);

View file

@ -1,37 +1,36 @@
<div class="container">
<h2>Owner</h2>
<form method="POST" class="form-horizontal" id="ownerForm">
<form class="form-horizontal" name="ownerForm" ng-controller="ownerFormController">
<fieldset>
<div class="control-group" id="firstName">
<label class="control-label">First name </label>
<input ng-model="owner.firstName" />
<span ng-show="owner.firstName.$error.required" class="help-inline">First name is required.</span>
<input ng-model="owner.firstName" name="firstName" required/>
<span ng-show="ownerForm.firstName.$error.required" class="help-inline">First name is required.</span>
</div>
<div class="control-group" id="lastName">
<label class="control-label">Last name </label>
<input ng-model="owner.lastName" />
<span ng-show="myForm.lastName.$error.required" class="help-inline">Last name is required.</span>
<input ng-model="owner.lastName" name="lastName" required/>
<span ng-show="ownerForm.lastName.$error.required" class="help-inline">Last name is required.</span>
</div>
<div class="control-group" id="address">
<label class="control-label">Address </label>
<input ng-model="owner.address" />
<span ng-show="myForm.address.$error.required" class="help-inline">Address is required.</span>
<input ng-model="owner.address" name="address" required/>
<span ng-show="ownerForm.address.$error.required" class="help-inline">Address is required.</span>
</div>
<div class="control-group" id="city">
<label class="control-label">City </label>
<input ng-model="owner.city" />
<span ng-show="myForm.city.$error.required" class="help-inline">City is required.</span>
<input ng-model="owner.city" name="city" required/>
<span ng-show="ownerForm.city.$error.required" class="help-inline">City is required.</span>
</div>
<div class="control-group" id="telephone">
<label class="control-label">Telephone </label>
<input ng-model="owner.telephone" />
<span ng-show="myForm.telephone.$error.required" class="help-inline">Telephone is required.</span>
<input ng-model="owner.telephone" name="telephone" required/>
<span ng-show="ownerForm.telephone.$error.required" class="help-inline">Telephone is required.</span>
</div>
<div class="form-actions">
<button type="submit" ng-click="submitOwnerFindForm()">Submit</button>
<button type="submit" ng-click="submitOwnerForm()">Submit</button>
</div>
</fieldset>
</form>

View file

@ -1,6 +1,5 @@
<h2>Find Owners</h2>
<form class="form-horizontal" id="ownerFindForm">
<form class="form-horizontal" id="ownerFindForm" ng-controller="ownerSearchController">
<fieldset>
<div class="control-group" id="lastName">
<label class="control-label">Last name </label>