springmvc数据回显问题
2016-10-06 15:07
232 查看
数据回显就是在页面提交请求后,如果出现错误,则将刚才提交的数据回显到刚才的提交页面
1、springmvc默认对pojo数据进行回显
Pojo数据传入controller方法后,springmvc自动将pojo数据放到request域,key等于pojo类型(首字母小写)。如:
<tr>
<td>商品价格</td>
<td><input type="text" name="price" value="$<span style="background-color: rgb(255, 255, 102);">{itemsCustom</span>.price }"/></td>
</tr>页面中的取值key为itemsCustom,要和 controller方法形参中的参数名称一致,这样springmvc就会自动的进行数据的回显
@RequestMapping("/editItemsSubmit")
public String editItemsSubmit(Model model,HttpServletRequest request,Integer id,String name,
@Validated(value={ValidGroup1.class}) ItemsCustom <span style="background-color: rgb(255, 255, 102);">itemsCusto</span>m,BindingResult bindingResult) throws Exception{}
2、使用@ModelAttribute("items"):可以指定pojo回显到页面在request域中的key
<table width="100%;" border="1">
<tr>
<td>商品名称</td>
<td><input type="text" name="name" value="${<span style="background-color: rgb(255, 255, 102);">items</span>.name }"/></td>
</tr>
<tr>
<td>商品价格</td>
<td><input type="text" name="price" value="${<span style="background-color: rgb(255, 255, 102);">items</span>.price }"/></td>
</tr>
<tr>
<td>商品生产日期</td>
<td><input type="text" name="createtime" value="<fmt:formatDate value="${<span style="background-color: rgb(255, 255, 102);">items</span>.createtime }" pattern="yyyy-MM-dd HH:mm:ss"/>"/></td>
</tr>
</table>
3、@ModelAttribute还可以将方法的返回值传到页面
例如:在controller中定义商品类型查询方法,最终将商品类型传到页面。
//商品分类
//itemtypes表示最终将方法返回值放在request中的key
@ModelAttribute("<span style="background-color: rgb(255, 255, 102);">itemtypes</span>")
public Map<String, Object> getItemTypes(){
Map<String, Object> itemTypes = new HashMap<String, Object>();
itemTypes.put("101", "数码");
itemTypes.put("102", "母婴");
return itemTypes;
}
在页面上可以得到itemsTypes数据
<td>
商品类型:
<select name="itemtype">
<c:forEach items="${<span style="background-color: rgb(255, 255, 102);">itemtypes</span> }" var="itemtype">
<option value="${itemtype.key }">${itemtype.value}</option>
</c:forEach>
</select>
</td>3、最简单的数据回显的方式:使用model
@RequestMapping("/editItemsSubmit")
public String editItemsSubmit(Model model,HttpServletRequest request,Integer id,String name,
@ModelAttribute("items") @Validated(value={ValidGroup1.class}) ItemsCustom itemsCustom,BindingResult bindingResult) throws Exception{
//获取校验错误信息
if(bindingResult.hasErrors()){//如果有错误
//输出错误信息
List<ObjectError> allErrors = bindingResult.getAllErrors();
for (ObjectError objectError : allErrors) {
//输出错误信息
System.out.println(objectError.getDefaultMessage());
}
//将错误信息传到页面
model.addAttribute("allErrors", allErrors);
<span style="background-color: rgb(255, 255, 102);">//可以直接使用model将提交的pojo回显到页面
model.addAttribute("itemsCustom",itemsCustom);</span>
//出错后重新回到商品的修改页面
return "items/editItems";
}
//调用service更新商品信息,页面需要将商品新传到此方法
itemsService.updateItems(id, itemsCustom);
//重定向到商品的查询列表
return "redirect:queryItems.action";
}在页面上通过 ${itemsCustom.***}接收参数
4、简单类型的数据回显
springmvc不支持简单类型的数据回显,因此直接使用model即可。
model.addAttribute("id",id);
1、springmvc默认对pojo数据进行回显
Pojo数据传入controller方法后,springmvc自动将pojo数据放到request域,key等于pojo类型(首字母小写)。如:
<tr>
<td>商品价格</td>
<td><input type="text" name="price" value="$<span style="background-color: rgb(255, 255, 102);">{itemsCustom</span>.price }"/></td>
</tr>页面中的取值key为itemsCustom,要和 controller方法形参中的参数名称一致,这样springmvc就会自动的进行数据的回显
@RequestMapping("/editItemsSubmit")
public String editItemsSubmit(Model model,HttpServletRequest request,Integer id,String name,
@Validated(value={ValidGroup1.class}) ItemsCustom <span style="background-color: rgb(255, 255, 102);">itemsCusto</span>m,BindingResult bindingResult) throws Exception{}
2、使用@ModelAttribute("items"):可以指定pojo回显到页面在request域中的key
//@ModelAttribute("items"):可以指定pojo回显到页面在request域中的key @RequestMapping("/editItemsSubmit") public String editItemsSubmit(Model model,HttpServletRequest request,Integer id,String name, <span style="background-color: rgb(255, 255, 102);">@ModelAttribute("items")</span> @Validated(value={ValidGroup1.class}) ItemsCustom itemsCustom,BindingResult bindingResult) throws Exception{}页面中获取参数的key就是@ModelAttribute中定义的key
<table width="100%;" border="1">
<tr>
<td>商品名称</td>
<td><input type="text" name="name" value="${<span style="background-color: rgb(255, 255, 102);">items</span>.name }"/></td>
</tr>
<tr>
<td>商品价格</td>
<td><input type="text" name="price" value="${<span style="background-color: rgb(255, 255, 102);">items</span>.price }"/></td>
</tr>
<tr>
<td>商品生产日期</td>
<td><input type="text" name="createtime" value="<fmt:formatDate value="${<span style="background-color: rgb(255, 255, 102);">items</span>.createtime }" pattern="yyyy-MM-dd HH:mm:ss"/>"/></td>
</tr>
</table>
3、@ModelAttribute还可以将方法的返回值传到页面
例如:在controller中定义商品类型查询方法,最终将商品类型传到页面。
//商品分类
//itemtypes表示最终将方法返回值放在request中的key
@ModelAttribute("<span style="background-color: rgb(255, 255, 102);">itemtypes</span>")
public Map<String, Object> getItemTypes(){
Map<String, Object> itemTypes = new HashMap<String, Object>();
itemTypes.put("101", "数码");
itemTypes.put("102", "母婴");
return itemTypes;
}
在页面上可以得到itemsTypes数据
<td>
商品类型:
<select name="itemtype">
<c:forEach items="${<span style="background-color: rgb(255, 255, 102);">itemtypes</span> }" var="itemtype">
<option value="${itemtype.key }">${itemtype.value}</option>
</c:forEach>
</select>
</td>3、最简单的数据回显的方式:使用model
@RequestMapping("/editItemsSubmit")
public String editItemsSubmit(Model model,HttpServletRequest request,Integer id,String name,
@ModelAttribute("items") @Validated(value={ValidGroup1.class}) ItemsCustom itemsCustom,BindingResult bindingResult) throws Exception{
//获取校验错误信息
if(bindingResult.hasErrors()){//如果有错误
//输出错误信息
List<ObjectError> allErrors = bindingResult.getAllErrors();
for (ObjectError objectError : allErrors) {
//输出错误信息
System.out.println(objectError.getDefaultMessage());
}
//将错误信息传到页面
model.addAttribute("allErrors", allErrors);
<span style="background-color: rgb(255, 255, 102);">//可以直接使用model将提交的pojo回显到页面
model.addAttribute("itemsCustom",itemsCustom);</span>
//出错后重新回到商品的修改页面
return "items/editItems";
}
//调用service更新商品信息,页面需要将商品新传到此方法
itemsService.updateItems(id, itemsCustom);
//重定向到商品的查询列表
return "redirect:queryItems.action";
}在页面上通过 ${itemsCustom.***}接收参数
4、简单类型的数据回显
springmvc不支持简单类型的数据回显,因此直接使用model即可。
model.addAttribute("id",id);
相关文章推荐
- springMVC_json格式数据的接收和发送(未解决中文乱码问题)
- Ext 表单数据回显问题
- SpringMVC-Hibernate关于数据更新没有持久化的问题
- SpringMVC中servletFileUpload.parseRequest(request)解析为空获取不到数据问题
- springMVC之对象中的基本类型数据绑定遇到的问题
- 求解--jsp中radio的性别信息的数据回显问题
- IE浏览器请求json数据弹出下载框问题【在springmvc中配置如下内容,可以避免】
- springMVC整合hibernate的时候数据插入需要flush问题
- jsp中radio的性别信息的数据回显问题
- SpringMVC中servletFileUpload.parseRequest(request)解析为空获取不到数据问题
- struts2--(3)--一些小的知识---简单数据验证信息回显问题
- springmvc数据绑定重名问题
- springMVC之事务配置(问题来源:为什么数据保存不了)
- checkbox数据回显问题
- checkbox数据回显问题
- SpringMVC返回JSON数据时日期格式化问题
- SpringMVC 中向后台传入json数据的问题
- SpringMVC返回的json数据Date(时间)格式化的问题
- springmvc的数据回显
- 关于 下拉框 数据 回显 的问题