基于spring MVC的用户管理
以实际小项目出发来深入理解spring MVC
1.首先是User类
public class User { private int id; private String username; private String password; private String nickname; private String email; 省略构造函数和get、set方法 }
2.接着UserController的编写
几点注意:
(1).两种跳转方式,服务器端跳转(带参数) return "redirect:/user/users"; 并返回相关错误信息 和客户端跳转(理解为重定向) return "user/add";
(2).通过model对象将信息返回给视图层;
(3).get和post请求分开处理;
(4).相关的validate验证,需要导入bean-validate.jar包,下面JavaBean中细说。
package com.qzp.web; import java.util.HashMap; import java.util.Map; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import com.qzp.model.User; @Controller @RequestMapping("/user") public class UserController { private Map<String, User> users = new HashMap<String, User>(); public UserController() { users.put("sdy",new User(1, "sdy","123","宋冬野","[email protected]")); users.put("ldh",new User(2,"ldh","123","刘德华","[email protected]")); users.put("gfc",new User(3,"gfc","123","郭富城","[email protected]")); users.put("lm",new User(4,"lm","123","黎明","[email protected]")); users.put("zxy",new User(5,"zxy","123","张学友","[email protected]")); } //列表页面 @RequestMapping(value="/users",method = RequestMethod.GET) public String list(Model model){ model.addAttribute("users",users); return "user/list"; } //链接到add页面时会使用这段代码 @RequestMapping(value="/add",method = RequestMethod.GET) public String add(Model model){ //开启ModelDriven model.addAttribute(new User()); return "user/add"; } //在具体添加用户时,是post请求,就访问以下的代码 @RequestMapping(value="/add",method = RequestMethod.POST) //一定要紧跟validate写验证类 public String add(@Validated User user,BindingResult br) { //如果有错误直接跳转到add视图 if(br.hasErrors()){ return "user/add"; } users.put(user.getUsername(),user); return "redirect:/user/users"; } }
3.考虑到增加信息时,应该有一个服务器端验证,因而此处导入bean-validate.jar包,并修改user文件
package com.qzp.model; import javax.validation.constraints.Size; import org.hibernate.validator.constraints.Email; import org.hibernate.validator.constraints.NotEmpty; public class User { private int id; private String username; private String password; private String nickname; private String email; public User() { super(); } public User(int id, String username, String password, String nickname, String email) { super(); this.id = id; this.username = username; this.password = password; this.nickname = nickname; this.email = email; } public int getId() { return id; } public void setId(int id) { this.id = id; } @NotEmpty(message="用户名不能为空!") public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } @Size(min=1,max=10,message="密码大于1位小于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; } public String getNickname() { return nickname; } public void setNickname(String nickname) { this.nickname = nickname; } }
4.视图层,首先list页面
jstl标签库要引入,下面是一个迭代标签,spring MVC没有自己的一套标签库,所以这边还是用的strust2的
users是一个map对象,所以要通过他的key来获取值。
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>用户列表</title> </head> <body> <a href="add">用户添加</a><br/> <c:forEach items="${users}" var="um"> ${um.value.id }- ${um.value.username}- ${um.value.password}- ${um.value.nickname}- ${um.value.email}<br/> </c:forEach> </body> </html>
(显示用户名列表页面)执行结果如下:
5.接着是add页面
同样要导入spring的表单标签库,如果验证信息不通过,会提示信息
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="sf" uri="http://www.springframework.org/tags/form" %> <html> <head> <title>增加页面</title> </head> <body> <!-- 此时没有写action --> <sf:form method="post" modelAttribute="user"> username:<sf:input path="username"/><sf:errors path="username"/><br/> password:<sf:password path="password"/><sf:errors path="password"/><br/> nickname:<sf:input path="nickname"/><br/> email:<sf:input path="email"/><sf:errors path="email"/><br/> <input type="submit" value="添加用户"/> </sf:form> </body> </html>
add页面的执行结果,添加成功的话,会跳转到user/list页面之中。
时间: 2024-10-19 10:42:42