demo
控制器 UserController.java:
1.获取列表:
控制器
@Autowired private UserDao userDao; /** * 路径 : user/getAll */ @RequestMapping("/getAll") public String getAll(Map<String,Object> map){ map.put("userList",userDao.findAll()); return "list"; }
视图 list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> <body> <h3>用户列表</h3> <a href="add">添加用户</a><br/> <c:if test="${empty requestScope.userList }"> <span>暂无数据</span> </c:if> <c:if test="${!empty requestScope.userList }"> <table> <tr> <td>编号</td> <td>用户名</td> <td>年龄</td> <td>修改</td> <td>删除</td> </tr> <c:forEach items="${ requestScope.userList }" var="user"> <tr> <td>${user.uid}</td> <td>${user.uname}</td> <td>${user.age}</td> <td><a href="edit?uid=${user.uid}">修改</a></td> <td><a href="del?uid=${user.uid}">删除</a></td> </tr> </c:forEach> </table> </c:if> </body> </html>
2.新增:
表单
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> <body> <h3>添加用户</h3> <form action="addPost" method="post"> 用户名:<input type="text" name="uname" value="用户1"/> <br/> 年 龄:<input type="text" name="age" value="26"/> <br/> <input type="submit" value="提交"/> </form> </body> </html>
控制器接收数据
/** * 提交新增用户的表单提交 * @param user * @return */ @RequestMapping(value="/addPost",method=RequestMethod.POST) public String addPost(User user){ userDao.add(user); return "success"; }
3.修改
控制器获取数据
/** * 展示编辑用户的页面 * @param uid * @return */ @RequestMapping("edit") public String edit(@RequestParam("uid") int uid,Map<String,Object> map){ User user = userDao.get(uid); map.put("user",user); return "edit"; }
视图
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> <body> <h3>添加用户</h3> <form action="editPost" method="post"> 编 号:${requestScope.user.uid} <br/> 用户名:<input type="text" name="uname" value="${requestScope.user.uname }"/> <br/> 年 龄:<input type="text" name="age" value="${requestScope.user.age }"/> <br/> <input type="hidden" name="uid" value="${requestScope.user.uid }"> <input type="submit" value="提交"/> </form> </body> </html>
控制器接收表单
/** * 修改用户信息的表单提交 * @param user * @return */ @RequestMapping(value="/editPost",method=RequestMethod.POST) public String editPost(User user){ userDao.edit(user); return "success"; }
4.删除
控制器
/** * 删除 * @param uid * @return */ @RequestMapping("del") public String del(@RequestParam("uid") int uid){ userDao.del(uid); return "success"; }
持久层 UserDao.java:
package com.shuoeasy.springmvc.dao; import java.util.Collection; import java.util.HashMap; import java.util.Map; import org.springframework.stereotype.Repository; import com.shuoeasy.springmvc.entity.User; @Repository public class UserDao { private static Map<Integer,User> users = null; /** * 初始化数据 */ static { users = new HashMap<Integer,User>(); users.put(1001, new User(1001,"张三",23)); users.put(1002, new User(1002,"李四",24)); users.put(1003, new User(1003,"王五",25)); users.put(1004, new User(1004,"赵六",26)); users.put(1005, new User(1005,"呵呵",27)); } private int lastId=1006; private int createLastId(){ return ++lastId; } public Collection<User> findAll(){ return users.values(); } public User get(int id){ return users.get(id); } public int add(User user){ if(user.getUid()<=0){ user.setUid(this.createLastId()); } users.put(user.getUid(), user); return 1; } public int edit(User user){ users.replace(user.getUid(), user); return 1; } public int del(int id){ users.remove(id); return 1; } }
实体 User.java:
package com.shuoeasy.springmvc.entity; public class User { private int uid; private String uname; private int age; public User(){} public User(int uid, String uname, int age) { super(); this.uid = uid; this.uname = uname; this.age = age; } public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "User [uid=" + uid + ", uname=" + uname + ", age=" + age + "]"; } }