Java SpringMVC數(shù)據(jù)響應(yīng)超詳細(xì)講解
1)頁(yè)面跳轉(zhuǎn) ?
直接返回字符串:此種方式會(huì)將返回的字符串與視圖解析器的前后綴拼接后跳轉(zhuǎn)。?
返回帶有前綴的字符串:
轉(zhuǎn)發(fā): forward:/WEB-INF/views/index.jsp
重定向: redirect:/index.jsp
通過(guò)ModelAndView對(duì)象返回
@RequestMapping("/quick2") public ModelAndView quickMethod2(){ ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("redirect:index.jsp"); return modelAndView; } @RequestMapping("/quick3") public ModelAndView quickMethod3(){ ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("forward:/WEB-INF/views/index.jsp"); return modelAndView; }
?在進(jìn)行轉(zhuǎn)發(fā)時(shí),往往要向request域中存儲(chǔ)數(shù)據(jù),在jsp頁(yè)面中顯示,那么Controller中怎樣向request 域中存儲(chǔ)數(shù)據(jù)呢?
① 通過(guò)SpringMVC框架注入的request對(duì)象setAttribute()方法設(shè)置。
@RequestMapping("/quick") public String quickMethod(HttpServletRequest request){ request.setAttribute("name","zhangsan"); return "index"; }
② 通過(guò)ModelAndView的addObject()方法設(shè)置。
@RequestMapping("/quick3") public ModelAndView quickMethod3(){ ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("forward:/WEB-INF/views/index.jsp"); modelAndView.addObject("name","lisi"); return modelAndView; }
2)回寫數(shù)據(jù)
直接返回字符串:Web基礎(chǔ)階段,客戶端訪問(wèn)服務(wù)器端,如果想直接回寫字符串作為響應(yīng)體返回的話,只需要使用response.getWriter().print(“hello world”) 即可,那么在Controller中想直接回寫字符串該怎樣呢?
① 通過(guò)SpringMVC框架注入的response對(duì)象,使用response.getWriter().print(“hello world”) 回寫數(shù)據(jù),此時(shí)不需要視圖跳轉(zhuǎn),業(yè)務(wù)方法返回值為void。
@RequestMapping("/quick4") public void quickMethod4(HttpServletResponse response) throws IOException { response.getWriter().print("hello world"); }
② 將需要回寫的字符串直接返回,但此時(shí)需要通過(guò)@ResponseBody注解告知SpringMVC框架,方法 返回的字符串不是跳轉(zhuǎn)是直接在http響應(yīng)體中返回。
@RequestMapping("/quick5") @ResponseBody public String quickMethod5() throws IOException { return "hello springMVC!!!"; }
開(kāi)發(fā)中往往要將復(fù)雜的java對(duì)象轉(zhuǎn)換成json格式的字符串,我們可以使用web階段學(xué)習(xí)過(guò)的json轉(zhuǎn)換工具jackson進(jìn)行轉(zhuǎn)換,
1.在pom.xml中導(dǎo)入jackson坐標(biāo)。
<!--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>
2.通過(guò)jackson轉(zhuǎn)換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; }
返回對(duì)象或集合
通過(guò)SpringMVC幫助我們對(duì)對(duì)象或集合進(jìn)行json字符串的轉(zhuǎn)換并回寫,為處理器適配器配置消息轉(zhuǎn)換參數(shù), 指定使用jackson進(jìn)行對(duì)象或集合的轉(zhuǎn)換,因此需要在spring-mvc.xml中進(jìn)行如下配置:
<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>
直接在方法中返回對(duì)象或集合
@RequestMapping("/quick8") @ResponseBody public User quickMethod8() throws IOException { User user = new User(); user.setUsername("zhangsan"); user.setAge(18); return user; }
3)配置注解驅(qū)動(dòng)
在方法上添加 @ResponseBody就可以返回json格式的字符串,但是這樣配置比較麻煩,配置的代碼比較多, 因此,我們可以使用mvc的注解驅(qū)動(dòng)代替上述配置。
在 SpringMVC 的各個(gè)組件中, 處理器映射器、 處理器適配器、 視圖解析器稱為 SpringMVC 的三大組件。
使用<mvc:annotation-driven>自動(dòng)加載 RequestMappingHandlerMapping(處理映射器)和 RequestMappingHandlerAdapter(處理適配器)可用在Spring-xml.xml配置文件中使用 <mvc:annotation-driven>替代注解處理器和適配器的配置。
同時(shí)使用<mvc:annotation-driven>默認(rèn)底層就會(huì)集成jackson進(jìn)行對(duì)象或集合的json格式字符串的轉(zhuǎn)換。
<!--在spring-mvc.xml中配置mvc的注解驅(qū)動(dòng)--> <mvc:annotation-driven/>
4)知識(shí)要點(diǎn)
SpringMVC的數(shù)據(jù)響應(yīng)方式
1) 頁(yè)面跳轉(zhuǎn) ????????
- 直接返回字符串 ????????
- 通過(guò)ModelAndView對(duì)象返回
2) 回寫數(shù)據(jù) ????????
- 直接返回字符串 ????????
- 返回對(duì)象或集合
到此這篇關(guān)于Java SpringMVC數(shù)據(jù)響應(yīng)超詳細(xì)講解的文章就介紹到這了,更多相關(guān)Java SpringMVC內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot解決BigDecimal傳到前端后精度丟失問(wèn)題
這篇文章將通過(guò)示例詳細(xì)為大家介紹SpringBoot如何解決BigDecimal傳到前端后精度丟失問(wèn)題,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-06-06Java 數(shù)據(jù)結(jié)構(gòu)與算法系列精講之二叉堆
二叉堆是一種特殊的堆,其實(shí)質(zhì)是完全二叉樹(shù)。二叉堆有兩種:最大堆和最小堆。最大堆是指父節(jié)點(diǎn)鍵值總是大于或等于任何一個(gè)子節(jié)點(diǎn)的鍵值。而最小堆恰恰相反,指的是父節(jié)點(diǎn)鍵值總是小于任何一個(gè)子節(jié)點(diǎn)的鍵值2022-02-02SpringBoot?項(xiàng)目的創(chuàng)建與啟動(dòng)步驟詳解
這篇文章主要介紹了SpringBoot?項(xiàng)目的創(chuàng)建與啟動(dòng),本文分步驟給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03Windows下使用IDEA搭建Hadoop開(kāi)發(fā)環(huán)境的詳細(xì)方法
這篇文章主要介紹了Windows下使用IDEA搭建Hadoop開(kāi)發(fā)環(huán)境,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12Java?數(shù)據(jù)結(jié)構(gòu)與算法系列精講之貪心算法
我們可能在好多地方都會(huì)聽(tīng)到貪心算法這一概念,并且它的算法思想也比較簡(jiǎn)單就是說(shuō)算法只保證局部最優(yōu),進(jìn)而達(dá)到全局最優(yōu)。但我們實(shí)際編程的過(guò)程中用的并不是很多,究其原因可能是貪心算法使用的條件比較苛刻,所要解決的問(wèn)題必須滿足貪心選擇性質(zhì)2022-02-02基于application和bootstrap的加載順序及區(qū)別說(shuō)明
這篇文章主要介紹了application和bootstrap的加載順序及區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07