1.定义一个pojo类
package com.study.domain;
import java.io.Serializable;
import java.util.Date;
@SuppressWarnings("serial")
public class User implements Serializable{
private Integer id;
private String userName;
private Integer age;
private Date birthday;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "User [id=" + id + ", userName=" + userName + ", age=" + age + ", birthday=" + birthday + "]";
}
}
2.后台获取数据
@RequestMapping("/showList")
public String showList(Model model){
User user = new User();
user.setId(1);
user.setUserName("兰陵王");
user.setAge(16);
user.setBirthday(new Date());
User user2 = new User();
user2.setId(2);
user2.setUserName("花木兰");
user2.setAge(17);
user2.setBirthday(new Date());
User user3 = new User();
user3.setId(3);
user3.setUserName("孙尚香");
user3.setAge(18);
user3.setBirthday(new Date());
List<User> list = new ArrayList<User>();
list.add(user);
list.add(user2);
list.add(user2);
model.addAttribute("userList", list);
return "list";
}
3.页面获取list循环
<body>
<table border="1px" >
<tr>
<td>id</td>
<td>姓名</td>
<td>年龄</td>
<td>生日</td>
<td>操作</td>
</tr>
<c:forEach items="${userList}" var="user">
<tr>
<td>${user.id }</td>
<td>${user.userName }</td>
<td>${user.age }</td>
<td>${user.birthday }</td>
<td>
<a href="${pageContext.request.contextPath }/user/edit.do?id=${user.id }" type="button">修改</a>
</td>
</tr>
</c:forEach>
</table>
注:页面中的 items=”${userList}”,userList需与后台存的model.addAttribute(“userList”, list);,userList一致
4.参数修改
点击修改将参数传递后台,通过id获取数据
@RequestMapping("edit")
public String edit(Integer id, Model model){
User user3 = new User();
user3.setId(id);
user3.setUserName("孙尚香");
user3.setAge(18);
user3.setBirthday(new Date());
model.addAttribute("user", user3);
return "edit";
}
5.页面书籍编辑
<body>
id:<input name="id" value="${user.id }"/><br/>
姓名:<input name="id" value="${user.userName }"/><br/>
年龄:<input name="id" value="${user.age }"/><br/>
生日:<input name="id" value="${user.birthday }"/><br/>
<a href="${pageContext.request.contextPath }/user/showList.do" type="button">返回</a>
</body>
显示效果
注:参数回显都是通过springmvc的Model对象来实现的