/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package com.cesgroup.cesmonitor.web;

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 org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.cesgroup.cesmonitor.service.IdentityService;

/**
 * LoginController负责打开登录页面(GET请求)和登录出错页面(POST请求)，
 *
 * 真正登录的POST请求由Filter完成,
 *
 */
@Controller
@RequestMapping(value = "/login")
public class LoginController {

    @Autowired
    IdentityService identityService;

    private static final String USERNAME_PARAM = "username";

    /**
     * 登录页面跳转
     *
     * @param model
     *            模型
     * @return 登录页面
     */
    @RequestMapping(method = RequestMethod.GET)
    public String login(Model model) {
        model.addAttribute("loginFlag", "1");
        return "login/login";
    }

    /**
     * 登录失败跳转
     *
     * @param userName
     *            用户名
     * @param model
     *            模型
     * @return 登录页面
     */
    @RequestMapping(method = RequestMethod.POST)
    public String fail(@RequestParam(USERNAME_PARAM) String userName, Model model) {
        model.addAttribute(USERNAME_PARAM, userName);
        return "login/login";
    }
}
