@PutMapping("/universities/{id}/edit") public String editUniversity( @Valid @ModelAttribute("thisUniversity") University updatedUniversity, BindingResult result, @PathVariable Long id) { if(result.hasErrors()) { return "editUniversity"; } universityService.updateUniversity(updatedUniversity); return "redirect:/"; }
Service
服务
// imports
@Service public class UniversityService {
@Autowired private UniversityRepo universityRepo;
// Add a university public University createUniversity (University newUniversity) { return universityRepo.save(newUniversity); }
// More codes
public University updateUniversity(University updatedUniversity) { return universityRepo.save(updatedUniversity); }
}
HTML
超文本标记语言
<!DOCTYPE html> <1-- head --> <form th:taction="@{'/universities/'+${thisUniversity.universityId}+'/edit'}" th:object="${thisUniversity}" th:method="POST"> <!-- The below tag is required for PUT/DELETE request --> <input type="hidden" name="_method" value="PUT">
I have try th:method="PUT" in form tag or not including <input type="hidden" name="_method" value="PUT"> But the results are still the same
我在表单标签中尝试了th:method=“PUT”或不包含,但结果仍然相同
更多回答
Stop using entities in your api. Learn about 3tier architecture
停止在API中使用实体。了解3TIER架构
Has your updatedUniversity entity a proper id set?
您的更新版大学实体是否设置了正确的ID?
优秀答案推荐
You're taking the id as a path variable but not giving it to the updated object, which is why it gets saved to a new object instead of editing the existing one.
我是一名优秀的程序员,十分优秀!