package com.controller;

import com.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 org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 数据相应
 */

@Controller
public class DataResponseController {
    @Autowired
    UserService userService;
    // 返回 modelAndView
    @RequestMapping("/modelView1")
    public ModelAndView modelView1() {
        // 直接返回字符串
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("name","Mr.zhang");
        modelAndView.setViewName("template/index.jsp");
        return modelAndView;
    }

    // 返回 modelAndView 参数可以自动注入
    @RequestMapping("/modelView2")
    public ModelAndView modelView2(ModelAndView modelAndView) {
        // 直接返回字符串
        modelAndView.addObject("name","Mr.zhang");
        modelAndView.setViewName("template/index.jsp");
        return modelAndView;
    }

    // 可以单独设置 Model
    @RequestMapping("/modelView3")
    public String modelView3(Model model) {
        userService.saveUser();
        model.addAttribute("name","modelName");
        return "template/index.jsp";
    }



}
