SpringMVC数据回显

来源:互联网 发布:阿里云建立远程连接 编辑:程序博客网 时间:2024/05/18 15:04

什么是数据回显

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

pojo数据回显方法

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

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

这里写图片描述

所以我们可以转发到页面后通过EL表达式把值取出来。

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

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

    //商品分类    @ModelAttribute("itemtypes")    public Map<String, String> getItemTypes() throws  Exception{        Map<String, String> itemTypes = new HashMap<String,String>();        itemTypes.put("101", "数码");        itemTypes.put("102", "母婴");        return itemTypes;    }

该方法不需要我们手动调用,就可以在JSP页面通过EL访问requestScope里面的数据。

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

3、最简单方法使用model实现回显,可以不用@ModelAttribute

这里写图片描述

简单类型数据回显

因为SpringMVC不会把String,Integer,int等单纯类型存入request域中,所以我们需要用下面的方法。

//使用最简单方法使用model。model.addAttribute("id", id);
1 0
原创粉丝点击