springMVC @ModelAttribute功能
@ModelAttribute功能:将数据模型回写到页面
如:
public String validate(@Valid @ModelAttribute("user") UserModel user, Errors errors) {
if (errors.hasErrors()) {
return "validate/error";
}
return "validate/success";
}
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<form:form commandName="user">
<form:errors path="*" cssStyle="color:red"></form:errors><br/>
</form:form>
可用Model代替:
@RequestMapping("/editItemsSubmit")
public String editItemsSubmit(Model model,HttpServletRequest request,Integer id,
@Validated MItemsCustom mItemsCustom,BindingResult bindingResult) throws Exception{
//获取校验错误信息
if(bindingResult.hasErrors()){
//输出错误信息
List<ObjectError> allErrors = bindingResult.getAllErrors();
List<String> listErrors = new ArrayList<String>();
for(ObjectError objectError:allErrors){
//输出错误信息
String strError = new String(objectError.getDefaultMessage().getBytes("ISO-8859-1"),"UTF-8");
listErrors.add(strError);
}
//将错误信息传到页面
model.addAttribute("allErrors", listErrors);
16 model.addAttribute("mItemsCustom",mItemsCustom);
//出错重新到商品修改页面
return "items/editItems";
}
//调用service更新商品信息,页面需要将商品信息传到此方法
mItemsService.updateMItems(id, mItemsCustom);
//重定向
//return "redirect:queryItems.action";
//页面转发
return "forward:queryItems.action";
}
<!-- 显示错误信息 -->
<c:if test="${allErrors!=null }">
<c:forEach items="${allErrors }" var="error">
${error}
</c:forEach>
</c:if>
springMVC @ModelAttribute功能的更多相关文章
- springMVC @ModelAttribute学习
springMVC @ModelAttribute学习 博客分类: Spring @ModelAttribute 绑定请求参数到命令对象 @ModelAttribute一个具有如下三个作用: ①绑 ...
- Spring Boot2.0+中,自定义配置类扩展springMVC的功能
在spring boot1.0+,我们可以使用WebMvcConfigurerAdapter来扩展springMVC的功能,其中自定义的拦截器并不会拦截静态资源(js.css等). @Configur ...
- SpringMVC @ModelAttribute注解
/** * 1. 有 @ModelAttribute 标记的方法, 会在每个目标方法执行之前被 SpringMVC 调用! * 2. @ModelAttribute 注解也可以来修饰 ...
- SpringMVC @ModelAttribute详解
被@ModelAttribute注释的方法会在此controller每个方法执行前被执行,因此对于一个controller映射多个URL的用法来说,要谨慎使用. 我们编写控制器代码时,会将保存方法独立 ...
- SpringMvc @ModelAttribute 的用法
参考:Spring 3.x 企业应用开发实战 第15章:SpringMvc 页码:532 ModelAttribute 从字面上解释就是模型的属性. 对于MVC框架来说是模型数据是最重要的,因为 ...
- SpringMVC学习--功能完善
简介 在基本的项目中,无非就是基本的增删改查,前面我们已经实现了一个简单的查询功能,现在来实现增删改功能,来了解实际开发中的运用,以修改功能为例,因为修改功能基本覆盖了增加和删除的运用. 前面我们实现 ...
- SpringMVC @ModelAttribute 详解
[@Controller]4 详解@ModelAttribute http://blog.sina.com.cn/s/blog_6d3c1ec601017q4p.html A.@ModelAttrib ...
- SpringMVC一些功能
1.日期格式转换 当页面提交日期格式时 默认的格式为2017/10/1 如果指定日期格式为2017-10-1 //初始化绑定日期格式--不定义初始化格式时只能默认用yyyy/MM/dd格式 @Init ...
- SpringMVC修改功能
articleList.jsp <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" ...
随机推荐
- luogu1064 金明的预算方案
这道题我就想说一点:审题!附件只有2个!钱是10的整数倍,不是100的整数倍! #include <cstdio> #include <cstring> #include &l ...
- codeforce1046 Bubble Cup 11 - Finals 题解
比赛的时候开G开了3h结果rose说一句那唯一一个AC的是羊的心态就崩了.. 这套题感觉质量挺好然后就back了下 A: AI robots 有三个限制条件:相互能够看见和智商的差.使用主席树,可以维 ...
- C# winform KeyPress 事件中对应的数字
C# winform KeyPress 事件中对应的数字所有e.KeyChar值的意思 常用ASCII码表 你自己看看应该就明白了 键盘 ASCII码 键盘 ASCII码 ESC 27 7 55 S ...
- org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.demo.pojo.IdCard
转自:https://blog.csdn.net/zheng0518/article/details/11029733 TestStudent.testSchemaExporttestSchemaEx ...
- (Go)09.指针赋值修改示例
答案: 1 package main 2 import ( 3 "fmt" 4 ) 5 6 7 func modify(p *int) { 8 fmt.Println(p) 9 ...
- input点击修改样式
<input id="geren" type="button" value="个人奖励" style="BORDER-TOP ...
- Objective-C—— Block
OC Block 其实功能就类似C语言的函数指针,js中的闭包之类的.把代码块当做一个变量就行操作,有自己的变量和作用域. 简单看一下Block的语法和可能出现的问题: Block语法: block语 ...
- Deutsch lernen (07)
1. die Einführung, -en 介绍:引言,导论 Könnten Sie uns zuerst eine kleine Einführung über das Klonen geben. ...
- SQL Server存储过程作业(二)
阶段1:练习——统计某类型客房的入住客人人数 需求说明 使用存储过程统计在指定类型的客房入住客人的总人数 提示: 存储过程的输入参数是指定的客房类型名称 USE Hotel GO --阶段1:查询入住 ...
- AI:忧郁的机器人
1.塔奇克马 塔奇克马研究起来哲学,被缴械....... 2.机器人瓦力 孤独等待EVA的瓦力 3.马文 http://www.guokr.com/post/683881/