package com.zl.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
//@ResponseBody
//@RestController //@Controller+@ResponseBody
public class HanderController {
    /*@RequestMapping("/hello")
    public String hello() {
        System.out.println("hello");
        return "success";

    }*/
    //如果返回值为void,此时会跳转到 prefix+uri+suffix 对应的页面
    /*@RequestMapping("/void")
    public void hello(HttpServletResponse response, HttpServletRequest request) throws Exception {   //跳转到 WEB-INF/pages/void.jsp
        System.out.println("hello-void");
        //response.getWriter().write("zs");  //加上之后,使得请求乱码了???
        response.sendRedirect("http://www.baidu.com");
        //request.getRequestDispatcher("/WEB-INF/pages/success.jsp").forward(request, response);
    }*/
    /*@RequestMapping("/mv")
    public ModelAndView mvhander() {
        System.out.println("mv");
        ModelAndView mv = new ModelAndView();  //创建一个ModelAndView
        //往ModelAndView中封装数据
        User user = new User();
        user.setName("zs");
        user.setAge(23);
        mv.addObject("user", user);

        mv.setViewName("success");  //设置视图名字。success-success.jsp
        return mv;
    }*/
    //SpringMVC自身也提供了转发和重定向,不论是转发还是重定向,返回类型都为String,但返回的String都需要在前面加上特殊字符。
    /*@RequestMapping("/mv")
    public String hander() {
        System.out.println("hello");
        //return "redirect:http://www.baidu.com";
        return "forward:/WEB-INF/pages/succ0ess.jsp";  //转发不能使用视图解析器的前缀和后缀,必须写页面完整路径.不然与return有冲突
    }*/
    /*@RequestMapping("/json")
    @ResponseBody //也可作用于类
    public User handler(@RequestBody User user) { //RequestBody-json-jackson-userduixiang
        System.out.println(user);
        user.setName("gd");
        return user;
    }*/

    /*//自定义异常
    @RequestMapping("/error")
    public String error() {
        System.out.println("hello");
        System.out.println(10/0);
        return "success";
    }*/
    @RequestMapping("hello")
    public String hello() throws InterruptedException {
        Thread.sleep(3000);
        System.out.println("目标方法");
        Thread.sleep(3000);
        return "success";
    }
}

 

posted on 2020-10-02 16:00  JustCrazy  阅读(127)  评论(0编辑  收藏  举报