直接返回字符串:此種方式會(huì)將返回的字符串與視圖解析器的前后綴拼接后跳轉(zhuǎn)。?
返回帶有前綴的字符串:
轉(zhuǎn)發(fā): ? forward:/WEB-INF/views/index.jsp
重定向: ? redirect:/index.jsp
立即學(xué)習(xí)“Java免費(fèi)學(xué)習(xí)筆記(深入)”;
通過(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; }
直接返回字符串: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!!!"; }
開發(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; }
在方法上添加 ?@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/>
SpringMVC的數(shù)據(jù)響應(yīng)方式
1) 頁(yè)面跳轉(zhuǎn) ?????????
直接返回字符串 ?????????
通過(guò)ModelAndView對(duì)象返回
2) 回寫數(shù)據(jù) ?????????
直接返回字符串 ?????????
返回對(duì)象或集合
以上就是Java?SpringMVC數(shù)據(jù)響應(yīng)實(shí)例分析的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注php中文網(wǎng)其它相關(guān)文章!
java怎么學(xué)習(xí)?java怎么入門?java在哪學(xué)?java怎么學(xué)才快?不用擔(dān)心,這里為大家提供了java速學(xué)教程(入門到精通),有需要的小伙伴保存下載就能學(xué)習(xí)啦!
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)