您好,登錄后才能下訂單哦!
這篇文章主要講解了“如何實現SSM登陸注冊功能”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“如何實現SSM登陸注冊功能”吧!
這幾個包是自己手動創建的,然后往里面寫代碼
package com.maike.controller; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import com.maike.dto.UserDto; import com.maike.model.User; import com.maike.service.UserService; /** * * @author tan * */ @Controller @RequestMapping("/user") public class UserController { @Autowired private UserService userService; /** * toRegister: 跳轉到注冊頁面<br/> * @return */ @RequestMapping("/toRegister") public String toRegister(){ return "register"; } /** * addUser:實現注冊 <br/> * @param userDto * @return */ @RequestMapping("/addUser") public ModelAndView addUser(UserDto userDto) { int k = userService.addUser(userDto); if(k > 0) { ModelAndView view = new ModelAndView("login"); return view; }else { ModelAndView view = new ModelAndView("register"); return view; } } /** * 登錄判斷 * @param userDto * @return */ @RequestMapping("/judgeLogin") public ModelAndView judgeLogin(UserDto userDto) { int k = userService.judgeLogin(userDto); if(k > 0) { ModelAndView view = new ModelAndView("success"); return view; }else { ModelAndView view = new ModelAndView("login"); return view; } } }
package com.maike.dto; import java.io.Serializable; public class UserDto implements Serializable { /** * */ private static final long serialVersionUID = 1L; private String userName; private String userPwd; private String reUserPwd; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserPwd() { return userPwd; } public void setUserPwd(String userPwd) { this.userPwd = userPwd; } public String getReUserPwd() { return reUserPwd; } public void setReUserPwd(String reUserPwd) { this.reUserPwd = reUserPwd; } public static long getSerialversionuid() { return serialVersionUID; } @Override public String toString() { return "UserDto [userName=" + userName + ", userPwd=" + userPwd + ", reUserPwd=" + reUserPwd + "]"; } }
UserMapper.xml
<!-- 通過用戶名查詢用戶 --> <select id="selectByName" parameterType="java.lang.String" resultMap="BaseResultMap"> SELECT <include refid="Base_Column_List" /> FROM user WHERE user_name=#{userName,jdbcType=VARCHAR} </select> <!-- 通過用戶名密碼查詢 --> <select id="selectByNamePwd" parameterType="java.lang.String" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from user where user_name=#{userName,jdbcType=VARCHAR} and user_pwd=#{userPwd,jdbcType=VARCHAR} </select> <!-- 添加用戶信息 --> <insert id="insert" parameterType="com.maike.model.User"> INSERT INTO user(user_id, user_name, user_pwd) values(#{userId,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{userPwd,jdbcType=VARCHAR}) </insert>
UserMapper.java
int insert(User user); User selectByName(String userName); User selectByNamePwd(@Param("userName") String userName,@Param("userPwd") String userPwd);
package com.maike.service; import com.maike.dto.UserDto; public interface UserService { int addUser(UserDto userDto); int judgeLogin(UserDto userDto); }
package com.maike.serviceImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.maike.dao.UserMapper; import com.maike.dto.UserDto; import com.maike.model.User; import com.maike.service.UserService; @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; /** * addUser: 添加用戶<br/> */ public int addUser(UserDto userDto) { // TODO Auto-generated method stub int k = 0; String userName = userDto.getUserName(); String userPwd = userDto.getUserPwd(); String reUserPwd = userDto.getReUserPwd(); if("" == userName || "" == userPwd|| "" == reUserPwd) { return k; } if(!userPwd.equals(reUserPwd)) { return k; } User u = userMapper.selectByName(userName); if(null != u) { return k; } User user = new User(); user.setUserName(userName); user.setUserPwd(reUserPwd); k = userMapper.insert(user); return k; } /** * 登錄判斷 */ public int judgeLogin(UserDto userDto) { // TODO Auto-generated method stub int k = 0; String userName = userDto.getUserName(); String userPwd = userDto.getUserPwd(); if("" == userName || "" == userPwd) { return 0; } User user = userMapper.selectByNamePwd(userName, userPwd); if(null != user) { k = 1; return k; } return 0; } }
JSP
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!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> <br/> <br/> <br/> <form action="addUser"> <table align = "center" > <tr> <td>用戶名</td><td><input type = "text" name = "userName"></td> </tr> <tr> <td>密碼</td><td><input type = "password" name = "userPwd"></td> </tr> <tr> <td>確認密碼</td><td><input type = "password" name = "reUserPwd"></td> </tr> <tr> <td colspan="2" align = "center"><input type = "submit" value = "注冊" style = "background-color: Cyan;color:blue"></td> </tr> </table> </form> </body> </html>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!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> <br/> <br/> <br/> <form action="user/judgeLogin"> <table align = "center"> <tr> <td>用戶名</td><td><input type = "text" name = "userName"></td> </tr> <tr> <td>密碼</td><td><input type = "password" name = "userPwd"></td> </tr> <tr> <td align = "left"><input type = "submit" value = "登錄" style = "background-color: Cyan;color:blue"></td> <td align = "right" ><button style = "background-color: Cyan;color:blue"><a href = "/SSM/user/toRegister">注冊</a></button></td> </tr> </table> </form> </body> </html>
感謝各位的閱讀,以上就是“如何實現SSM登陸注冊功能”的內容了,經過本文的學習后,相信大家對如何實現SSM登陸注冊功能這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。