1 package com.kite.controller; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 6 import javax.validation.Valid; 7 8 import org.springframework.stereotype.Controller; 9 import org.springframework.ui.Model; 10 import org.springframework.validation.BindingResult; 11 import org.springframework.validation.annotation.Validated; 12 import org.springframework.web.bind.annotation.ModelAttribute; 13 import org.springframework.web.bind.annotation.PathVariable; 14 import org.springframework.web.bind.annotation.RequestMapping; 15 import org.springframework.web.bind.annotation.RequestMethod; 16 17 import com.kite.entity.User; 18 19 /** 20 * 简单的增删改查实现 21 * @author kite 22 * 23 */ 24 @Controller 25 @RequestMapping(value={"/user"}) 26 public class UserController 27 { 28 //简单数据模拟 29 Map<String, User> map = new HashMap<String, User>(); 30 public UserController() 31 { 32 map.put("zs", new User("zs", "123", "张三", "123dsad456")); 33 map.put("ll", new User("ll", "321", "李四", "123dsa456")); 34 map.put("ww", new User("ww", "32154", "王五", "dsa")); 35 map.put("zl", new User("zl", "5dsada", "赵六", "dsadsa")); 36 } 37 /** 38 * 显示列表 39 * @param model 40 * @return 41 */ 42 @RequestMapping({"/list"}) 43 public String list(Model model) 44 { 45 model.addAttribute("users", map); 46 return "user/list"; 47 } 48 /** 49 * 到添加页面使用method请求的方式来区分 50 * @return 51 */ 52 @RequestMapping(value={"/add"},method=RequestMethod.GET) 53 public String add(User user) 54 { 55 return "/user/add"; 56 } 57 /** 58 * post请求方式实现真正的添加 59 * 使用bean validation 60 * BindingResult br 必须在@valid后面 61 * @return 62 */ 63 @RequestMapping(value={"/add"}, method=RequestMethod.POST) 64 public String add(@Validated User user, BindingResult br) 65 { 66 if(br.hasErrors()) 67 { 68 //出错 69 return "user/add"; 70 } 71 map.put(user.getUserName(), user); 72 return "redirect:/user/list"; 73 } 74 /** 75 * 查看 76 * @param userName 77 * @param model 78 * @return 79 */ 80 @RequestMapping({"/{userName}/show"}) 81 public String show(@PathVariable String userName, Model model) 82 { 83 User user = map.get(userName); 84 model.addAttribute(user); 85 return "/user/show"; 86 } 87 /** 88 * 到修改页面 89 * @param userName 90 * @return 91 */ 92 @RequestMapping(value={"/{userName}/update"},method=RequestMethod.GET) 93 public String update(@PathVariable @ModelAttribute(value="userName") String userName, Model model) 94 { 95 User user = map.get(userName); 96 model.addAttribute(user); 97 return "/user/update"; 98 } 99 /** 100 * 真正的更新 101 * @param userName 102 * @param user 103 * @param br 104 * @return 105 */ 106 @RequestMapping(value={"/{userName}/update"},method=RequestMethod.POST) 107 public String update(@PathVariable String userName, @Valid User user, BindingResult br) 108 { 109 map.put(userName, user); 110 return "redirect:/user/list"; 111 } 112 /** 113 * 删除的实现 114 * @param userName 115 * @return 116 */ 117 @RequestMapping(value={"/{userName}/delete"},method=RequestMethod.GET) 118 public String delete(@PathVariable String userName) 119 { 120 map.remove(userName); 121 return "redirect:/user/list"; 122 } 123 } 使用到bean validation的类 注解放在get方法只上
1 package com.kite.entity; 2 3 4 import javax.validation.constraints.Size; 5 6 import org.hibernate.validator.constraints.Email; 7 import org.hibernate.validator.constraints.NotEmpty; 8 import org.hibernate.validator.constraints.Range; 9 10 public class User 11 { 12 private String userName; 13 private String password; 14 private String nickName; 15 private String email; 16 public User() 17 { 18 super(); 19 // TODO Auto-generated constructor stub 20 } 21 public User(String userName, String password, String nickName, String email) 22 { 23 super(); 24 this.userName = userName; 25 this.password = password; 26 this.nickName = nickName; 27 this.email = email; 28 } 29 @Override 30 public String toString() 31 { 32 return "User [userName=" + userName + ", password=" + password 33 + ", nickName=" + nickName + ", email=" + email + "]"; 34 } 35 @NotEmpty(message="用户名不能为空") 36 public String getUserName() 37 { 38 return userName; 39 } 40 41 public void setUserName(String userName) 42 { 43 this.userName = userName; 44 } 45 @Size(min=6,max=18,message="密码的长度必须在6-18之间") 46 public String getPassword() 47 { 48 return password; 49 } 50 51 public void setPassword(String password) 52 { 53 this.password = password; 54 } 55 public String getNickName() 56 { 57 return nickName; 58 } 59 public void setNickName(String nickName) 60 { 61 this.nickName = nickName; 62 } 63 @Email(message="邮箱格式不符合") 64 public String getEmail() 65 { 66 return email; 67 } 68 69 public void setEmail(String email) 70 { 71 this.email = email; 72 } 73 74 75 }
其他代码就不一一复制了
时间: 2024-10-13 22:17:57