spring mvc实现新增用户
1、先在展示页面(查询出来的结果页)添加一个连接<a href="http://t.zoukankan.com/add">添加</a>
2、在后台添加一个添加的方法,点击添加,是跳转到add.jsp页面,刚过去没数据,应该是get方法请求
@RequestMapping(value="/add",method=RequestMethod.GET) public String add(){
//这个页面点击add跳转就会报500的错,传入的是空的
return "user/add"; }
//链接到add页面时,为get请求,访问以下代码(两种方式) @RequestMapping(value="/add",method=RequestMethod.GET) /* 方式一: public String add(Model model){ model.addAttribute(new User()); return "user/add"; } */ // 方式二:把一个对象放入到model中,key 就是 user public String add(@ModelAttribute("user") User user){ //model.addAttribute(new User()); return "user/add"; }
//add方法,根据get和post请求来调用哪个方法 //提交数据的时候时post请求 //如果页面有提交到action,这边的value就是action的值 @RequestMapping(value="/add",method=RequestMethod.POST) public String add(User user){ //把user对象放进去key(id) userList.put(user.getId(), user); // return "user/userlist";//返回到逻辑视图名 //(添加后,不能把添加过的数据展示出来)服务器端行为 return "redirect:user/list";//客户端重新发送请求(把添加进去的展示出来) }
3、在add.jsp中添加form表单
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> 这句话在spring-framework-3.2.1.RELEASE-distspring-framework-3.2.1.RELEASEdocsspring-framework-referencehtmlview.html中 <body> <!-- 不指定action,那么请求提交到/add --> <!-- modelAttribute添加的对象(我们添加用户,就是添加到user中) --> <form:form modelAttribute = "user" method="post"> ID:<form:input path="id"/><br/> userName:<form:input path="userName"/><br/> password:<form:input path="password"/><br/> email:<form:input path="email"/><br/> <input type="submit" value="提交"/> </form:form> </body>
处理编码问题
<!-- 处理中文编码的filter --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
验证添加页面(注释方法)
public class User { private String id; private String userName; private String password; private String email; public User(){ } public User(String id, String userName, String password, String email) { this.id = id; this.userName = userName; this.password = password; this.email = email; } public String getId() { return id; } public void setId(String id) { this.id = id; } @NotEmpty(message="用户名不能为空") public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } @Size(min=4,max=10,message="密码长度4-10位") public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Email(message="邮箱格式不正确") public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
@NotEmpty(message="用户名不能为空") public String getUserName() { return userName; } @Size(min=4,max=10,message="密码长度4-10位") public String getPassword() { return password; } @Email(message="邮箱格式不正确") public String getEmail() { return email; }
在控制器那得加一个验证注释
@Validated User user,BindingResult bindingResult
//添加用户是,为post请求,访问以下代码 @RequestMapping(value="/add",method=RequestMethod.POST) public String add(@Validated User user,BindingResult bindingResult){//紧跟validate之后写验证结果 if(bindingResult.hasErrors()){ //若有错误,直接掉转到add 视图 return "user/add"; } userList.put(user.getUserName(), user); return "redirect:/user/userlist"; }
最后在页面上提示错误
<body> <!-- 没有写action,直接提交给/add --> <form:formmethod="post" modelAttribute="user"> UserName:<sf:input path="userName"/><form:errors path="userName"/><br/> password:<sf:password path="password"/><sf:errors path="password"/><br/> email:<sf:input path="email"/><form:errors path="email"/><br/> <input type="submit" value="保存"/> </form:form> </body>
添加的控制器(完整代码)
@Controller @RequestMapping("/user") public class UserController { private Map<String, User> userList= new HashMap<String, User>(); public UserController() { userList.put("hl", new User("hl","123456","部门经理","hanlu@bdqn.cn")); userList.put("zs", new User("zs","123456","质量经理","zhangsan@bdqn.cn")); userList.put("zw", new User("zw","123456","开发工程师","zhangwei@bdqn.cn")); userList.put("ly", new User("ly","123456","实施顾问","liyu@bdqn.cn")); } //上面的value是url,下面的userlist是视图逻辑名 @RequestMapping(value="/userlist",method=RequestMethod.GET) public String list(Model model){ model.addAttribute("userlist",userList); return "user/list";//逻辑视图名 } //链接到add页面时,为get请求,访问以下代码(两种方式) @RequestMapping(value="/adduser",method=RequestMethod.GET) /* 方式一: public String add(Model model){ model.addAttribute(new User()); return "user/add"; } */ // 方式二:把一个对象放入到model中,key 就是 user public String add(@ModelAttribute("user") User user){ //model.addAttribute(new User()); return "user/add"; } //添加用户是,为post请求,访问以下代码 @RequestMapping(value="/adduser",method=RequestMethod.POST) public String add(@Validated User user,BindingResult bindingResult){//紧跟validate之后写验证结果 if(bindingResult.hasErrors()){ //若有错误,直接掉转到add 视图 return "user/add"; } userList.put(user.getUserName(), user); return "redirect:/user/userlist";//客户端重新发送请求 } }
别忘了添加验证的3个jar包
1、在userlist.jsp的展示页面添加链接
<a href="view/${ul.value.id }">userName:${ul.value.userName }</a><br/>
2、实现view.jsp页面
<body> =============查看用户信息=========== ID:${user.id }<br/> userName:${user.userName }<br/> password:${user.password }<br/> email:${user.email }<br/> </body>
在userController中再加上如下代码:实现查看每条记录
//地址中的参数用{id}来写 //REST风格 是/user/list/{id} 网页那是?id=3 @RequestMapping(value="/view/{id}",method=RequestMethod.GET) public String view(@PathVariable String id,Model model){ //userlist。get就是拿到key(id)的值 model.addAttribute(userList.get(id)); return "user/view"; }
增加后的查看view.jsp