RequestMapping value id method RequestMethod PUT public String updateC

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
@RequestMapping(value = "{id}", method = RequestMethod.PUT)
public String updateCustomer(@PathVariable("id") String id,
@ModelAttribute("model") ModelMap model,
@Valid @ModelAttribute("updateCustomer") CustomerForm form,
BindingResult result) {
Customer customer = null;
if (id.matches("[0-9]+")) {
customer = customerService.getById(Long.parseLong(id));
}
if (customer == null) {
return "redirect:/customers";
}
if (!result.hasErrors()) {
customer.setFirstName(form.getFirstName());
customer.setLastName(form.getLastName());
customer.setAddress(form.getAddress());
customer.setBirthDate(form.getBirthDate());
customerService.save(customer);
}
// else {
// List<String> errors = new ArrayList<>();
// for (FieldError error : result.getFieldErrors()) {
// errors.add(error.getField()+ ": " + error.getDefaultMessage());
// }
// model.addAttribute("errors", errors);
// }
model.addAttribute("customer", customer);
return "customer";
<@form.form id="update_form" commandName="updateCustomer" acceptCharset="UTF-8" method="PUT">
<@spring.bind path="firstName">
<label for="firstName">First Name</label>
<@form.input path="firstName" id="firstName"/>
<#if status.error>
<@form.errors path="firstName" element="label"/>
</#if>
</@spring.bind>
</@form.form>