天天看點

SpringMVC資料響應方式1. SpringMVC的資料響應方式2. 頁面跳轉3. 回寫資料4. 知識要點

1. SpringMVC的資料響應方式

1) 頁面跳轉

直接傳回字元串

通過ModelAndView對象傳回

2) 回寫資料

傳回對象或集合

2. 頁面跳轉

  1. 傳回字元串形式

直接傳回字元串:此種方式會将傳回的字元串與視圖解析器的前字尾拼接後跳轉。

SpringMVC資料響應方式1. SpringMVC的資料響應方式2. 頁面跳轉3. 回寫資料4. 知識要點
  1. 傳回ModelAndView對象
SpringMVC資料響應方式1. SpringMVC的資料響應方式2. 頁面跳轉3. 回寫資料4. 知識要點
  1. 向request域存儲資料
  • 在進行轉發時,往往要向request域中存儲資料,在jsp頁面中顯示,那麼Controller中怎樣向request域中存儲資料呢?
  • 通過SpringMVC架構注入的request對象

    setAttribute()

    方法設定
SpringMVC資料響應方式1. SpringMVC的資料響應方式2. 頁面跳轉3. 回寫資料4. 知識要點
SpringMVC資料響應方式1. SpringMVC的資料響應方式2. 頁面跳轉3. 回寫資料4. 知識要點
SpringMVC資料響應方式1. SpringMVC的資料響應方式2. 頁面跳轉3. 回寫資料4. 知識要點
  • 通過ModelAndView的addObject()方法設定
@RequestMapping(value = "/quick2")
    public ModelAndView save2(){
        /**
         * Model:模型 作用:封裝資料
         * View:視圖 作用:展示資料
         */
        ModelAndView modelAndView = new ModelAndView();
        //設定模型資料
        modelAndView.addObject("username","xdr630");
        //設定視圖名稱
        modelAndView.setViewName("success");
        return modelAndView;
    }           
SpringMVC資料響應方式1. SpringMVC的資料響應方式2. 頁面跳轉3. 回寫資料4. 知識要點
SpringMVC資料響應方式1. SpringMVC的資料響應方式2. 頁面跳轉3. 回寫資料4. 知識要點
SpringMVC資料響應方式1. SpringMVC的資料響應方式2. 頁面跳轉3. 回寫資料4. 知識要點
SpringMVC資料響應方式1. SpringMVC的資料響應方式2. 頁面跳轉3. 回寫資料4. 知識要點
  • 也可以這樣寫
SpringMVC資料響應方式1. SpringMVC的資料響應方式2. 頁面跳轉3. 回寫資料4. 知識要點
SpringMVC資料響應方式1. SpringMVC的資料響應方式2. 頁面跳轉3. 回寫資料4. 知識要點
  • 單獨把model抽取出來使用
SpringMVC資料響應方式1. SpringMVC的資料響應方式2. 頁面跳轉3. 回寫資料4. 知識要點
SpringMVC資料響應方式1. SpringMVC的資料響應方式2. 頁面跳轉3. 回寫資料4. 知識要點

3. 回寫資料

3.1 直接傳回字元串

Web基礎階段,用戶端通路伺服器端,如果想直接回寫字元串作為響應體傳回的話,隻需要使用

response.getWriter().print(“hello world”) 即可,那麼在Controller中想直接回寫字元串該怎樣呢?

  1. 通過SpringMVC架構注入的response對象,使用response.getWriter().print(“hello world”) 回寫資料,此時不需要視圖跳轉,業務方法傳回值為void。
SpringMVC資料響應方式1. SpringMVC的資料響應方式2. 頁面跳轉3. 回寫資料4. 知識要點
SpringMVC資料響應方式1. SpringMVC的資料響應方式2. 頁面跳轉3. 回寫資料4. 知識要點
  1. 将需要回寫的字元串直接傳回,但此時需要通過@ResponseBody注解告知SpringMVC架構,方法傳回的字元串不是跳轉,是直接在http響應體中傳回。
SpringMVC資料響應方式1. SpringMVC的資料響應方式2. 頁面跳轉3. 回寫資料4. 知識要點
SpringMVC資料響應方式1. SpringMVC的資料響應方式2. 頁面跳轉3. 回寫資料4. 知識要點
  • 在異步項目中,用戶端與伺服器端往往要進行json格式字元串互動,此時我們可以手動拼接json字元串傳回。
SpringMVC資料響應方式1. SpringMVC的資料響應方式2. 頁面跳轉3. 回寫資料4. 知識要點
SpringMVC資料響應方式1. SpringMVC的資料響應方式2. 頁面跳轉3. 回寫資料4. 知識要點
  • 上述方式手動拼接json格式字元串的方式很麻煩,開發中往往要将複雜的java對象轉換成json格式的字元串,可以json轉換工具jackson進行轉換,導入jackson坐标。
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.9.0</version>
</dependency>

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

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.9.0</version>
</dependency>           
  • 通過jackson轉換json格式字元串,回寫字元串。
SpringMVC資料響應方式1. SpringMVC的資料響應方式2. 頁面跳轉3. 回寫資料4. 知識要點
SpringMVC資料響應方式1. SpringMVC的資料響應方式2. 頁面跳轉3. 回寫資料4. 知識要點

3.2 傳回對象或集合

  • 通過SpringMVC幫助我們對對象或集合進行json字元串的轉換并回寫,為處理器擴充卡配置消息轉換參數,指定使用jackson進行對象或集合的轉換,是以需要在

    spring-mvc.xml

    中進行如下配置:
SpringMVC資料響應方式1. SpringMVC的資料響應方式2. 頁面跳轉3. 回寫資料4. 知識要點
SpringMVC資料響應方式1. SpringMVC的資料響應方式2. 頁面跳轉3. 回寫資料4. 知識要點
SpringMVC資料響應方式1. SpringMVC的資料響應方式2. 頁面跳轉3. 回寫資料4. 知識要點
SpringMVC資料響應方式1. SpringMVC的資料響應方式2. 頁面跳轉3. 回寫資料4. 知識要點
  • 在方法上添加

    @ResponseBody

    就可以傳回json格式的字元串,但是這樣配置比較麻煩,配置的代碼比較多,是以,我們可以使用mvc的注解驅動代替上述配置。
<!--mvc的注解驅動-->
<mvc:annotation-driven/>           
SpringMVC資料響應方式1. SpringMVC的資料響應方式2. 頁面跳轉3. 回寫資料4. 知識要點
SpringMVC資料響應方式1. SpringMVC的資料響應方式2. 頁面跳轉3. 回寫資料4. 知識要點
  • 在 SpringMVC 的各個元件中,處理器映射器、處理器擴充卡、視圖解析器稱為 SpringMVC 的三大元件。
  • 使用

    <mvc:annotation-driven>

    自動加載 RequestMappingHandlerMapping(處理映射器)和

RequestMappingHandlerAdapter( 處 理 适 配 器 ),可用在Spring-xml.xml配置檔案中使用

<mvc:annotation-driven>

替代注解處理器和擴充卡的配置。

  • 同時使用

    <mvc:annotation-driven>

    預設底層就會內建jackson進行對象或集合的json格式字元串的轉換。

4. 知識要點

SpringMVC的資料響應方式