I have an hibernate entity object. I need to update this object, so I passed this object to a form. In the form i will change some values and the others are constant. And I can not show these constant values to the client, so they should pass to next page via another method except from diplaying them explicity in a html form.
我有一个hibernate实体对象。我需要更新此对象,因此我将此对象传递给表单。在形式中,我将改变一些值,其他值是不变的。而且我无法向客户端显示这些常量值,因此它们应该通过另一种方法传递到下一页,除非在html表单中将它们显式化。
Here is my object obtained in controller and passed to the view:
这是我在控制器中获得的对象并传递给视图:
@GetMapping("/update")
public String update(@RequestParam("dataId") int id, Model md){
Doctor dr = doctorService.getById(id);
/*for example lets say this doctor object has following properties
dr.setId(3);
dr.setName("James");
dr.setUserId(7);
*/
md.addAttribute("doctor", dr);
return "object-form";
}
Here is my form in view :
这是我的表格:
<form:form action="save" modelAttribute="doctor" method="post">
<form:errors path="name"></form:errors>
<form:input path="name" placeholder="Doktor İsmi" class="form-control" />
<form:hidden path="id" />
<input type="submit" value="Save doc" />
</form:form>
From form, only name and id values are coming, however, the userId is null. I need to get this userId without post.
从表单开始,只有name和id值会出现,但userId为null。我需要在没有帖子的情况下获得此userId。
Here is my post-process controller that I handle the object:
这是我处理对象的后处理控制器:
@PostMapping(value="/save")
public String save(@Valid Doctor dr, BindingResult bindingResult){
doctorValidator.validate(dr, bindingResult);
if (bindingResult.hasErrors()) {
return "object-form";
}
else{
doctorService.save(dr);
return "redirect:list";
}
}
I don't know how can achieve this r even there is way for it. I searched on Google but I did not find any solution.
我不知道怎么能实现这个甚至有它的方法。我在Google上搜索过,但我找不到任何解决方案。
Thank a lot,,
非常感谢,,
1 个解决方案
#1
1
You can get previous doctor object from db and get the user ID from there like below:
您可以从db获取以前的医生对象并从中获取用户ID,如下所示:
@PostMapping(value="/save")
public String save(@Valid Doctor dr, BindingResult bindingResult){
Doctor prevDr = doctorService.getById(dr.getId());
dr.setUserId(prevDr.getUserId());
doctorValidator.validate(dr, bindingResult);
if (bindingResult.hasErrors()) {
return "object-form";
}
else{
doctorService.save(dr);
return "redirect:list";
}
}
#1
1
You can get previous doctor object from db and get the user ID from there like below:
您可以从db获取以前的医生对象并从中获取用户ID,如下所示:
@PostMapping(value="/save")
public String save(@Valid Doctor dr, BindingResult bindingResult){
Doctor prevDr = doctorService.getById(dr.getId());
dr.setUserId(prevDr.getUserId());
doctorValidator.validate(dr, bindingResult);
if (bindingResult.hasErrors()) {
return "object-form";
}
else{
doctorService.save(dr);
return "redirect:list";
}
}