• springmvc 从session中和隐藏域中获取值


    需求:第一次进入时,从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>
  • 相关阅读:
    5种Python使用定时调度任务的方式
    基于Tensorflow + Opencv 实现CNN自定义图像分类
    CANN 5.0硬核技术抢先看
    大力出奇迹,揭秘昇腾CANN的AI超能力
    MSQL:超强的多任务表示学习方法
    Shell:Lite OS在线调试工具知多少
    带你掌握Vue过滤器filters及时间戳转换
    Selenium系列(六) 详细解读强制等待、隐式等待、显式等待的区别和源码解读
    Linux常用命令 top命令详解(重点)
    Selenium系列(一) 详细解读8种元素定位方式
  • 原文地址:https://www.cnblogs.com/sdgtxuyong/p/12444903.html
Copyright © 2020-2023  润新知