package com.api.controller;

import com.api.form.LoginForm;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Controller
@RequestMapping("/ModelAndMap")
public class ModelAndMapController {
    @GetMapping("/goto")
    public String goTo(
            Map<String,Object> map,
            Model model,
            HttpServletResponse response,
            HttpServletRequest request) {
        map.put("m1","v1");
        model.addAttribute("m2","v2");
        Cookie cookie = new Cookie("cookie_key", "cookieValue");
        cookie.setDomain("localhost");
        response.addCookie(cookie);
        request.setAttribute("zhang","xiaoyu");
        return "forward:/ModelAndMap/success";
    }
    @GetMapping("/success")
    @ResponseBody
    public LoginForm success(@RequestAttribute("zhang") String zhang,
                          HttpServletRequest request) {
        LoginForm loginForm = new LoginForm();
        loginForm.setMobile("mobile");
        loginForm.setPassword("zxys");
        return loginForm;
    }
    @GetMapping("getTest")
    @ResponseBody
    public LoginForm getTest() {
        LoginForm loginForm = new LoginForm();
        loginForm.setMobile("mobile");
        loginForm.setPassword("zxys");
        return loginForm;
    }
    @PostMapping("postTest")
    @ResponseBody
    public LoginForm postTest(@RequestBody LoginForm loginForm) {
        return loginForm;
    }

}
