基于SSM框架实现简单的登录注册的示例代码

一、环境配置工程目录

为绥宁等地区用户提供了全套网页设计制作服务,及绥宁网站建设行业解决方案。主营业务为成都网站设计、成都网站建设、绥宁网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

基于SSM框架实现简单的登录注册的示例代码

在pom.xml添加依赖


  4.0.0
  com.imooc
  userLogin
  0.0.1-SNAPSHOT
  war

  
    
      
      junit
      junit
      4.11
      test
    
    
    
    
      org.slf4j
      slf4j-api
      1.7.12
    
    
      ch.qos.logback
      logback-core
      1.1.1
    
    
    
      ch.qos.logback
      logback-classic
      1.1.1
    

    
    
      MySQL
      mysql-connector-java
      5.1.35
      runtime
    
    
      c3p0
      c3p0
      0.9.1.2
    
    
    
      org.mybatis
      mybatis
      3.3.0
    
    
    
      org.mybatis
      mybatis-spring
      1.2.3
    
    
    
      taglibs
      standard
      1.1.2
    
    
      jstl
      jstl
      1.2
    
    
      com.fasterxml.jackson.core
      jackson-databind
      2.5.4
    
    
      javax.servlet
      javax.servlet-api
      3.1.0
    
    
    
    
      org.springframework
      spring-core
      4.2.0.RELEASE
    
    
      org.springframework
      spring-beans
      4.2.0.RELEASE
    
    
      org.springframework
      spring-context
      4.2.0.RELEASE
    
    
    
      org.springframework
      spring-jdbc
      4.2.0.RELEASE
    
    
      org.springframework
      spring-tx
      4.2.0.RELEASE
    
    
    
      org.springframework
      spring-web
      4.2.0.RELEASE
    
    
      org.springframework
      spring-webmvc
      4.2.0.RELEASE
    
    
    
      org.springframework
      spring-test
      4.2.0.RELEASE
    
  
  
    userLogin
    
      
      
      
        org.apache.maven.plugins
        maven-compiler-plugin
        2.3.2
        
          1.8
          1.8
        
      
    
  

数据库驱动配置

jdbc.properties

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mysystem?useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=kongtao

spring-web.xml

<?xml version="1.0" encoding="UTF-8"?>

  
  
  
  
  
  
  
    
    
    
  
  
  

二、具体逻辑实现数据访问对象(DAO接口)

UserDao.java

package com.sy.dao;

import org.apache.ibatis.annotations.Param;

import com.sy.entity.User;

public interface UserDao {
  
  
  /**
   * 查找用户名和密码
   * @param username 登录用户名 
   * @return
   */
  User findByUsername(String username);
  /**
   * 注册用户和密码
   */
  void registerByUsernameAndPassword(@Param("username")String username,
                    @Param("password")String password);
}

UserDao.xml

<?xml version="1.0" encoding="UTF-8" ?>


  
  
  

  
  
     insert into user (username,password)
    values (#{username},#{password})
  
  

控制器

UserController.java

package com.sy.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.sy.entity.User;
import com.sy.service.UserService;

@Controller
@RequestMapping("/user")

//这里用了@SessionAttributes,可以直接把model中的user(也就key)放入其中
//这样保证了session中存在user这个对象
@SessionAttributes("user")
public class UserController {
  
  @Autowired
  private UserService userServivce;
  
  //正常访问login页面
  @RequestMapping("/login")
  public String login(){
    return "login";
  }
  
  //表单提交过来的路径
  @RequestMapping("/checkLogin")
  public String checkLogin(User user,Model model){
    //调用service方法
    user = userServivce.checkLogin(user.getUsername(), user.getPassword());
    //若有user则添加到model里并且跳转到成功页面
    if(user != null){
      model.addAttribute("user",user);
      return "success";
    }
    return "fail";
  }
  
  //测试超链接跳转到另一个页面是否可以取到session值
  @RequestMapping("/anotherpage")
  public String hrefpage(){
    
    return "anotherpage";
  }
  
  //注销方法
  @RequestMapping("/outLogin")
  public String outLogin(HttpSession session){
    //通过session.invalidata()方法来注销当前的session
    session.invalidate();
    return "login";
  }

  @RequestMapping("/regist")
  public String regist(){
    return "regist";
  }

  @RequestMapping("/doRegist")
  public String doRegist(User user,Model model){
    System.out.println(user.getUsername());
    userServivce.Regist(user);
    return "success";
  }
}

实体类

package com.sy.entity;

public class User {
  
  
  private String id;
  
  private String username;
  
  private String password;

  public String getId() {
    return id;
  }

  public void setId(String id) {
    this.id = id;
  }

  public String getUsername() {
    return username;
  }

  public void setUsername(String username) {
    this.username = username;
  }

  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }
}

相关服务

UserService.java (接口)

package com.sy.service;
import com.sy.entity.User;

//Service层接口
public interface UserService {
  
  //检验用户登录
  User checkLogin(String username,String password);
  void Regist(User user);
}

UserServiceImpl.java

package com.sy.service.Impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sy.dao.UserDao;
import com.sy.entity.User;
import com.sy.service.UserService;

@Service
public class UserServiceImpl implements UserService{

  @Autowired
  private UserDao userDao;
  
  /* 
   * 检验用户登录业务
   * 
   */

  public User checkLogin(String username, String password) {
    
    User user = userDao.findByUsername(username);
    if(user != null && user.getPassword().equals(password)){
    
      return user;
    }
    return null;
  }

  @Override
  public void Regist(User user) {

    userDao.registerByUsernameAndPassword(user.getUsername(),user.getPassword());

  }
}

其他配置文件

持久层相关配置文件 sprintg-dao.xml

<?xml version="1.0" encoding="UTF-8"?>

  
  
  
  
  
    
    
    
    
    
  
    
    
    
    
    
    
    
    
    
  
  
  
  
  
    
    
    
    
    
    
    
    
  
  
  
  
    
    
    
    
  

spring-service.xml

<?xml version="1.0" encoding="UTF-8"?>

  
  

  
  
    
    
  
  
  
  

logback.xml

<?xml version="1.0" encoding="UTF-8"?>

  
    
    
      %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
      
    
  

  
    
  

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>


  
  
    
    
    
    
    
    
   
 

JSP视图文件

<%@ page language="java" contentType="text/html; charset=utf-8"
  pageEncoding="utf-8"%>




Insert title here



  
用户名:
密码:

regist.jsp

<%--
 Created by IntelliJ IDEA.
 User: 86265
 Date: 2018/1/30
 Time: 10:32
 To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


  用户注册页面


  

注册页面


用户名
密码

success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>




Insert title here



  
welcome,${sessionScope.user.username}!
this is success page! 点我跳到另一个页面

fail.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  pageEncoding="ISO-8859-1"%>




Insert title here


    u are loser,this is a fail page!

anotherpage.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>




Insert title here



  
${sessionScope.user.username}!!!!!

三、运行结果

基于SSM框架实现简单的登录注册的示例代码

基于SSM框架实现简单的登录注册的示例代码

基于SSM框架实现简单的登录注册的示例代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


分享名称:基于SSM框架实现简单的登录注册的示例代码
网站URL:http://ybzwz.com/article/gchsdh.html