• javaweb登录 SSM框架


    同样数据库中先建表

    DROP TABLE IF EXISTS `admin`;
    CREATE TABLE `admin` (
      `adminPassword` varchar(20) DEFAULT NULL,
      `adminName` varchar(20) DEFAULT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

    下图为整体结构:

     

    要使用的包请自行百度

    package controller;
    import javax.servlet.http.HttpSession;

    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 pojo.Admin;
    import service.AdminService;@Controller
    @RequestMapping(value="/admin")
    public class AdminController {
     @Autowired
     private AdminService adminService;
     @RequestMapping(value="/login.do")
     public ModelAndView login(Admin admin, HttpSession session) {
      ModelAndView mv = new ModelAndView();
      
      
      //登录
      Admin a = adminService.login(admin);
      //登录验证
      if(a != null) { //成功
       session.setAttribute("admin", a);
       mv.setViewName("/success.jsp");
      } else { //失败
        String msg = "用户名或密码错误";
        mv.addObject("msg",msg);
        mv.setViewName("/login.jsp");
      }
      return mv;
     }
     
     @RequestMapping(value="/register.do")
     public ModelAndView register(Admin admin, HttpSession session) {
      ModelAndView mv = new ModelAndView();
      
      //登录
      
      int a = adminService.register(admin);
     
      if(a==1){
       session.setAttribute("admin",admin);
       mv.setViewName("/regsucce.jsp");
      }else {
       String msg = "注册失败,请重试!";
        mv.addObject("msg",msg);
        mv.setViewName("/reg.jsp");
      }
      return mv;
     
     }
     
     @RequestMapping(value="/logout.do")
     public ModelAndView logout(HttpSession session) {
      ModelAndView mv = new ModelAndView();
       session.removeAttribute("admin");
       mv.setViewName("/login.jsp");
      return mv;
     
     }
     
     
    }
    package mapper;
    import pojo.Admin;
    public interface AdminMapper {
     //登录
     Admin findAdminByNameAndPwd(Admin admin);
     //注册
     int register(Admin admin);
    }
     
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <!--
     使用Mapper代理方式进行开发,namespace要和Mapper接口的全类名一致
     -->
    <mapper namespace="mapper.AdminMapper">
       <select id="findAdminByNameAndPwd" parameterType="pojo.Admin" resultType="admin">
        select * from admin where adminName=#{adminName} and adminPassword=#{adminPassword}
       </select>
       <insert id="register" parameterType="pojo.Admin">
          <!--
        keyProperty:返回的主键存放在pojo对象的什么位置
        order="AFTER" 在插入操作运行完成之后执行
        resultType:返回的主键的类型
        -->
       <selectKey keyProperty="id" order="AFTER" resultType="int">
        select LAST_INSERT_ID()
       </selectKey>
       insert into admin(adminName,adminPassword) values(#{adminName},#{adminPassword})
       </insert>
    </mapper>
     
    package service;
    import pojo.Admin;
    public interface AdminService {
     Admin login(Admin admin);
     //注册
     int register(Admin admin);
    }
     
     
    package service;
    import mapper.AdminMapper;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import pojo.Admin;
    import service.AdminService;
    @Service(value="adminService")
    public class AdminServiceImpl implements AdminService{
     @Autowired
     private AdminMapper adminMapper;
     @Override
     public Admin login(Admin admin) {
      return adminMapper.findAdminByNameAndPwd(admin);
     }
     @Override
     public int register(Admin admin) {
      return adminMapper.register(admin);
     }
     
    }
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd">
     <!-- 加载配置文件 -->
     <context:property-placeholder location="classpath:db.properties"/>
     <!-- 配置数据源 -->
     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
      <property name="driverClass" value="${jdbc.driverName}"></property>  
      <property name="jdbcUrl" value="${jdbc.url}"></property>  
      <property name="user" value="${jdbc.username}"></property>  
      <property name="password" value="${jdbc.password}"></property>  
      <property name="acquireIncrement" value="${acquireIncrement}"></property>  
      <property name="initialPoolSize" value="${initialPoolSize}"></property>  
      <property name="minPoolSize" value="${minPoolSize}"></property>  
      <property name="maxPoolSize" value="${maxPoolSize}"></property>
     </bean>
     
     <!-- 配置sqlSessionFactory -->
     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
      <property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
      <property name="dataSource" ref="dataSource"></property>
     </bean>
     
     <!--
      使用包扫描的方式批量引入Mapper,扫描后引用的时候可以使用类名,首字母小写
      -->
     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
      <!-- 指定要扫描的包的全路径名称,如果有多个包用英文状态下的逗号分隔 -->
      <property name="basePackage" value="mapper"></property>
     </bean>
     
     <!--
      开启注解扫描,扫描所有service包下的注解
      -->
     <context:component-scan base-package="service"></context:component-scan>
    </beans>jdbc.driverName=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/admin
    jdbc.username=用户名
    jdbc.password=密码
    acquireIncrement=3
    initialPoolSize=10
    minPoolSize=2
    maxPoolSize=10

    log4j.rootLogger=DEBUG,stdout
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
       <!-- 配置@Controller注解扫描
     
       -->
       <context:component-scan base-package="controller"></context:component-scan>
           
    </beans>
     
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE configuration
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
     <typeAliases>
      <package name="pojo"/>
     </typeAliases>
    </configuration>
     
     

    <%@ 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>Login</title>
    </head>
    <body>
     ${msg}
     <form action="${pageContext.request.contextPath}/admin/login.do" method="post">
      用户名<input type="text" name="adminName" /><br/>
      密码<input type="password" name="adminPassword" /><br/>
      <input type="submit" value="登录" />
       <a href="${pageContext.request.contextPath}/reg.jsp">注册</a>
     </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>register</title>
    <script language="javascript" type="">
      function RegSubmit(){
       var user = document.reg.adminName.value;
       var pwd = document.reg.adminPassword.value;
       if(user==null||user==""){
        alert("请填写用户名");
    document.getElementById("adminName").focus;
       }else if(pwd==null||pwd==""){
        alert("密码不能为空");
       
       }else document.reg.submit();
       
      }
    </script>
    </head>
    <body>
      ${msg}
     <form name="reg" action="${pageContext.request.contextPath}/admin/register.do" method="post">
      用户名<input type="text" name="adminName" /><br/>
      密码<input type="password" name="adminPassword" /><br/>
      <input type="button" value="注册" onclick="RegSubmit()"/>
      
     </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>Insert title here</title>
    </head>
    <body>
       恭喜您,注册成功!<br/>
       <a href="${pageContext.request.contextPath}/login.jsp">登录</a>
    </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>Insert title here</title>
    </head>
    <body>
     恭喜您,登录成功!
     欢迎${sessionScope.admin.adminName}<br/>
     当前您的用户信息为:<br/>
     用户名:${sessionScope.admin.adminName}
     密码:${sessionScope.admin.adminPassword}
     <form action="${pageContext.request.contextPath}/admin/logout.do" method="post">
     <input type="submit" value="注销" />
     </form>
    </body>
    </html>

  • 相关阅读:
    linux下用shell删除三天前或者三天内的文件
    linux在shell中获取时间
    mongodb_修改器($inc/$set/$unset/$push/$pop/upsert......)
    java List<Map> 排序问题
    linux crontab定时执行shell脚本
    MongoDB 基础命令——数据库表的增删改查——遍历操作表中的记录
    Windows7下安装MongoDB
    JavaWeb学习总结(十二)——Session
    javaweb学习总结(十一)——使用Cookie进行会话管理
    iOS10 SiriKit QQ适配详解
  • 原文地址:https://www.cnblogs.com/Syue/p/9433847.html
Copyright © 2020-2023  润新知