建立一个动态web项目,起名为SpringMVC_crud
导包,其中包括jstl的一些包等
1.先写一个User.java,是用户类
文件User.java文件
package org.common.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 email; private String nickname; public User() { super(); } public User(String username, String password, String email, String nickname) { super(); this.username = username; this.password = password; this.email = email; this.nickname = nickname; } @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; } }
2.再创建Controller控制器
文件UserController.java
add的时候有两种,第一种
//链接到add页面时候是GET请求,会访问这段代码 @RequestMapping(value="/add",method=RequestMethod.GET) //把一个对象放到了@ModelAttribute中,Model的Key就是user public String add(Model model){ //开启modeDrive model.addAttribute(new User()); return "user/add"; }
第二种
//链接到add页面时候是GET请求,会访问这段代码 @RequestMapping(value="/add",method=RequestMethod.GET) //把一个对象放到了@ModelAttribute中,Model的Key就是user public String add(@ModelAttribute("user") User user){ //开启modeDrive //model.addAttribute(new User()); return "user/add"; }
package org.common.controller; import java.util.HashMap; import java.util.Map; import org.common.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>(); //创建一个users对象 public UserController() { users.put("ldh",new User("ldh","刘德华","123","123123")); users.put("zxy",new User("zxy","张学友","123","123123")); users.put("gfc",new User("gfc","郭富城","123","123123")); users.put("lm",new User("lm","黎明","123","123123")); } @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) //把一个对象放到了@ModelAttribute中,Model的Key就是user public String add(@ModelAttribute("user") User user){ //开启modeDrive //model.addAttribute(new User()); return "user/add"; } //在具体添加用户的时候,是POST请求,就访问以下代码 @RequestMapping(value="/add",method=RequestMethod.POST) public String add(@Validated User user,BindingResult br){//一定要紧跟@Validated之后写验证结果类 if(br.hasErrors()){ //如果有错误,直接跳转到add视图 return "user/add"; } users.put(user.getUsername(),user); return "redirect:/user/users"; } // //链接到add页面时候是GET请求,会访问这段代码 // @RequestMapping(value="/add",method=RequestMethod.GET) // public String add(Model model){ // //开启modeDrive // model.addAttribute(new User()); // return "user/add"; // } // // //在具体添加用户的时候,是POST请求,就访问以下代码 // @RequestMapping(value="/add",method=RequestMethod.POST) // public String add(User user){ // users.put(user.getUsername(),user); // return "redirect:/user/users"; // } }
jsp/user/add.jsp
http://localhost:8080/springmvc_crud/user/add
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="sf" 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>Insert title here</title> </head> <body> <sf:form method="post" modelAttribute="user"> <table width="700" align="center" border="1"> <tr> <td>用户名:</td><td><sf:input path="username"/><sf:errors path="username"/></td> </tr> <tr> <td>用户密码:</td><td><sf:password path="password"/><sf:errors path="password"/></td> </tr> <tr> <td>用户昵称:</td><td><sf:input path="nickname"/></td> </tr> <tr> <td>用户邮箱:</td><td><sf:input path="email"/><sf:errors path="email"/></td> </tr> <tr> <td colspan="2"> <input type="submit" value="用户添加"/> </td> </tr> </table> </sf:form> </body> </html>
jsp/user/list.jsp
http://localhost:8080/springmvc_crud/user/users
<%@ 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> <c:forEach items="${users}" var="um"> ${um.value.username} ----${um.value.nickname} ----${um.value.password} ----${um.value.email}<br/> </c:forEach> </body> </html>
user-servlet.xml文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <!-- 使用defaultAnnotationHandleMapping --> <context:component-scan base-package="org.common.controller"></context:component-scan> <mvc:annotation-driven></mvc:annotation-driven> <!-- InternalResourceViewResolver视图的映射关系,还有其他很多视图 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
web.xml文件
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <!-- 配置DispatchServlet,截获特定的URL请求 --> <!-- 默认自动加载/WEB-INF/simpleSpringMVC-servlet.xml --> <!-- (即<servlet-name>-servlet.xml)的Spring配置文件,启动web层的Spring容器 --> <servlet> <servlet-name>user</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>user</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 设置字符编码 --> <filter> <filter-name>CharacterFilter</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>CharacterFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>