1
- /*
2
- * Copyright 2012-2025 the original author or authors.
3
- *
4
- * Licensed under the Apache License, Version 2.0 (the "License");
5
- * you may not use this file except in compliance with the License.
6
- * You may obtain a copy of the License at
7
- *
8
- * https://www.apache.org/licenses/LICENSE-2.0
9
- *
10
- * Unless required by applicable law or agreed to in writing, software
11
- * distributed under the License is distributed on an "AS IS" BASIS,
12
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- * See the License for the specific language governing permissions and
14
- * limitations under the License.
15
- */
16
1
package org .springframework .samples .petclinic .owner ;
17
2
18
3
import java .util .List ;
19
- import java .util .Optional ;
20
4
21
5
import org .springframework .data .domain .Page ;
22
6
import org .springframework .data .domain .PageRequest ;
32
16
import org .springframework .web .bind .annotation .PostMapping ;
33
17
import org .springframework .web .bind .annotation .RequestParam ;
34
18
import org .springframework .web .servlet .ModelAndView ;
19
+ import org .springframework .web .servlet .mvc .support .RedirectAttributes ;
35
20
36
21
import jakarta .validation .Valid ;
37
- import org .springframework .web .servlet .mvc .support .RedirectAttributes ;
38
22
39
- /**
40
- * @author Juergen Hoeller
41
- * @author Ken Krebs
42
- * @author Arjen Poutsma
43
- * @author Michael Isvy
44
- * @author Wick Dynex
45
- */
46
23
@ Controller
47
24
class OwnerController {
48
25
@@ -60,15 +37,13 @@ public void setAllowedFields(WebDataBinder dataBinder) {
60
37
}
61
38
62
39
@ ModelAttribute ("owner" )
63
- public Owner findOwner (@ PathVariable (name = "ownerId" , required = false ) Integer ownerId ) {
64
- return ownerId == null ? new Owner ()
65
- : this .owners .findById (ownerId )
66
- .orElseThrow (() -> new IllegalArgumentException ("Owner not found with id: " + ownerId
67
- + ". Please ensure the ID is correct " + "and the owner exists in the database." ));
40
+ public Owner ownerModel () {
41
+ return new Owner ();
68
42
}
69
43
70
44
@ GetMapping ("/owners/new" )
71
- public String initCreationForm () {
45
+ public String initCreationForm (Model model ) {
46
+ model .addAttribute ("owner" , new Owner ());
72
47
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM ;
73
48
}
74
49
@@ -91,27 +66,22 @@ public String initFindForm() {
91
66
92
67
@ GetMapping ("/owners" )
93
68
public String processFindForm (@ RequestParam (defaultValue = "1" ) int page , Owner owner , BindingResult result ,
94
- Model model ) {
95
- // allow parameterless GET request for /owners to return all records
69
+ Model model ) {
96
70
if (owner .getLastName () == null ) {
97
- owner .setLastName ("" ); // empty string signifies broadest possible search
71
+ owner .setLastName ("" ); // broadest possible search
98
72
}
99
73
100
- // find owners by last name
101
74
Page <Owner > ownersResults = findPaginatedForOwnersLastName (page , owner .getLastName ());
102
75
if (ownersResults .isEmpty ()) {
103
- // no owners found
104
76
result .rejectValue ("lastName" , "notFound" , "not found" );
105
77
return "owners/findOwners" ;
106
78
}
107
79
108
80
if (ownersResults .getTotalElements () == 1 ) {
109
- // 1 owner found
110
- owner = ownersResults .iterator ().next ();
111
- return "redirect:/owners/" + owner .getId ();
81
+ Owner singleOwner = ownersResults .iterator ().next ();
82
+ return "redirect:/owners/" + singleOwner .getId ();
112
83
}
113
84
114
- // multiple owners found
115
85
return addPaginationModel (page , model , ownersResults );
116
86
}
117
87
@@ -130,44 +100,43 @@ private Page<Owner> findPaginatedForOwnersLastName(int page, String lastname) {
130
100
return owners .findByLastNameStartingWith (lastname , pageable );
131
101
}
132
102
103
+
133
104
@ GetMapping ("/owners/{ownerId}/edit" )
134
- public String initUpdateOwnerForm () {
105
+ public String initUpdateOwnerForm (@ PathVariable ("ownerId" ) Integer ownerId , Model model ) {
106
+ Owner owner = owners .findById (ownerId )
107
+ .orElseThrow (() -> new IllegalArgumentException ("Owner not found with id: " + ownerId ));
108
+ model .addAttribute ("owner" , owner );
135
109
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM ;
136
110
}
137
111
138
112
@ PostMapping ("/owners/{ownerId}/edit" )
139
- public String processUpdateOwnerForm (@ Valid Owner owner , BindingResult result , @ PathVariable ("ownerId" ) int ownerId ,
140
- RedirectAttributes redirectAttributes ) {
113
+ public String processUpdateOwnerForm (@ Valid Owner owner , BindingResult result ,
114
+ @ PathVariable ("ownerId" ) Integer ownerId ,
115
+ RedirectAttributes redirectAttributes ) {
141
116
if (result .hasErrors ()) {
142
117
redirectAttributes .addFlashAttribute ("error" , "There was an error in updating the owner." );
143
118
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM ;
144
119
}
145
120
146
- if (owner .getId () != ownerId ) {
121
+ if (owner .getId () == null || ! ownerId . equals ( owner . getId ()) ) {
147
122
result .rejectValue ("id" , "mismatch" , "The owner ID in the form does not match the URL." );
148
- redirectAttributes .addFlashAttribute ( "error " , "Owner ID mismatch. Please try again." );
123
+ redirectAttributes .addAttribute ( "ownerId " , ownerId );
149
124
return "redirect:/owners/{ownerId}/edit" ;
150
125
}
151
126
152
127
owner .setId (ownerId );
153
128
this .owners .save (owner );
154
129
redirectAttributes .addFlashAttribute ("message" , "Owner Values Updated" );
130
+ redirectAttributes .addAttribute ("ownerId" , ownerId );
155
131
return "redirect:/owners/{ownerId}" ;
156
132
}
157
133
158
- /**
159
- * Custom handler for displaying an owner.
160
- * @param ownerId the ID of the owner to display
161
- * @return a ModelMap with the model attributes for the view
162
- */
163
134
@ GetMapping ("/owners/{ownerId}" )
164
- public ModelAndView showOwner (@ PathVariable ("ownerId" ) int ownerId ) {
135
+ public ModelAndView showOwner (@ PathVariable ("ownerId" ) Integer ownerId ) {
136
+ Owner owner = this .owners .findById (ownerId )
137
+ .orElseThrow (() -> new IllegalArgumentException ("Owner not found with id: " + ownerId ));
165
138
ModelAndView mav = new ModelAndView ("owners/ownerDetails" );
166
- Optional <Owner > optionalOwner = this .owners .findById (ownerId );
167
- Owner owner = optionalOwner .orElseThrow (() -> new IllegalArgumentException (
168
- "Owner not found with id: " + ownerId + ". Please ensure the ID is correct " ));
169
- mav .addObject (owner );
139
+ mav .addObject ("owner" , owner );
170
140
return mav ;
171
141
}
172
-
173
142
}
0 commit comments