zoukankan      html  css  js  c++  java
  • SpringMVC的数据回现

    一、什么是数据回显

    数据提交后,如果出现错误,将刚才提交的数据回显到刚才的提交页面。

    二、pojo数据回显方法

    1、springmvc默认对pojo数据进行回显。
    pojo数据传入controller方法后,springmvc自动将pojo数据放到request域,key等于pojo类型(首字母小写)

    使用@ModelAttribute指定pojo回显到页面在request中的key

    2、@ModelAttribute还可以将方法的返回值传到页面

    在商品查询列表页面,通过商品类型查询商品信息。
    在controller中定义商品类型查询方法,最终将商品类型传到页面。

      @ModelAttribute("itemTypes")
        public Map<String,String> getItemsType(){
            Map<String,String> map = new HashMap<>();
            map.put("1","数码产品");
            map.put("2","生活用品");
            return map;
        }
    

    页面上可以得到itemTypes数据。

      <td>商品类型: <select name="itemtype">
                            <c:forEach items="${itemTypes}" var="itemType">
                                <option value="${itemType.key}">${itemType.value}</option>
                            </c:forEach>
                        </select></td>
    

    测试如下:

    三、最简单的pojo数据回现

    使用处理器适配器的默认支持的Model对象,将其作为controller的形参来回现数据。

     @RequestMapping("/updateitems")
        public String updateitems(Model model,Integer id, @Validated(value = {VaildatorGroup1.class}) ItemsCustom itemsExtend,
                                  BindingResult bindingResult) throws Exception{
    
            if(bindingResult.hasErrors()){
                List<ObjectError> errors = bindingResult.getAllErrors();
                for (ObjectError error: errors) {
                    System.out.println(error.getDefaultMessage());
                }
                model.addAttribute("errors",errors);
                model.addAttribute("itemsExtend",itemsExtend);
                return "updateitem";
            }
                itemsService.updateitems(id,itemsExtend);
            return"redirect:queryItems.action";
        }
    

    四、简单类型的回现

    用最简单方法使用model。

    model.addAttribute("id", id);
  • 相关阅读:
    memcache 应用场景
    如何写接口文档(登录)
    PHP常见错误级别及错误码
    ex33 while 循环
    ex32 循环和列表
    ex31--作出决定
    ex29-30 if,elif and else.
    ex28 布尔表达式练习
    ex25 更多更多的实践
    ex21 函数可以返回某些东西
  • 原文地址:https://www.cnblogs.com/jack1995/p/7357580.html
Copyright © 2011-2022 走看看