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();
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM