package com.xpx.controller;

import com.xpx.exception.UsersException;
import com.xpx.pojo.Users;
import com.xpx.service.UserService;
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;

/**
 * @author Administrator
 */
@Controller
public class LoginController {
    @Autowired
    private UserService userService;

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