SpringBoot 项目用户登录

1、登录页面

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>登录</title>
<script th:src="@{/js/sockjs.js}"></script>
<script th:src="@{/js/stomp.js}"></script>
<script th:src="@{/js/jquery.js}"></script>
</head>
<body>
<div class="login" align="center">
    <form action="/login" method="post">
        <div>
            <span>用户名:</span><input type="text" id="userName" name="userName">
            <span>密码:</span><input type="password" id="password" name="password">
            <div th:if="${result} == '0'"><span th:text="${errorMsg}"></span></div>
            <input type="submit" value="登录">
        </div>
    </form>
</div>
</body>
</html>

2、写controller层

@Controller
public class LoginController {

    @Autowired
    private UserInfoService userInfoService;

    @RequestMapping(value = "login", method = RequestMethod.POST)
    @ResponseBody
    public String login(@RequestParam(name = "userName", required = false) String userName,
            @RequestParam(name = "password", required = false) String password, Model model, HttpSession session) {
        // @RequestParam(name = "userName", required = false)是name 而不是value
        // 是false 而不是true
        if (userName == null) {
            model.addAttribute("result", 0);
            model.addAttribute("errorMsg", "用户名不能为空");
            return "index";
        }
        if (password == null) {
            model.addAttribute("result", 0);
            model.addAttribute("errorMsg", "密码不能为空");
            return "index";
        }
        // 和数据库中的进行比对
        UserInfo userInfo = userInfoService.getUserByName(userName);
        if (userInfo != null) {
            if (password.equals(userInfo.getPassword())) {
                // 用户名和密码都正确
                model.addAttribute("result", 1);
                // 根据用户的类别跳转到相应的页面,管理员和客户
                if (userInfo.getUserState() == 1)
                    return "server/main";
                return "client/main";
            } else {
                // 用户存在但是输入的密码不正确
                model.addAttribute("result", 0);
                model.addAttribute("errorMsg", "密码不正确,请重新输入");
                // 返回到登录页面
                return "index";
            }
        }
        // 如果没有获取到相应的用户对象
        model.addAttribute("result", 0);
        model.addAttribute("errorMsg", "这个用户名不存在");
        return "index";
    }
}

3、service层

@Service
public class UserInfoServiceImpl implements UserInfoService{

    @Autowired
    private UserInfoDao userInfoDao;
    /**
     * @description 根据用户名获取用户信息
     */
    @Override
    public UserInfo getUserByName(String userName) {
        return userInfoDao.getUserByName(userName);
    }
    
}

4、dao层

@Repository(value="UserInfoDao")
public class UserInfoDaoImpl implements UserInfoDao{
    @Autowired
    private JdbcTemplate jdbcTemplate;
    String sql;

    /**
     * @description 根据用户名获取用户信息
     */
    @Override
    public UserInfo getUserByName(String userName) {
        sql = "select * from userInfo where userName = '" + userName + "'";
        List<UserInfo> userList = jdbcTemplate.query(sql, new Object[]{}, new BeanPropertyRowMapper<UserInfo>(UserInfo.class));
        if(userList !=null && userList.size() > 0) {
            return userList.get(0);
        }
        return null;
    }

}

猜你喜欢

转载自www.cnblogs.com/stujike/p/9020782.html