package com.example.demo.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.example.demo.domain.Person;

@Controller
@RequestMapping(value = "/return")
public class HelloController {
    
    /**
     * @Controller 返回一个页面
     *
     * Step 1: spring-boot-starter-thymeleaf
     * Step 2: 配置thymeleaf视图解析器:application.properties
     * Step 3: 设置非严格html5:net.sourceforge.nekohtml
     * Step 4: 编写此控制器
     * Step 5: 使用thymeleaf语法编写html模板,Spring 默认会去 resources 目录下 templates 目录下找
     *
     * URL: http://localhost:8080/return/hello1?name=team
     */
    @GetMapping(value = "/hello1")
    public String hello1(Model model, String name) {
        model.addAttribute("name", name);
        return "hello";
    }
    
    /**
     * @Controller +@ResponseBody 返回JSON 或 XML 形式数据
     * @Controller +@ResponseBody= @RestController
     *
     * 使用 post 请求访问 http://localhost:8080/return/hello2
     * body 中附带以下参数,后端会以json 格式将 person 对象返回
     *  
        {
            "name": "team",
            "age": 1
        }
     *
     */
    @PostMapping(value = "/hello2")
    @ResponseBody
    public Person hello2(@RequestBody Person person) {
        return person;
    }

    /**
     * Model本身不能设置页面跳转的url地址别名或者物理跳转地址,那么我们可以通过控制器方法的返回值来设置跳转url
     * 地址别名或者物理跳转地址
     *
     * @param model
     *            一个接口, 其实现类为ExtendedModelMap,继承了ModelMap类
     * @return 跳转url地址别名或者物理跳转地址
     */
    @RequestMapping(value = "/index1")
    public String index1(Model model) {
        model.addAttribute("result", "后台返回index1");
        return "result";
    }

    /**
     * ModelMap对象主要用于传递控制方法处理数据到结果页面,类似于request对象的setAttribute方法的作用。 用法等同于Model
     *
     * @param model
     * @return 跳转url地址别名或者物理跳转地址
     */

    @RequestMapping(value = "/index2")
    public String index2(ModelMap model) {
        model.addAttribute("result", "后台返回index2");
        return "result";
    }

    /**
     * ModelAndView主要有两个作用 设置转向地址和传递控制方法处理结果数据到结果页面
     * @return 返回一个模板视图对象
     */
    @RequestMapping(value = "/index3")
    public ModelAndView index3() {
        ModelAndView mv = new ModelAndView("result");
        // ModelAndView mv = new ModelAndView();
        // mv.setViewName("result");
        mv.addObject("result", "后台返回index3");
        return mv;
    }

    /**
     * map用来存储递控制方法处理结果数据,通过ModelAndView方法返回。
     * 当然Model,ModelMap也可以通过这种方法返回
     * @return 返回一个模板视图对象
     */
    @RequestMapping(value = "/index4")
    public ModelAndView index4() {
        Map<String, String> map = new HashMap<String, String>();
        map.put("result", "后台返回index4");
        return new ModelAndView("result", map);
    }
}

 

package com.example.demo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/return2")
public class DomainController {
    /**
     * @RestController 返回JSON 或 XML 形式数据
     * 访问:http://localhost:8080/return2/say
     */
    @RequestMapping(value="say", method=RequestMethod.GET)
    public String say(){
        return "ok";
    }
}

 

posted on 2019-08-26 11:04  刘达人186  阅读(296)  评论(0编辑  收藏  举报