需求:第一次进入时,从session获取,并将值赋给了隐藏域,
第二次再次添加时,从隐藏域中获取,如果都从session获取,可能存在session混乱的问题,比如说在编辑的过成中,点开了另一个页面,这个页面把session给冲了。
//添加跳转页面
@RequestMapping("/zhouyiContentAdd") public ModelAndView zhouyiContentAdd(ZhouyiContent zhouyiContent,@SessionAttribute("authorId") Integer authorId){ ModelAndView mv = new ModelAndView(); //因为保存成功后,继续跳转到本页,所以内容清空。非常例。 zhouyiContent.setContent(null); zhouyiContent.setInfo(null); zhouyiContent.setTuan(null); zhouyiContent.setXiang(null); //这一行是从页面自动封装成一个zhouyiAuthor的实体bean ZhouyiAuthor zhouyiAuthor = zhouyiContent.getZhouyiAuthor(); if(zhouyiAuthor!=null) { // System.out.println("zhouyiAuthor is ========: "+ zhouyiAuthor.toString()); mv.addObject("zhouyiAuthor",zhouyiAuthor); }else { // System.out.println("zhouyiAuthor is use session"); mv.addObject("zhouyiAuthor", zhouyiAuthorService.getZhouyiContentAuthorById(authorId)); } mv.addObject("zhouyiYao",zhouyiYaoService.getZhouyiYaos() ); mv.setViewName("admin116/zhouyiContentAdd"); return mv; }
//保存业务 @RequestMapping("/zhouyiContentSave") public ModelAndView zhouyiContentSave(@Valid ZhouyiContent zhouyiContent,BindingResult errors,ZhouyiAuthor zhouyiAuthor) { ModelAndView mv = new ModelAndView(); try { zhouyiContentService.addZhouyiContent(zhouyiContent); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); errors.addError(new FieldError("zhouyiContent", "info", "*事务一致性处理错误")); } if(errors.getErrorCount()>0) { for (FieldError err : errors.getFieldErrors()) { System.out.println(err.getField()+err.getDefaultMessage()); } mv.addObject("tips", null); mv.addObject("errors", "*添加保存异常!"); mv.addObject("zhouyiAuthor", zhouyiAuthor); mv.addObject("zhouyiYao",zhouyiYaoService.getZhouyiYaos() ); mv.setViewName("admin116/zhouyiContentAdd"); return mv; } mv.addObject("tips", zhouyiContent.getId()+"条,添加成功!"); mv.setViewName("forward:/admin116/zhouyiContentAdd"); return mv; }
jsp页面,
在zhouyiAuthor.name 那里会自动封装成一个zhouyiAuthor的实体bean
<form:form modelAttribute="zhouyiContent" class="form-horizontal" id="saveForm" > <div class="form-group"> <label for="zhouyiAuthor" class="col-sm-2 control-label">作者</label> <div class="col-sm-2"> <input type="text" name="zhouyiAuthor.name" class="form-control" readonly="true" value="${zhouyiAuthor.name }"/> <form:hidden path="zhouyiAuthor.id" value="${zhouyiAuthor.id }"></form:hidden> </div> </div> <div class="form-group"> <label for="info" class="col-sm-2 control-label">爻</label> <div class="col-sm-6"> <form:select path="zhouyiYao.id" class="form-control" id="zhouyiYao" > <form:options items="${zhouyiYao}" itemValue="id" itemLabel="extend"></form:options> </form:select> </div> </div>