SpringMVC四种响应传值的方式

1、返回值为void类型

  使用方法的参数requesr和response进行数据分享和页面跳转

 @RequestMapping("/test1")
public void test1(HttpServletRequest request, HttpServletResponse response) throws Exception{
      //通过request和response控制页面和共享数据
      request.setAttribute("msg","你好,太阳");
      request.getRequestDispatcher("views/test.jsp").forward(request,response);
}

2、返回值为ModelAndView类型

  使用控制ModelAndView对象进行数据共享和页面的跳转

@RequestMapping("/test2")
public ModelAndView test2(){
      //通过moderAndView对象控制页面和共享数据
      ModelAndView mv = new ModelAndView();
      mv.setViewName("test");
      mv.addObject("msg","你好,月亮");
      return mv;
}

3、返回值为String类型

  使用方法的参数model进行数据共享,使用返回的字符串控制页面的跳转,可以使用试图解析器自动补充前后缀

@RequestMapping("/test3")
public String test3(Model model){
      //使用model对象进行共享数据,使用返回的字符串进行控制页面
      model.addAttribute("msg","你好,星星");
      model.addAttribute(new User("qq","123"));
      return "test";
}

  当返回的字符串使用forward或redirect控制重定向或请求转发时,视图解析器将不再会添加前缀后缀。

@RequestMapping("/test4")
public String test4(Model model){
      //使用forward和redirect可以控制重定向或请求转发,此时不会再加前后缀
      model.addAttribute("msg","你好,星星");
      model.addAttribute(new User("qq","1234"));
      return "forward:views/test.jsp";
}

4、返回值为对象类型

  返回的对象可以在页面上拿到,key为返回的对象类型首字母小写,可以使用@ModelAttribute("别名")修改共享对象的key值

  返回的页面的路径为请求的路径+视图解析器拼接的后缀

@RequestMapping("/test5")
public User test5(){
      //使用访问路径名+.jsp作为返回路径
      return new User("mike","xxxx");
}

  这种方法一般与JSON连用,返回JSON格式的对象字符串

  MVC的注解解析器支持使用@RequestBody和@ResponseBody读写JSON。

  需要先导入依赖

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.9.6</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.9.6</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.6</version>
        </dependency>

  在方法上使用@ResponseBody,会将返回的对象以字符串格式返回

@Setter@Getter@ToString
@AllArgsConstructor
@NoArgsConstructor
public class Employee {
  //在转换成JSON格式时忽略该属性
   @JsonIgnore
private Integer id; private String name; //在转换成JSON格式时进行格式化 @JsonFormat(pattern = "yyyy-MM-dd") private Date birthday; //在转换成JSON格式时起别名 @JsonProperty("gender") private Boolean sex; //使返回的字符串按照自己设定的格式格式集合即可 public Map toJsonMap(){ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Map map = new HashMap(); map.put("id",this.id); map.put("name",this.name); map.put("birthday",format.format(this.birthday)); map.put("gender",this.sex); return map; } }
@RequestMapping("/test8")
@ResponseBody
public Employee test8(){
     return new Employee(1,"张三",new Date(),true);
}
@RequestMapping("/test9")
@ResponseBody
public Object test9(){
    Employee employee = new Employee(1,"张三",new Date(),true);
    return employee.toJsonMap();
}

猜你喜欢

转载自www.cnblogs.com/xfdhh/p/11517485.html