UserController.java
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>(); 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"; // } }
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; } }
jsp/user/add.jsp
<%@ 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
<%@ 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>
hello-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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <mvc:annotation-driven/> <context:component-scan base-package="org.common.controller"></context:component-scan> <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>hello</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>hello</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>
时间: 2024-11-04 15:59:41