SpringMVC数据回显

数据回显即 表单提交失败后,原来提交的数据重新在提交页面上显示,不需要全部重新填写。在页面上数据回显本质上就是获取reqeust域的值

一、pojo数据默认回显方法

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

controller中传入User

	@RequestMapping(value = "save", method = RequestMethod.POST)
    public String save(User user) {
        、、、
    }

jsp中用user 作为key:

<input type="text" name="username" value="${user.username}" class="form-control">

二、使用@ModelAttribute注解

controller:

	@RequestMapping(value = "save", method = RequestMethod.POST)
    public String save(@ModelAttribute("editUser") User user) {
        、、、
    }

jsp:

<input type="text" name="username" value="${editUser.username}" class="form-control">
注:被@ModelAttribute注释的方法会在此controller 每个方法执行前被执行,所以可用于回显公共数据

controller:

	@ModelAttribute("typelist")
    public Map<String,String> getTypeList(){
        HashMap<String,String> typelist= new HashMap<String,String>();
        typelist.put("0","男装");
        typelist.put("1","女装");
        return typelist;
    }

jsp:

<select name="typelist">
   <c:foreach items="${typelist}" var="type">
        <option value="${type.key}">${type.value}</option>
   </c:foreach>
</select>

三、使用model存入request,在前端取出

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值