SSM+AJAX+jsp实现登录功能

2021.4.13_SSM+AJAX开发,实现简单登录

  1. 建立User实体层

  2. UserMapper

    public interface UserMapper {
    
        //全查
        public List<User> selectAll();
    
        //登录
        public User loginCheck(@Param("username")String username,@Param("password")String password);
        
    }
    

    UserMapper.xml

    <?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="com.hy.mapper.UserMapper">
        <select id="selectAll" resultType="User">
            select * from user;
        </select>
    
        <select id="loginCheck" resultType="User">
            select * from user where username = #{username} and password =#{password};
        </select>
    
    </mapper>
    
    

    3.UserService

    public interface UserService {
    
        //全查
        public List<User> selectAll();
    
        //登录
        public User loginCheck(String username, String password);
    }
    
    

    UserServiceImpl

    package com.hy.service;
    
    import com.hy.entity.User;
    import com.hy.mapper.UserMapper;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import java.util.List;
    
    @Service
    public class UserServiceImpl implements UserService {
    
        @Autowired
        private UserMapper userMapper;
    
        //全查
        @Override
        public List<User> selectAll() {
            List<User> users = userMapper.selectAll();
            return users;
        }
    
        //登录
        @Override
        public User loginCheck(String username, String password) {
            return userMapper.loginCheck(username,password);
        }
    }
    

    4.UserController

    package com.hy.controller;
    
    import com.hy.entity.User;
    import com.hy.service.UserService;
    import net.minidev.json.JSONObject;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.servlet.ModelAndView;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.PrintWriter;
    import java.util.List;
    
    @Controller
    @RequestMapping("/user")
    public class UserController {
    
        @Autowired
        private UserService userService;
    
        //页面通用跳转方法
        @RequestMapping("/{page}")
        public String toPage(@PathVariable()String page){
            return page;
        }
    
        //全查
        @RequestMapping("/all")
        public ModelAndView selectAll(){
            ModelAndView mv = new ModelAndView();  //创建ModelAndView对象
            List<User> users = userService.selectAll();
            mv.addObject("li", users); //把查询出来的结果passenger存入ModelAndView对象中
            mv.setViewName("main"); //跳转main页面 本质是/WEB-INF/jsp/main.jsp
            return mv;
        }
    
        //登录
        @ResponseBody
        @RequestMapping("/loginCheck")
        public String loginCheck(String username,String password){
            User user = userService.loginCheck(username, password);
            if(user != null) {
                return "1";
            }
            else {
                return "0";//用户不存在
            }
        }
    }
    

    5.jsp页面

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>login</title>
        <link rel="stylesheet" href="${pageContext.request.contextPath}/static/bootstrap/css/bootstrap.css">
        <script src="https://cdn.bootcdn.net/ajax/libs/jquery/2.0.0/jquery.js"></script>
    
    </head>
    <body>
    <form class="form-horizontal">
        <div class="form-group">
            <label for="inputEmail3" class="col-sm-2 control-label">用户名:</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" id="inputEmail3"  name="username" >
            </div>
        </div>
        <p></p>
        <div class="form-group">
            <label for="inputPassword3" class="col-sm-2 control-label">密码:</label>
            <div class="col-sm-10">
                <input type="password" class="form-control" id="inputPassword3" name="password">
            </div>
        </div>
        <p></p>
        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
                <button type="button" class="btn btn-default" onclick="login()">Sign in</button>
            </div>
        </div>
    </form>
    </body>
    <script>
        function login() {
            var username = $("#inputEmail3").val();
            var password = $("#inputPassword3").val();
            if (username == ""){
                alert("请输入用户名");
                return false;
            }
            else if(password == ""){
                alert("请输入密码");
             return false;
            }
            $.ajax({
                type:"post",
                url:"/user/loginCheck",
                data:{username:username,password:password},
                dataType:"json",
                success:function (msg){
                    if(msg == "1"){
                        window.location.href="/user/all";
                    }
                    else if(msg == "0"){
                        alert("用户名或密码错误");
                    }
                }
            })
        }
    </script>
    </html>
    
    
  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

[猫玖]

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值