Spring第七--SpringMVC資料響應
1. SpringMVC的資料響應
1.1 springMVC的資料響應方式
(1)頁面跳轉
直接返回字串、通過ModelAndView物件返回
(2)回寫資料
直接返回字串、返回物件或集合
1.2 頁面跳轉
1.2.1. 返回字串形式
直接返回字串:此種方式會將返回的字串與檢視解析器的前後綴拼接後跳轉。
1.2.2. 返回ModelAndView物件
Model模型:作用封裝資料
View檢視:作用展示資料
@RequestMapping("/quick2")
public ModelAndView quickMethod2(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("redirect:index.jsp");
return modelAndView;}
//SpringMVC 解析時會把方法的形參的物件進行注入
@RequestMapping("/quick3")
public ModelAndView quickMethod3(ModelAndView modelAndView){
modelAndView.addObject("name","lisi");
modelAndView.setViewName("redirect:index.jsp");
return modelAndView;}
1.2.3. 向request域儲存資料
在進行轉發時,往往要向request域中儲存資料,在jsp頁面中顯示,那麼Controller中怎樣向request域中儲存資料呢?
①通過SpringMVC框架注入的request物件setAttribute()方法設定
@RequestMapping("/quick")
public String quickMethod(HttpServletRequest request){
request.setAttribute("name","zhangsan");
return "index";}
1.3 回寫資料
1.3.1 直接返回字串
Web基礎階段,客戶端訪問伺服器端,如果想直接回寫字串作為響應體返回的話,只需要使用response.getWriter().print(“hello world”) 即可,那麼在Controller中想直接回寫字串該怎樣呢?①通過SpringMVC框架注入的response物件,使用response.getWriter().print(“hello world”) 回寫資料,此時不需要檢視跳轉,業務方法返回值為void。
@RequestMapping("/quick4")
public void quickMethod4(HttpServletResponse response) throws IOException {response.getWriter().print("hello world");}
springMVC階段直接返回字串
@ResponseBody//告知springMVC框架,不進行頁面跳轉,直接返回字串
將需要回寫的字串直接返回,但此時需要通過@ResponseBody註解告知SpringMVC框架,方法返回的字串不是跳轉是直接在http響應體中返回。
@RequestMapping("/quick5")
@ResponseBody
public String quickMethod5() throws IOException {
return "hello springMVC!!!";}
返回json格式的字串
在非同步專案中,客戶端與伺服器端往往要進行json格式字串互動,此時我們可以手動拼接json字串返回。@RequestMapping("/quick6")
@ResponseBody
public String quickMethod6() throws IOException {return "{\"name\":\"zhangsan\",\"age\":18}";}
上述方式手動拼接json格式字串的方式很麻煩,開發中往往要將複雜的java物件轉換成json格式的字串,我們可以使用web階段學習過的json轉換工具jackson進行轉換,匯入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.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>
通過jackson轉換json格式字串,回寫字串。@RequestMapping("/quick7")
@ResponseBody
public String quickMethod7() throws IOException {User user = new User();
user.setUsername("zhangsan");user.setAge(18);
ObjectMapper objectMapper = new ObjectMapper();
String s = objectMapper.writeValueAsString(user);
return s;}
1.3.2 返回物件或集合
通過SpringMVC幫助我們對 物件或集合進行json字串的轉換並回寫,為處理器介面卡配置訊息轉換引數,指定使用jackson進行物件或集合的轉換,因此需要在spring-mvc.xml中進行如下配置:
<!—配置處理器對映器-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
</bean>
</list>
</property></bean>
//SpringMVC 自動將user物件轉化為json格式的字串
@RequestMapping("/quick8")
@ResponseBody
public User quickMethod8() throws IOException {
User user = new User();
user.setUsername("zhangsan");
user.setAge(18);
return user;}
springmvc.xml中配置處理器對映器RequestMappingHandlerAdapter,可以將返回的物件轉換為json格式的字串,但是這樣配置比較麻煩,配置的程式碼比較多,因此,我們可以使用mvc的註解驅動代替上述配置。
<!--mvc的註解驅動-->
<mvc:annotation-driven/>
在SpringMVC的各個元件中,處理器對映器、處理器介面卡、檢視解析器稱為SpringMVC的三大元件。使用<mvc:annotation-driven>自動載入RequestMappingHandlerMapping(處理對映器)和RequestMappingHandlerAdapter(處理介面卡),可用在Spring-xml.xml配置檔案中使用<mvc:annotation-driven>替代註解處理器和介面卡的配置。同時使用<mvc:annotation-driven>預設底層就會整合jackson進行物件或集合的json格式字串的轉換。