package com.bjsxt.web.controller;

import com.bjsxt.exception.UsersException;
import com.bjsxt.pojo.Users;
import com.bjsxt.service.UsersService;
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
    @Autowired
    private UsersService usersService;

    @RequestMapping("/userLogin")
    public  String userLogin(Users users, Model model, HttpServletRequest request){
        try{
            Users u=this.usersService.usersLogin(users.getUsername(),users.getUserpwd());
            HttpSession session = request.getSession();
            session.setAttribute("user",u);
        }catch (UsersException e){
            e.printStackTrace();
            model.addAttribute("msg",e.getMessage());
            return "login";
        }
        return "redirect:index";
    }
}
