SpringMVC中Controller類數(shù)據(jù)響應(yīng)的方法
上篇博客我們了解了請求參數(shù)的獲取,那么獲取到請求參數(shù)之后,需要對參數(shù)進行出來,然后進行數(shù)據(jù)響應(yīng)。那么這篇博客我們就來了解 Controller 類如何進行數(shù)據(jù)響應(yīng)。
1. 方法返回值類型
在 web 階段我們也了解過數(shù)據(jù)響應(yīng),我們可以簡單的將數(shù)據(jù)響應(yīng)分為:頁面跳轉(zhuǎn)和回寫數(shù)據(jù)
Controller 類的業(yè)務(wù)返回的返回值類型有很多,但歸根結(jié)底就是用于完成頁面跳轉(zhuǎn)和回寫數(shù)據(jù)。我們了解一下常用的幾個返回值類型:ModelAndView, Model,ModelMap,Map,View, String, void ,@ResponseBody,HttpHeaders
。
2. 頁面跳轉(zhuǎn)
在 SpringMVC 中完成頁面跳轉(zhuǎn)有兩種方式:直接返回字符串和返回 ModelAndView 對象
2.1 直接返回字符串
當(dāng)直接返回一個字符串時,會自動通過視圖解析器解析為物理視圖地址。
@RequestMapping("/user") public class MyController { //請求地址:localhost:8080/user/testReturnString @RequestMapping("/testReturnString") public String testReturnString() { System.out.println("MyController 的 testReturnString 方法執(zhí)行 了。。。。"); return "success.jsp"; } }
當(dāng)你的視圖不是位于 user 文件夾下時,客戶端會報 404 錯誤,因為在找不到該視圖。這種方式設(shè)置是視圖的相對地址,相對 MyController 類的請求地址,所以我們可以將其設(shè)置為絕對地址return "\success.jsp";
。
2.2 返回 ModelAndView 對象
ModelAndView 對象我們可以進行分解, Model 表示模型用于封裝數(shù)據(jù),View 表示視圖用于展示數(shù)據(jù)。 ModelAndView 對象的一些方法:
使用 ModelAndView 對象完成頁面跳轉(zhuǎn):
@RequestMapping("test01") public ModelAndView test01(){ //創(chuàng)建 modelAndView 對象 ModelAndView modelAndView =new ModelAndView(); //設(shè)置視圖名稱 modelAndView.setViewName("/user.jsp"); //設(shè)置模型數(shù)據(jù) modelAndView.addObject("user","zhangsan"); return modelAndView; }
也可以不手動創(chuàng)建 ModelAndView 對象,直接在方法上添加形參,這種方式的ModelAndView 對象創(chuàng)建實參
@RequestMapping("test02") public ModelAndView test02(ModelAndView modelAndView){ //設(shè)置視圖名稱 modelAndView.setViewName("/user"); //設(shè)置模型數(shù)據(jù) modelAndView.addObject("user","lisi"); return modelAndView; }
這兩種方式是一樣的,只不過 ModelAndView 對象的創(chuàng)建角色改變了,除了這種兩種方式還有其他方式,我們可以通過View
和Model
將 ModelAndView 對象拆分。
@RequestMapping("test03") public String test03(Model model){ model.addAttribute("user","wangwu"); return "user.jsp"; } @RequestMapping("test04") public String test04(ModelMap modelMap){ modelMap.addAttribute("user","zhaoliu"); return "user.jsp"; }
2.3 視圖前綴和后綴
在返回視圖時,我們需要給定一個視圖名,除了視圖名還需要前綴和后綴。前綴就是視圖存放的路徑,后綴就是視圖類型。而 SpringMVC 可以配置內(nèi)部資源視圖解析器,將前綴和后綴提取出來,在 SpringMVC 的配置文件中進行配置:
<!--配置內(nèi)部資源視圖解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean>
在 Controller 類的業(yè)務(wù)方法中就可以省略前綴和后綴,SpringMVC 將返回的字符串與在視圖解析器的前后綴拼接后跳轉(zhuǎn):
@RequestMapping("test01") public ModelAndView test01(){ //創(chuàng)建 modelAndView 對象 ModelAndView modelAndView =new ModelAndView(); //設(shè)置視圖名稱 modelAndView.setViewName("user"); //設(shè)置模型數(shù)據(jù) modelAndView.addObject("user","zhangsan"); return modelAndView; }
2.3 重定向和轉(zhuǎn)發(fā)
- 轉(zhuǎn)發(fā):請求轉(zhuǎn)發(fā)是指將請求再轉(zhuǎn)發(fā)到其他地址,轉(zhuǎn)發(fā)過程中使用的是同一個請求,轉(zhuǎn)發(fā)的地址欄內(nèi)容不變。重
- 定向:是指由原請求地址重新定位到某個新地址,原有的請求失效,客戶端看到的是新的請求返回的相應(yīng)結(jié)果。
在SpringMVC 中默認(rèn)是通過轉(zhuǎn)發(fā)完成跳轉(zhuǎn)的,當(dāng)然也可以設(shè)置為重定向:
//轉(zhuǎn)發(fā)到user.jsp @RequestMapping("test05") public String test05(VO vo){ return "forward:user.jsp"; } //重定向user.jsp @RequestMapping("test05") public String test05(VO vo){ return "redirect:user.jsp"; }
注意:如果在方法返回值前加 forward:或者redirect: 則SpringMVC配置文件中的自定義視圖解析器無效。return "forward:/main"表示轉(zhuǎn)發(fā)到映射名為main的controller,而return "forward:/main.jsp"表示轉(zhuǎn)發(fā)到main.jsp頁面。
在方法上只有@RequestMapping 時,無論方法返回值是什么,都需要進行跳轉(zhuǎn)。
3. 回寫數(shù)據(jù)
回寫數(shù)據(jù)也有兩種方式:直接返回字符串和返回對象或集合
3.1 直接返回字符串
Web基礎(chǔ)階段,客戶端訪問服務(wù)器端,如果想直接回寫字符串作為響應(yīng)體返回的話,只需要使用response.getWriter().print(“hello world”) 即可,所以我們通過SpringMVC框架注入的response對象,此時不需要視圖跳轉(zhuǎn),業(yè)務(wù)方法返回值為void。
@RequestMapping("test05") public void test05(HttpServletResponse response) throws IOException { response.getWriter().println("zhangsan"); }
除了這種方式,還有通過@ResponseBody
注解告知SpringMVC框架,方法返回的字符串不是跳轉(zhuǎn)是直接在http響應(yīng)體中返回:
@RequestMapping("test06") @ResponseBody //告知 SpringMVC框架 不進行視圖跳轉(zhuǎn),直接進行數(shù)據(jù)響應(yīng) public String test06() throws IOException { return "lisi"; }
在實際開發(fā)中,一般不會直接返回 “l(fā)isi” 這是類型的字符串,一般返回的是有一定格式的字符串,例如 json 格式。在返回 json 格式的字符串時,我們需要用到額外添加Jackson的jar包,在xml 文件中添加:
<!--json轉(zhuǎn)換工具jackson--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.11.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.11.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.11.1</version> </dependency>
@RequestMapping("test07") @ResponseBody //告知 SpringMVC框架 不進行視圖跳轉(zhuǎn),直接進行數(shù)據(jù)響應(yīng) public String test07() throws IOException { User user = new User(); user.setAge(18); user.setUsername("zhangsan"); ObjectMapper objectMapper = new ObjectMapper(); String json = objectMapper.writeValueAsString(user); return json; }
3.2 返回對象或集合
通過 SpringMVC 幫助我們對對象或集合進行json字符串的轉(zhuǎn)換并回寫,為處理器適配器配置消息轉(zhuǎn)換參數(shù),指定使用jackson進行對象或集合的轉(zhuǎn)換,因此需要在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>
通過配置了消息轉(zhuǎn)換器之后,我們就不需要在業(yè)務(wù)方法中進行手動轉(zhuǎn)換了:
@RequestMapping("test08") @ResponseBody //告知 SpringMVC框架 不進行視圖跳轉(zhuǎn),直接進行數(shù)據(jù)響應(yīng) public User test08() throws IOException { User user = new User(); user.setAge(18); user.setUsername("zhangsan"); return user; }
在上述方法中我們通過配置處理器映射器完成了json格式的字符串的轉(zhuǎn)換,但是這種配置方式比較繁瑣,配置代碼比較多,因此,我們可以使用mvc的注解驅(qū)動代替上述配置。使用<mvc:annotation-driven>自動加載 RequestMappingHandlerMapping(處理映射器)和RequestMappingHandlerAdapter( 處 理 適 配 器 ),可用在Spring-xml.xml配置文件中使用<mvc:annotation-driven>替代注解處理器和適配器的配置。
<!--mvc的注解驅(qū)動--> <mvc:annotation-driven/>
到此這篇關(guān)于SpringMVC中的數(shù)據(jù)響應(yīng)的文章就介紹到這了,更多相關(guān)SpringMVC數(shù)據(jù)響應(yīng)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實戰(zhàn)之晚會抽獎系統(tǒng)的實現(xiàn)
這篇文章主要介紹了如何利用Java語言編寫一個晚會抽獎系統(tǒng),文中采用到的技術(shù)有Jdbc、Servlert、JavaScript、JQuery、Ajax等,感興趣的可以學(xué)習(xí)一下2022-03-03Java實現(xiàn)List轉(zhuǎn)換為Map的方法小結(jié)
這篇文章主要為大家詳細(xì)介紹了Java實現(xiàn)List轉(zhuǎn)換為Map的一些常見的方法,文中的示例代碼講解詳細(xì),具有一定的借鑒價值,有需要的小伙伴可以參考一下2024-03-03Java中的ArrayList.trimToSize()方法詳解
這篇文章主要介紹了Java中的ArrayList.trimToSize()方法詳解,前幾天看了Java?ArrayList,沒有明白trimToSize()這個方法是什么意思,所以看了一下源碼并且debug一下自己的一個例子,明白了其中的含義,需要的朋友可以參考下2023-11-11關(guān)于kafka-consumer-offset位移問題
這篇文章主要介紹了關(guān)于kafka-consumer-offset位移問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03JAVA中String類與StringBuffer類的區(qū)別
這篇文章主要為大家詳細(xì)介紹了JAVA中String類與StringBuffer類的區(qū)別,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12