增删改查的例子
3.添加用户
4.JSR303引入
5.修改、查看、删除
3.添加用户
UserController
package controller; import java.util.HashMap; import java.util.Map; import model.User; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller @RequestMapping("/user" ) public class UserController { private Map<String,User> users = new HashMap<String,User>(); public UserController() { users.put("haha" ,new User("haha", "123","哈哈" ,"sss" )); users.put("hehe" ,new User("haha", "123","呵呵 " ,"sss" )); users.put("nimei" ,new User("haha","123" ,"你妹" ,"sss" )); users.put("zhangquandan" ,new User("haha","张全蛋" ,"哈哈" ,"sss" )); } @RequestMapping(value= "/users",method=RequestMethod.GET) public String list(Model model){ model.addAttribute("users" ,users ); return "user/list" ; } //链接到add页面时是GET请求,会访问这段代码 @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) public String add(User user){ users.put(user .getUserName(), user); //UrlBasedViewResolver return "redirect:/user/users" ; } }
UserController
第二种方式
package controller; import java.util.HashMap; import java.util.Map; import model.User; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller @RequestMapping("/user" ) public class UserController { private Map<String,User> users = new HashMap<String,User>(); public UserController() { users.put("haha" ,new User("haha", "123","哈哈" ,"sss" )); users.put("hehe" ,new User("haha", "123","呵呵 " ,"sss" )); users.put("nimei" ,new User("haha","123" ,"你妹" ,"sss" )); users.put("zhangquandan" ,new User("haha","张全蛋" ,"哈哈" ,"sss" )); } @RequestMapping(value= "/users",method=RequestMethod.GET) public String list(Model model){ model.addAttribute("users" ,users ); return "user/list" ; } //链接到add页面时是GET请求,会访问这段代码 // @RequestMapping(value="/add",method=RequestMethod.GET) // public String add(Model model){ // //开启modelDriven // model.addAttribute(new User()); // return "user/add"; // } @RequestMapping(value= "/add",method=RequestMethod.GET) public String add( @ModelAttribute("user" ) User user){ return "user/add" ; } //在具体添加用户时,是post请求时,就访问以下代码 @RequestMapping(value= "/add",method=RequestMethod.POST) public String add(User user,String abc ){ users.put(user .getUserName(), user); //UrlBasedViewResolver return "redirect:/user/users" ; } }
User
package model; public class User { private String userName; private String password; private String nickName; private String email; public String getUserName() { return userName ; } public void setUserName(String userName ) { this.userName = userName; } public String getPassword() { return password ; } public void setPassword(String password ) { this.password = password; } public String getNickName() { return nickName ; } public void setNickName(String nickName ) { this.nickName = nickName; } public String getEmail() { return email ; } public void setEmail(String email ) { this.email = email; } public User() { } public User(String userName, String password , String nickName, String email ) { this.userName = userName; this.password = password; this.nickName = nickName; this.email = email; } }
web.xml
<?xml version="1.0" encoding= "UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name >SpringMvc1</ display-name> <servlet > <servlet-name> spring</servlet-name > <servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class > <load-on-startup> 1</ load-on-startup> </servlet > <servlet-mapping > <servlet-name> spring</servlet-name > <url-pattern> /</ url-pattern> </servlet-mapping > <!-- 字符过滤器 --> <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 > </web-app>
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 PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" > <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title> 用户列表</title > </head> <body> <a href="add" >添加</ a> <br/> <c:forEach var="user" items=" ${users }"> ${user.value.userName } -- ${user.value.password } -- ${user.value.nickName } -- ${user.value.email } <br/> </c:forEach > </body> </html>
add.jsp
<%@ page language= "java" contentType ="text/html; charset=utf-8" pageEncoding="utf-8" %> <!-- the form tag --> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" > <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title> 用户列表</title > </head> <body> <!-- 此时没有写action,直接提交会提交给/add --> <form:form method="post" modelAttribute="user" > 用户名: <form:input path="userName" /><br/> 密码: <form:password path="password" /><br/> 真实姓名: <form:input path="nickName" /><br/> Email: <form:input path="email" /><br/> <input type="submit" value="添加用户"> </form:form > </body> </html>
4.JSR 303的引入
User
package model; import javax.validation.constraints.Size; import org.hibernate.validator.constraints.Email; import org.hibernate.validator.constraints.NotEmpty; public class User { private String userName; private String password; private String nickName; private String email; @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; } public String getNickName() { return nickName ; } public void setNickName(String nickName ) { this.nickName = nickName; } @Email(message= "邮箱的格式不正确" ) public String getEmail() { return email ; } public void setEmail(String email ) { this.email = email; } public User() { } public User(String userName, String password , String nickName, String email ) { this.userName = userName; this.password = password; this.nickName = nickName; this.email = email; } }
UserController
package controller; import java.util.HashMap; import java.util.Map; import model.User; 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.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller @RequestMapping("/user" ) public class UserController { private Map<String,User> users = new HashMap<String,User>(); public UserController() { users.put("haha" ,new User("haha", "123","哈哈" ,"sss" )); users.put("hehe" ,new User("haha", "123","呵呵 " ,"sss" )); users.put("nimei" ,new User("haha","123" ,"你妹" ,"sss" )); users.put("zhangquandan" ,new User("haha","张全蛋" ,"哈哈" ,"sss" )); } @RequestMapping(value= "/users",method=RequestMethod. GET) public String list(Model model){ model.addAttribute("users" ,users ); return "user/list" ; } //链接到add页面时是GET请求,会访问这段代码 // @RequestMapping(value="/add",method=RequestMethod.GET) // public String add(Model model){ // //开启modelDriven // model.addAttribute(new User()); // return "user/add"; // } @RequestMapping(value= "/add",method=RequestMethod. GET) public String add( @ModelAttribute("user" ) User user){ return "user/add" ; } //在具体添加用户时,是post请求时,就访问以下代码 @RequestMapping(value= "/add",method=RequestMethod. POST) public String add( @Validated User user,BindingResult br ){//一定要紧跟validated之后写验证结果类 if(br .hasErrors()) { //如果有错误直接返回页面 return "user/add" ; } users.put(user .getUserName(), user); //UrlBasedViewResolver接口 return "redirect:/user/users" ; } }
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 PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" > <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title> 用户列表</title > </head> <body> <a href="add" >添加</ a> <br/> <c:forEach var="user" items=" ${users }"> ${user.value.userName } -- ${user.value.password } -- ${user.value.nickName } -- ${user.value.email } <br/> </c:forEach > </body> </html>
add.jsp
<%@ page language= "java" contentType ="text/html; charset=utf-8" pageEncoding="utf-8" %> <!-- the form tag --> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" > <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title> 用户列表</title > </head> <body> <!-- 此时没有写action,直接提交会提交给/add --> <form:form method="post" modelAttribute="user" > 用户名: <form:input path="userName" /><form:errors path="userName" ></form:errors>< br/> 密码: <form:password path="password" /><form:errors path="password" ></form:errors>< br/> 真实姓名: <form:input path="nickName" /><br/> Email: <form:input path="email" /><form:errors path="email" ></form:errors>< br/> <input type="submit" value="添加用户"> </form:form > </body> </html>
源码:http://pan.baidu.com/s/1ntE6Lwl
5.修改,查看,删除
UserController
package controller; import java.util.HashMap; import java.util.Map; import model.User; 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.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller @RequestMapping ("/user" ) public class UserController { private Map<String,User> users = new HashMap<String,User>(); public UserController() { users .put("haha" , new User("haha" ,"haha" ,"哈哈" ,"sss" )); users .put("hehe" , new User("hehe" ,"hehe" ,"呵呵 " ,"sss" )); users .put("nimei" , new User("nimei" ,"nimei" ,"你妹" ,"sss" )); users .put("zhangquandan" , new User("zhangquandan" ,"张全蛋" ,"哈哈" ,"sss" )); } /** * 遍历用户 * @param model * @return */ @RequestMapping(value= "/users" ,method=RequestMethod. GET) public String list(Model model ){ model .addAttribute("users" ,users ); return "user/list" ; } //链接到add页面时是GET请求,会访问这段代码 // @RequestMapping(value="/add",method=RequestMethod.GET) // public String add(Model model){ // //开启modelDriven // model.addAttribute(new User()); // return "user/add"; // } /** * 进入添加页面 * @param user * @return */ @RequestMapping(value= "/add" ,method=RequestMethod. GET) public String add( @ModelAttribute ("user" ) User user ){ return "user/add" ; } /** * 添加用户 * @param user * @param br * @return */ //在具体添加用户时,是post请求时,就访问以下代码 @RequestMapping(value= "/add" ,method=RequestMethod. POST) public String add( @Validated User user ,BindingResult br ){//一定要紧跟validated之后写验证结果类 if (br .hasErrors()) { //如果有错误直接返回页面 return "user/add" ; } users .put(user .getUserName(), user); //UrlBasedViewResolver接口 return "redirect:/user/users" ; } /** * 查看用户详细信息 * @param userName * @param model * @return */ @RequestMapping(value= "/{userName}" ,method=RequestMethod. GET) public String show( @PathVariable String userName ,Model model ) { model .addAttribute(users .get( userName)); return "user/show" ; } /** * 进入修改页面 * @param userName * @param model * @return */ @RequestMapping(value= "/{userName}/update" ,method=RequestMethod. GET) public String update( @PathVariable String userName ,Model model ) { model .addAttribute(users .get( userName)); return "user/update" ; } /** * 修改用户数据 * @param user * @param userName * @param br * @return */ @RequestMapping(value= "/{userName}/update" ,method=RequestMethod. POST) public String udpate( @PathVariable String userName ,@Validated Useruser,BindingResult br) { if (br .hasErrors()) { return "user/update" ; } users .put(userName ,user ); return "redirect:/user/users" ; } @RequestMapping(value= "/{userName}/delete" ,method=RequestMethod. GET) public String delete( @PathVariable String userName ) { users .remove(userName ); return "redirect:/user/users" ; } }
User
package model; import javax.validation.constraints.Size; import org.hibernate.validator.constraints.Email; import org.hibernate.validator.constraints.NotEmpty; public class User { private String userName ; private String password ; private String nickName ; private String email ; @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; } public String getNickName() { return nickName ; } public void setNickName(String nickName ) { this .nickName = nickName; } @Email(message= "邮箱的格式不正确" ) public String getEmail() { return email ; } public void setEmail(String email ) { this .email = email; } public User() { } public User(String userName , String password , String nickName , String email ) { this .userName = userName; this .password = password; this .nickName = nickName; this .email = email; } }
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 PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd" > < html> < head> < meta http-equiv ="Content-Type" content= "text/html; charset=utf-8"> < title> 用户列表 </title > </ head> < body> < a href ="add" > 添加</ a > < br/> <c:forEach var= "user" items =" ${users } "> ${user.value.userName } -- ${user.value.password } -- < a href =" ${user.value.userName }" >${user.value.nickName } </ a> -- ${user.value.email } < a href =" ${user.value.userName }/update" >修改 </ a> < a href =" ${user.value.userName }/delete" >删除 </ a> < br/> </c:forEach > </ body> </ html>
add.jsp
<%@ page language= "java" contentType = "text/html; charset=utf-8" pageEncoding ="utf-8" %> <!-- the form tag --> <%@ taglib prefix= "form" uri ="http://www.springframework.org/tags/form" %> <! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd" > < html> < head> < meta http-equiv ="Content-Type" content= "text/html; charset=utf-8"> < title> 用户列表 </title > </ head> < body> <!-- 此时没有写action,直接提交会提交给/add --> <form:form method= "post" modelAttribute ="user" > 用户名: < form:input path ="userName" />< form:errors path ="userName" ></ form:errors>< br /> 密码: < form:password path ="password" />< form:errors path ="password" ></ form:errors>< br /> 真实姓名: < form:input path ="nickName" />< br/> Email: < form:input path ="email" />< form:errors path ="email" ></ form:errors>< br /> < input type ="submit" value= "添加用户" > </form:form > </ body> </ html>
show.jsp
<%@ page language= "java" contentType = "text/html; charset=utf-8" pageEncoding ="utf-8" %> <!-- the form tag --> <%@ taglib prefix= "form" uri ="http://www.springframework.org/tags/form" %> <! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd" > < html> < head> < meta http-equiv ="Content-Type" content= "text/html; charset=utf-8"> < title> 用户列表 </title > </ head> < body> <!-- 此时没有写action,直接提交会提交给/add --> 用户名:${user.userName } < br/> 密码:${user.password } < br/> 真实姓名:${user.nickName } < br/> email:${user.email } < br/> </ body> </ html>
update.jsp
<%@ page language= "java" contentType = "text/html; charset=utf-8" pageEncoding ="utf-8" %> <!-- the form tag --> <%@ taglib prefix= "form" uri ="http://www.springframework.org/tags/form" %> <! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd" > < html> < head> < meta http-equiv ="Content-Type" content= "text/html; charset=utf-8"> < title> 用户列表 </title > </ head> < body> <!-- 此时没有写action,直接提交会提交给/add --> <form:form method= "post" modelAttribute ="user" > 用户名: < form:input path ="userName" />< form:errors path ="userName" ></ form:errors>< br /> 密码: < form:password path ="password" />< form:errors path ="password" ></ form:errors>< br /> 真实姓名: < form:input path ="nickName" />< br/> Email: < form:input path ="email" />< form:errors path ="email" ></ form:errors>< br /> < input type ="submit" value= "修改用户" > </form:form > </ body> </ html>
源码:http://pan.baidu.com/s/1kTkOEbX