一.数据回显
提交后,如果出现错误,将刚才提交的数据回显到刚才的提交页面。
二.pojo数据回显方法
1.springmvc默认对pojo数据进行回显。
pojo数据传入controller方法后,springmvc自动将pojo数据放到request域,key等于pojo类型(首字母小写),使用@ModelAttribute指定pojo回显到页面在request中的key。
2.@ModelAttribute还可以将方法的返回值传到页面
页面上可以得到itemTypes数据。
3、最简单方法使用model,可以不用@ModelAttribute
简单类型数据回显
使用最简单方法使用model。
model.addAttribute("id", id);
@ModelAttribute使用详解:
org.springframework.web.bind.annotation.ModelAttribute注解类型将请求参数绑定到Model对象。
@ModelAttribute注解只支持一个属性value,类型为String,表示绑定的属性名称。
提示:被@ModelAttribute注释的方法会在Controller每个方法执行前被执行,因此在一个Controller映射到多个URL时,要谨慎使用。
1.@ModelAttribute(value="")注释返回具体类的方法
index.jsp
用于展示调用关于@ModelAttribute注解不同的使用方法。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<h3>测试@ModelAttribute</h3>
<a href="loginForm1">测试@ModelAttribute(value="")注释返回具体类的方法</a>
<a href="loginForm2">测试@ModelAttribute注释