2017.05.14-1 SpringBoot简单登录注册-登录

来源:互联网 发布:季后赛数据排名 编辑:程序博客网 时间:2024/06/05 15:23

并非是那种传授并分享知识的,只想在个人博客上把自己学的东西记录下来,也希望我记录的东西对各位看官有帮助。


这次记录的博客是上一篇的延续,点击查看上一篇。
先附上结构目录跟效果图




在上一篇的项目中只增加了controller方法和增加login.html页面

login页面

[html] view plain copy
 print?
  1. <span style="font-size:18px;"><strong><body>  
  2.     <form action="/front/addlogin" name="loginfrom" accept-charset="utf-8" method="post">  
  3.         <label class="label-tips" for="u">账号:</label>  
  4.         <input type="text" id="u" name="username" class="inputstyle"/>  
  5.         <div>  
  6.             <label class="lable-tips" for="password">密码:</label>  
  7.             <input type="password" id="password" name="password" class="inputstyle" />  
  8.         </div>  
  9.         <input type="submit" name="登录"/>  
  10.         <a href="register.html" class="zcxy" target="_blank">注册</a>  
  11.     </form>  
  12. </body></strong></span>  

controller

[html] view plain copy
 print?
  1. <span style="font-size:18px;"><strong>@Controller  
  2. @RequestMapping("/front/*")  
  3. public class IndexController {  
  4.   
  5.     @Autowired  
  6.     private UserDao userDao;  
  7.   
  8.     //index页面  
  9.     @RequestMapping("/index")  
  10.     public String index() {  
  11.         return "index";  
  12.     }  
  13.   
  14.     //注册页面  
  15.     @RequestMapping("/register")  
  16.     public String register(){  
  17.         return "register";  
  18.     }  
  19.   
  20.     //登录页面  
  21.     @RequestMapping("/login")  
  22.     public String login(){  
  23.         return "login";  
  24.     }  
  25.   
  26.     //注册方法  
  27.     @RequestMapping("/addregister")  
  28.     public String register(HttpServletRequest request){  
  29.         String username = request.getParameter("username");  
  30.         String password = request.getParameter("password");  
  31.         String password2 = request.getParameter("password2");  
  32.         if (password.equals(password2)){  
  33.             UserEntity userEntity = new UserEntity();  
  34.             userEntity.setUsername(username);  
  35.             userEntity.setPassword(password);  
  36.             userDao.save(userEntity);  
  37.             return "login";  
  38.         }else {  
  39.             return "register";  
  40.         }  
  41.     }  
  42.   
  43.     //登录方法  
  44.     @RequestMapping("/addlogin")  
  45.     public String login(HttpServletRequest request){  
  46.         String username = request.getParameter("username");  
  47.         String password = request.getParameter("password");  
  48.         UserEntity userEntity = userDao.findByUsernameAndPassword(username,password);  
  49.         String str = "";  
  50.         if (userEntity !=null){  
  51.             str = "index";  
  52.         }else {  
  53.             str = "login";  
  54.         }  
  55.         return str;  
  56.     }  
  57.   
  58. }</strong></span>  

SpringBoot最简易版本的登录注册完成

源码下载