1.上一篇SSM框架搭建好了之后就要开始写功能了,现在来写一个简单的登录注册功能
这几个包是自己手动创建的,然后往里面写代码
2.代码详情
1 package com.maike.controller; 2 3 import javax.servlet.http.HttpServletRequest; 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.servlet.ModelAndView; 9 10 import com.maike.dto.UserDto; 11 import com.maike.model.User; 12 import com.maike.service.UserService; 13 /** 14 * 15 * @author tan 16 * 17 */ 18 @Controller 19 @RequestMapping("/user") 20 public class UserController { 21 @Autowired 22 private UserService userService; 23 24 /** 25 * toRegister: 跳转到注册页面<br/> 26 * @return 27 */ 28 @RequestMapping("/toRegister") 29 public String toRegister(){ 30 return "register"; 31 } 32 /** 33 * addUser:实现注册 <br/> 34 * @param userDto 35 * @return 36 */ 37 @RequestMapping("/addUser") 38 public ModelAndView addUser(UserDto userDto) { 39 int k = userService.addUser(userDto); 40 if(k > 0) { 41 ModelAndView view = new ModelAndView("login"); 42 return view; 43 }else { 44 ModelAndView view = new ModelAndView("register"); 45 return view; 46 } 47 } 48 /** 49 * 登录判断 50 * @param userDto 51 * @return 52 */ 53 @RequestMapping("/judgeLogin") 54 public ModelAndView judgeLogin(UserDto userDto) { 55 int k = userService.judgeLogin(userDto); 56 if(k > 0) { 57 ModelAndView view = new ModelAndView("success"); 58 return view; 59 }else { 60 ModelAndView view = new ModelAndView("login"); 61 return view; 62 } 63 } 64 65 66 }
1 package com.maike.dto; 2 3 import java.io.Serializable; 4 5 public class UserDto implements Serializable { 6 7 /** 8 * 9 */ 10 private static final long serialVersionUID = 1L; 11 12 private String userName; 13 14 private String userPwd; 15 16 private String reUserPwd; 17 18 public String getUserName() { 19 return userName; 20 } 21 22 public void setUserName(String userName) { 23 this.userName = userName; 24 } 25 26 public String getUserPwd() { 27 return userPwd; 28 } 29 30 public void setUserPwd(String userPwd) { 31 this.userPwd = userPwd; 32 } 33 34 public String getReUserPwd() { 35 return reUserPwd; 36 } 37 38 public void setReUserPwd(String reUserPwd) { 39 this.reUserPwd = reUserPwd; 40 } 41 42 public static long getSerialversionuid() { 43 return serialVersionUID; 44 } 45 46 @Override 47 public String toString() { 48 return "UserDto [userName=" + userName + ", userPwd=" + userPwd + ", reUserPwd=" + reUserPwd + "]"; 49 } 50 51 }
UserMapper.xml
1 <!-- 通过用户名查询用户 --> 2 <select id="selectByName" parameterType="java.lang.String" resultMap="BaseResultMap"> 3 SELECT 4 <include refid="Base_Column_List" /> 5 FROM user WHERE user_name=#{userName,jdbcType=VARCHAR} 6 </select> 7 <!-- 通过用户名密码查询 --> 8 <select id="selectByNamePwd" parameterType="java.lang.String" resultMap="BaseResultMap"> 9 select 10 <include refid="Base_Column_List" /> 11 from user 12 where user_name=#{userName,jdbcType=VARCHAR} and user_pwd=#{userPwd,jdbcType=VARCHAR} 13 </select> 14 <!-- 添加用户信息 --> 15 <insert id="insert" parameterType="com.maike.model.User"> 16 INSERT INTO user(user_id, user_name, user_pwd) values(#{userId,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{userPwd,jdbcType=VARCHAR}) 17 </insert>
UserMapper.java
1 int insert(User user); 2 3 User selectByName(String userName); 4 5 User selectByNamePwd(@Param("userName") String userName,@Param("userPwd") String userPwd);
1 package com.maike.service; 2 3 import com.maike.dto.UserDto; 4 5 public interface UserService { 6 7 int addUser(UserDto userDto); 8 9 int judgeLogin(UserDto userDto); 10 11 }
1 package com.maike.serviceImpl; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.stereotype.Service; 5 6 import com.maike.dao.UserMapper; 7 import com.maike.dto.UserDto; 8 import com.maike.model.User; 9 import com.maike.service.UserService; 10 @Service 11 public class UserServiceImpl implements UserService { 12 @Autowired 13 private UserMapper userMapper; 14 15 /** 16 * addUser: 添加用户<br/> 17 */ 18 public int addUser(UserDto userDto) { 19 // TODO Auto-generated method stub 20 int k = 0; 21 String userName = userDto.getUserName(); 22 String userPwd = userDto.getUserPwd(); 23 String reUserPwd = userDto.getReUserPwd(); 24 if("" == userName || "" == userPwd|| "" == reUserPwd) { 25 return k; 26 } 27 if(!userPwd.equals(reUserPwd)) { 28 return k; 29 } 30 User u = userMapper.selectByName(userName); 31 if(null != u) { 32 return k; 33 } 34 User user = new User(); 35 user.setUserName(userName); 36 user.setUserPwd(reUserPwd); 37 k = userMapper.insert(user); 38 return k; 39 } 40 41 /** 42 * 登录判断 43 */ 44 public int judgeLogin(UserDto userDto) { 45 // TODO Auto-generated method stub 46 int k = 0; 47 String userName = userDto.getUserName(); 48 String userPwd = userDto.getUserPwd(); 49 if("" == userName || "" == userPwd) { 50 return 0; 51 } 52 User user = userMapper.selectByNamePwd(userName, userPwd); 53 if(null != user) { 54 k = 1; 55 return k; 56 } 57 return 0; 58 } 59 60 }
JSP
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 7 <title>注册页面</title> 8 </head> 9 <body> 10 <br/> 11 <br/> 12 <br/> 13 <form action="addUser"> 14 <table align = "center" > 15 <tr> 16 <td>用户名</td><td><input type = "text" name = "userName"></td> 17 </tr> 18 <tr> 19 <td>密码</td><td><input type = "password" name = "userPwd"></td> 20 </tr> 21 <tr> 22 <td>确认密码</td><td><input type = "password" name = "reUserPwd"></td> 23 </tr> 24 <tr> 25 <td colspan="2" align = "center"><input type = "submit" value = "注册" style = "background-color: Cyan;color:blue"></td> 26 </tr> 27 </table> 28 </form> 29 </body> 30 </html>
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 7 <title>登录页面</title> 8 </head> 9 <body> 10 <br/> 11 <br/> 12 <br/> 13 <form action="user/judgeLogin"> 14 <table align = "center"> 15 <tr> 16 <td>用户名</td><td><input type = "text" name = "userName"></td> 17 </tr> 18 <tr> 19 <td>密码</td><td><input type = "password" name = "userPwd"></td> 20 </tr> 21 <tr> 22 <td align = "left"><input type = "submit" value = "登录" style = "background-color: Cyan;color:blue"></td> 23 <td align = "right" ><button style = "background-color: Cyan;color:blue"><a href = "/SSM/user/toRegister">注册</a></button></td> 24 </tr> 25 </table> 26 </form> 27 </body> 28 </html>
3.最好部署到Tomcat上 运行成功