上篇博客我們了解了請求參數(shù)的獲取,那么獲取到請求參數(shù)之后,需要對參數(shù)進(jìn)行出來,然后進(jìn)行數(shù)據(jù)響應(yīng)。那么這篇博客我們就來了解 Controller 類如何進(jìn)行數(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ù)。我們了解一下常用的幾個(gè)返回值類型:ModelAndView, Model,ModelMap,Map,View, String, void ,@ResponseBody,HttpHeaders
。
2. 頁面跳轉(zhuǎn)
在 SpringMVC 中完成頁面跳轉(zhuǎn)有兩種方式:直接返回字符串和返回 ModelAndView 對象
2.1 直接返回字符串
當(dāng)直接返回一個(gè)字符串時(shí),會(huì)自動(dòng)通過視圖解析器解析為物理視圖地址。
1
2
3
4
5
6
7
8
9
|
@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 文件夾下時(shí),客戶端會(huì)報(bào) 404 錯(cuò)誤,因?yàn)樵谡也坏皆撘晥D。這種方式設(shè)置是視圖的相對地址,相對 MyController 類的請求地址,所以我們可以將其設(shè)置為絕對地址return "\success.jsp";
。
2.2 返回 ModelAndView 對象
ModelAndView 對象我們可以進(jìn)行分解, Model 表示模型用于封裝數(shù)據(jù),View 表示視圖用于展示數(shù)據(jù)。 ModelAndView 對象的一些方法:
使用 ModelAndView 對象完成頁面跳轉(zhuǎn):
1
2
3
4
5
6
7
8
9
10
|
@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; } |
也可以不手動(dòng)創(chuàng)建 ModelAndView 對象,直接在方法上添加形參,這種方式的ModelAndView 對象創(chuàng)建實(shí)參
1
2
3
4
5
6
7
8
|
@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 對象拆分。
1
2
3
4
5
6
7
8
9
10
11
|
@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 視圖前綴和后綴
在返回視圖時(shí),我們需要給定一個(gè)視圖名,除了視圖名還需要前綴和后綴。前綴就是視圖存放的路徑,后綴就是視圖類型。而 SpringMVC 可以配置內(nèi)部資源視圖解析器,將前綴和后綴提取出來,在 SpringMVC 的配置文件中進(jìn)行配置:
1
2
3
4
5
|
<!--配置內(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):
1
2
3
4
5
6
7
8
9
10
|
@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ā)過程中使用的是同一個(gè)請求,轉(zhuǎn)發(fā)的地址欄內(nèi)容不變。重
- 定向:是指由原請求地址重新定位到某個(gè)新地址,原有的請求失效,客戶端看到的是新的請求返回的相應(yīng)結(jié)果。
在SpringMVC 中默認(rèn)是通過轉(zhuǎn)發(fā)完成跳轉(zhuǎn)的,當(dāng)然也可以設(shè)置為重定向:
1
2
3
4
5
6
7
8
9
10
11
|
//轉(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 時(shí),無論方法返回值是什么,都需要進(jìn)行跳轉(zhuǎn)。
3. 回寫數(shù)據(jù)
回寫數(shù)據(jù)也有兩種方式:直接返回字符串和返回對象或集合
3.1 直接返回字符串
Web基礎(chǔ)階段,客戶端訪問服務(wù)器端,如果想直接回寫字符串作為響應(yīng)體返回的話,只需要使用response.getWriter().print(“hello world”) 即可,所以我們通過SpringMVC框架注入的response對象,此時(shí)不需要視圖跳轉(zhuǎn),業(yè)務(wù)方法返回值為void。
1
2
3
4
|
@RequestMapping ( "test05" ) public void test05(HttpServletResponse response) throws IOException { response.getWriter().println( "zhangsan" ); } |
除了這種方式,還有通過@ResponseBody
注解告知SpringMVC框架,方法返回的字符串不是跳轉(zhuǎn)是直接在http響應(yīng)體中返回:
1
2
3
4
5
|
@RequestMapping ( "test06" ) @ResponseBody //告知 SpringMVC框架 不進(jìn)行視圖跳轉(zhuǎn),直接進(jìn)行數(shù)據(jù)響應(yīng) public String test06() throws IOException { return "lisi" ; } |
在實(shí)際開發(fā)中,一般不會(huì)直接返回 “lisi” 這是類型的字符串,一般返回的是有一定格式的字符串,例如 json 格式。在返回 json 格式的字符串時(shí),我們需要用到額外添加Jackson的jar包,在xml 文件中添加:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<!--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> |
1
2
3
4
5
6
7
8
9
10
|
@RequestMapping ( "test07" ) @ResponseBody //告知 SpringMVC框架 不進(jìn)行視圖跳轉(zhuǎn),直接進(jì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 幫助我們對對象或集合進(jìn)行json字符串的轉(zhuǎn)換并回寫,為處理器適配器配置消息轉(zhuǎn)換參數(shù),指定使用jackson進(jìn)行對象或集合的轉(zhuǎn)換,因此需要在spring-mvc.xml中進(jìn)行如下配置:
1
2
3
4
5
6
7
8
9
|
<!-- 配置處理器映射器 --> <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ù)方法中進(jìn)行手動(dòng)轉(zhuǎn)換了:
1
2
3
4
5
6
7
8
|
@RequestMapping ( "test08" ) @ResponseBody //告知 SpringMVC框架 不進(jìn)行視圖跳轉(zhuǎn),直接進(jì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ū)動(dòng)代替上述配置。使用<mvc:annotation-driven>自動(dòng)加載 RequestMappingHandlerMapping(處理映射器)和RequestMappingHandlerAdapter( 處 理 適 配 器 ),可用在Spring-xml.xml配置文件中使用<mvc:annotation-driven>替代注解處理器和適配器的配置。
1
2
|
<!--mvc的注解驅(qū)動(dòng)--> <mvc:annotation-driven/> |
到此這篇關(guān)于SpringMVC中的數(shù)據(jù)響應(yīng)的文章就介紹到這了,更多相關(guān)SpringMVC數(shù)據(jù)響應(yīng)內(nèi)容請搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/qq_45040919/article/details/119134806