I have a profile update page which is supposed to show errors on the fields.
However, when I remove the value from an input field instead of getting an error below, I get 405 - Request method 'PATCH' not supported
.
I have no idea what could be the problem, any advice is appreciated.
@Controller
@RequestMapping("/users")
public class UserController {
@GetMapping("/profile/update/{id}")
public String showUpdateForm(@PathVariable("id") Long id, Model model) {
ProfileUpdateServiceModel profileServiceModel = this.userService.getProfileUpdateServiceModelById(id);
ProfileUpdateBindingModel profileUpdateBindingModel = this.mapper.map(profileServiceModel, ProfileUpdateBindingModel.class);
if (!model.containsAttribute("profileUpdateBindingModel")) {
model.addAttribute("profileUpdateBindingModel", profileUpdateBindingModel);
}
// model.addAttribute("profileUpdateBindingModel", profileUpdateBindingModel);
return "update-profile";
}
@PatchMapping("/profile/update/{id}")
public String update(@PathVariable("id") Long id,
@Valid ProfileUpdateBindingModel profileUpdateBindingModel,
BindingResult br,
RedirectAttributes rAtt) {
if (br.hasErrors()) {
rAtt
.addFlashAttribute("profileUpdateBindingModel", profileUpdateBindingModel)
.addFlashAttribute("org.springframework.validation.BindingResult.profileUpdateBindingModel", br);
return "redirect:/users/profile/update/" id;
}
return "profile";
}
profile.html:
<form
th:method="GET"
th:action="@{/users/profile/update/{id}(id=${session.currentUserId})}">
update-profile.html:
<form
th:action="@{/users/profile/update/{id}(id=*{userId})}"
th:method="PATCH"
th:object="${profileUpdateBindingModel}"
enctype="multipart/form-data">
<div >
<label >First Name<span
>*</span></label>
<input
th:field="*{firstName}"
type="text"
name="name"/>
<div >
<small th:if="${#fields.hasErrors('firstName')}"
th:errors="*{firstName}"
id="first-nameError"
>xxx</small>
</div>
</div>
CodePudding user response:
HTTP Code 405 means Method not Allowed
. In your spring you are mentioning GET
method but in your Form you are mentioning to submit a PATCH
method.
Change your form to
<form
th:action="@{/users/profile/update/{id}(id=*{userId})}"
th:method="GET"
th:object="${profileUpdateBindingModel}"
enctype="multipart/form-data">
CodePudding user response:
I'd use the pathvariable like this :
@PathVariable Long id
or like this ::
@PatchMapping(params = "/{id}", consumes = "application/json")