我想在jspx文件中有一个隐藏字段。
我想要做的是自动将经过身份验证的用户名保存到数据库中。我是这样做的:
使用以下命令修改我的bean:
public void Got.setUserkt(String userkt) {
final String currentUser = SecurityContextHolder.getContext().getAuthentication().getName();
this.userkt = currentUser;
}环顾四周,我发现我不得不在我的create.jspx页面中使用render=" false“,但是当render设置为false时,我的输入字段中的任何数据都不会保存在数据库中。
我做错了什么?
发布于 2011-05-17 17:36:55
我会继续我所做的,为了我的新秀伙伴们。
第一步:从我的roo控制器(EntityController_Roo_Controller.aj)中截取我的方法
@RequestMapping(method = RequestMethod.POST)
public String create(@Valid Got got, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest) {
if (bindingResult.hasErrors()) {
uiModel.addAttribute("got", got);
addDateTimeFormatPatterns(uiModel);
return "gots/create";
}
uiModel.asMap().clear();
got.persist();
return "redirect:/gots/" + encodeUrlPathSegment(got.getId().toString(), httpServletRequest);
}第二步:将方法粘贴到java控制器(EntityController.java)中
第三步:编辑方法以获取usernmae并使用它修改我的方法
@请求映射(method= RequestMethod.POST)
公共字符串create(@Valid got,BindingResult bindingResult,模型uiModel,HttpServletRequest httpServletRequest,主体主体){ if (bindingResult.hasErrors()) { uiModel.addAttribute(" Got ",got);addDateTimeFormatPatterns(uiModel);return "gots/create";} uiModel.asMap().clear();got.setUserkt(principal.getName());got.persist();return "redirect:/gots/“+ encodeUrlPathSegment(got.getId().toString(),httpServletRequest);}
再次感谢!
发布于 2011-05-16 23:23:34
为什么不创建一个方面来拦截保存目标实体并添加相应的字段,或者通过将其复制到*.java控制器并在其中添加字段来覆盖Spring Controller中的相应方法呢?
发布于 2014-07-08 19:31:03
disableFormBinding和类型的组合应该可以工作,如下所示。
注意:不确定它是否是这样设计的:)
https://stackoverflow.com/questions/6019428
复制相似问题