## 控制器层
需求分析:
访问路径:`/user/reg.do`
请求参数:`username=xx&password=xx&&phone=xx&email=xx`
请求类型:`POST`
响应内容:`JSON`
1 在`pom.xml`中添加对`Jackson`的依赖: 2 3 <!-- jackson --> 4 <dependency> 5 <groupId>com.fasterxml.jackson.core</groupId> 6 <artifactId>jackson-databind</artifactId> 7 <version>2.9.6</version> 8 </dependency>
创建专门用于响应给客户端的JSON数据的类`com.demo.pojo.ResponseResult`
1 package com.demo.pojo; 2 3 import java.io.Serializable; 4 5 public class ResponseResult<T> implements Serializable { 6 7 /** 8 * 9 */ 10 private static final long serialVersionUID = -8070114279101787255L; 11 12 private static final int STATE_SUCCESS = 1; 13 private static final int STATE_ERROR = 0; 14 15 private Integer state;// 成功(1)或失败(0) 16 private String message;// 当失败时封装错误信息 17 private T data;// 数据 18 19 public ResponseResult() { 20 super(); 21 // TODO Auto-generated constructor stub 22 } 23 24 public ResponseResult(Integer state) { 25 super(); 26 this.state = state; 27 } 28 29 public ResponseResult(Integer state, String message) { 30 super(); 31 this.state = state; 32 this.message = message; 33 } 34 35 /** 36 * 状态成功 37 * 38 * @param state 1 39 * @param data 数据 40 */ 41 public ResponseResult(Integer state, T data) { 42 super(); 43 this.state = STATE_SUCCESS; 44 this.data = data; 45 } 46 47 /** 48 * 状态失败 49 * 50 * @param throwable 返回异常 51 */ 52 public ResponseResult(Throwable throwable) { 53 super(); 54 this.state = STATE_ERROR; 55 this.message = throwable.getMessage(); 56 } 57 58 public Integer getState() { 59 return state; 60 } 61 62 public void setState(Integer state) { 63 this.state = state; 64 } 65 66 public String getMessage() { 67 return message; 68 } 69 70 public void setMessage(String message) { 71 this.message = message; 72 } 73 74 public T getData() { 75 return data; 76 } 77 78 public void setData(T data) { 79 this.data = data; 80 } 81 82 public static long getSerialversionuid() { 83 return serialVersionUID; 84 } 85 86 public static int getStateSuccess() { 87 return STATE_SUCCESS; 88 } 89 90 public static int getStateError() { 91 return STATE_ERROR; 92 } 93 94 @Override 95 public String toString() { 96 return "ResponseResult [state=" + state + ", message=" + message + ", data=" + data + "]"; 97 } 98 99 }
## 在IUserService业务层添加登陆接口
1 /** 2 * 用户登录 3 * @param username 用户名 4 * @param password 密码 5 * @return 成功登录的用户名 6 * @throws UsernameNotExistsException 7 * @throws PasswordNotMatchException 8 */ 9 User login(String username,String password);
在UserServiceImpl类实现
1 public User login(String username, String password) { 2 // 根据用户名查询用户信息 3 User user = findUserByUsername(username); 4 // 判断是否查询到匹配的用户信息 5 if (user == null) { 6 // 没有则抛出异常,用户名不存在UsernameNotExistsException 7 throw new UsernameNotExistsException("尝试登陆的用户名" + username + "不存在!"); 8 } else { 9 // 获取该用户的盐(UUID) 10 String salt = user.getUuid(); 11 // 基于盐,对用于输入的密码(方法的参数)进行加密 12 String md5Password = getEncrpytedPassword(password, salt); 13 // 判断加密后的密码,与获取的用户信息中的密码是否匹配 14 if (user.getPassword().equals(md5Password)) { 15 // 返回查询到的User对象 16 return user; 17 } else { 18 // 不匹配则抛出异常,密码错误PasswordNotMatchException 19 throw new PasswordNotMatchException("您输入的用户名或密码不正确"); 20 } 21 } 22 }
创建控制器类`com.demo.controller.UserController`,使用`@Controller`注解,并添加`@RequestMapping("/user")`注解。
在这个类中添加`public ResponseResult<Void> handleReg()`方法以处理请求,该方法使用`@RequestMapping(value="/reg.do",method=RequestMethod.POST)`注解,并添加`@ResponseBody`注解。
1 package com.demo.controller; 2 3 import javax.servlet.http.HttpSession; 4 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Controller; 7 import org.springframework.web.bind.annotation.RequestMapping; 8 import org.springframework.web.bind.annotation.RequestMethod; 9 import org.springframework.web.bind.annotation.ResponseBody; 10 11 import com.demo.pojo.ResponseResult; 12 import com.demo.pojo.User; 13 import com.demo.service.IUserService; 14 import com.demo.service.ex.ServiceException; 15 import com.demo.service.ex.UsernameNotExistsException; 16 17 @Controller 18 @RequestMapping("/user") 19 public class UserController { 20 @Autowired 21 private IUserService userService; 22 23 // 访问注册页面 24 @RequestMapping("/reg.do") 25 public String showReg() { 26 return "reg"; 27 } 28 29 // 访问登录页面 30 @RequestMapping("login.do") 31 public String showLogin() { 32 return "login"; 33 } 34 35 /** 36 * 将java数据转换为JSON串类型 37 * 38 * @param username 39 * @param password 40 * @param phone 41 * @param email 42 * @param session 43 * @return 执行的数据 44 */ 45 //处理注册信息 46 @RequestMapping(value = "/handle_reg.do", method = RequestMethod.POST) 47 @ResponseBody 48 public ResponseResult<Void> handleReg(String username, String password, String phone, String email, 49 HttpSession session) { 50 ResponseResult<Void> rr; 51 52 User user = new User(); 53 user.setUsername(username); 54 user.setPassword(password); 55 user.setPhone(phone); 56 user.setEmail(email); 57 58 try { 59 User u = userService.reg(user); 60 //绑定信息 61 session.setAttribute("uid", u.getUsername()); 62 rr = new ResponseResult<Void>(ResponseResult.STATE_ERROR); 63 } catch (UsernameNotExistsException e) { 64 rr = new ResponseResult<Void>(e); 65 } 66 return rr; 67 } 68 /** 69 * 70 * @param username 71 * @param password 72 * @param session 73 * @return 74 */ 75 //处理登录信息 76 @RequestMapping(value = "/handle_login.do", method = RequestMethod.POST) 77 @ResponseBody 78 public ResponseResult<Void> handlerLogin(String username, String password, HttpSession session) { 79 ResponseResult<Void> rr; 80 try { 81 User user = userService.login(username, password); 82 //绑定信息 83 session.setAttribute("uid", user.getId()); 84 session.setAttribute("username", user.getUsername()); 85 rr = new ResponseResult<Void>(ResponseResult.STATE_SUCCESS); 86 } catch (ServiceException e) { 87 rr = new ResponseResult<Void>(e); 88 } 89 return rr; 90 } 91 }
编辑`/web/register.jsp`,测试完毕