SpringMVC中Controller類數(shù)據(jù)響應(yīng)的方法
上篇博客我們了解了請(qǐng)求參數(shù)的獲取,那么獲取到請(qǐng)求參數(shù)之后,需要對(duì)參數(shù)進(jìn)行出來(lái),然后進(jìn)行數(shù)據(jù)響應(yīng)。那么這篇博客我們就來(lái)了解 Controller 類如何進(jìn)行數(shù)據(jù)響應(yīng)。
1. 方法返回值類型
在 web 階段我們也了解過(guò)數(shù)據(jù)響應(yīng),我們可以簡(jiǎn)單的將數(shù)據(jù)響應(yīng)分為:頁(yè)面跳轉(zhuǎn)和回寫(xiě)數(shù)據(jù)
Controller 類的業(yè)務(wù)返回的返回值類型有很多,但歸根結(jié)底就是用于完成頁(yè)面跳轉(zhuǎn)和回寫(xiě)數(shù)據(jù)。我們了解一下常用的幾個(gè)返回值類型:ModelAndView, Model,ModelMap,Map,View, String, void ,@ResponseBody,HttpHeaders 。
2. 頁(yè)面跳轉(zhuǎn)
在 SpringMVC 中完成頁(yè)面跳轉(zhuǎn)有兩種方式:直接返回字符串和返回 ModelAndView 對(duì)象
2.1 直接返回字符串
當(dāng)直接返回一個(gè)字符串時(shí),會(huì)自動(dòng)通過(guò)視圖解析器解析為物理視圖地址。
@RequestMapping("/user")
public class MyController {
//請(qǐng)求地址: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è)置是視圖的相對(duì)地址,相對(duì) MyController 類的請(qǐng)求地址,所以我們可以將其設(shè)置為絕對(duì)地址return "\success.jsp";。
2.2 返回 ModelAndView 對(duì)象
ModelAndView 對(duì)象我們可以進(jìn)行分解, Model 表示模型用于封裝數(shù)據(jù),View 表示視圖用于展示數(shù)據(jù)。 ModelAndView 對(duì)象的一些方法:

使用 ModelAndView 對(duì)象完成頁(yè)面跳轉(zhuǎn):
@RequestMapping("test01")
public ModelAndView test01(){
//創(chuàng)建 modelAndView 對(duì)象
ModelAndView modelAndView =new ModelAndView();
//設(shè)置視圖名稱
modelAndView.setViewName("/user.jsp");
//設(shè)置模型數(shù)據(jù)
modelAndView.addObject("user","zhangsan");
return modelAndView;
}
也可以不手動(dòng)創(chuàng)建 ModelAndView 對(duì)象,直接在方法上添加形參,這種方式的ModelAndView 對(duì)象創(chuàng)建實(shí)參
@RequestMapping("test02")
public ModelAndView test02(ModelAndView modelAndView){
//設(shè)置視圖名稱
modelAndView.setViewName("/user");
//設(shè)置模型數(shù)據(jù)
modelAndView.addObject("user","lisi");
return modelAndView;
}
這兩種方式是一樣的,只不過(guò) ModelAndView 對(duì)象的創(chuàng)建角色改變了,除了這種兩種方式還有其他方式,我們可以通過(guò)View和Model將 ModelAndView 對(duì)象拆分。
@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)部資源視圖解析器,將前綴和后綴提取出來(lái),在 SpringMVC 的配置文件中進(jìn)行配置:
<!--配置內(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 對(duì)象
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ā):請(qǐng)求轉(zhuǎn)發(fā)是指將請(qǐng)求再轉(zhuǎn)發(fā)到其他地址,轉(zhuǎn)發(fā)過(guò)程中使用的是同一個(gè)請(qǐng)求,轉(zhuǎn)發(fā)的地址欄內(nèi)容不變。重
- 定向:是指由原請(qǐng)求地址重新定位到某個(gè)新地址,原有的請(qǐng)求失效,客戶端看到的是新的請(qǐng)求返回的相應(yīng)結(jié)果。
在SpringMVC 中默認(rèn)是通過(guò)轉(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配置文件中的自定義視圖解析器無(wú)效。return "forward:/main"表示轉(zhuǎn)發(fā)到映射名為main的controller,而return "forward:/main.jsp"表示轉(zhuǎn)發(fā)到main.jsp頁(yè)面。
在方法上只有@RequestMapping 時(shí),無(wú)論方法返回值是什么,都需要進(jìn)行跳轉(zhuǎn)。
3. 回寫(xiě)數(shù)據(jù)
回寫(xiě)數(shù)據(jù)也有兩種方式:直接返回字符串和返回對(duì)象或集合
3.1 直接返回字符串
Web基礎(chǔ)階段,客戶端訪問(wèn)服務(wù)器端,如果想直接回寫(xiě)字符串作為響應(yīng)體返回的話,只需要使用response.getWriter().print(“hello world”) 即可,所以我們通過(guò)SpringMVC框架注入的response對(duì)象,此時(shí)不需要視圖跳轉(zhuǎn),業(yè)務(wù)方法返回值為void。
@RequestMapping("test05")
public void test05(HttpServletResponse response) throws IOException {
response.getWriter().println("zhangsan");
}
除了這種方式,還有通過(guò)@ResponseBody注解告知SpringMVC框架,方法返回的字符串不是跳轉(zhuǎn)是直接在http響應(yīng)體中返回:
@RequestMapping("test06")
@ResponseBody //告知 SpringMVC框架 不進(jìn)行視圖跳轉(zhuǎn),直接進(jìn)行數(shù)據(jù)響應(yīng)
public String test06() throws IOException {
return "lisi";
}
在實(shí)際開(kāi)發(fā)中,一般不會(huì)直接返回 “l(fā)isi” 這是類型的字符串,一般返回的是有一定格式的字符串,例如 json 格式。在返回 json 格式的字符串時(shí),我們需要用到額外添加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框架 不進(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 返回對(duì)象或集合
通過(guò) SpringMVC 幫助我們對(duì)對(duì)象或集合進(jìn)行json字符串的轉(zhuǎn)換并回寫(xiě),為處理器適配器配置消息轉(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>
通過(guò)配置了消息轉(zhuǎn)換器之后,我們就不需要在業(yè)務(wù)方法中進(jìn)行手動(dòng)轉(zhuǎn)換了:
@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;
}
在上述方法中我們通過(guò)配置處理器映射器完成了json格式的字符串的轉(zhuǎn)換,但是這種配置方式比較繁瑣,配置代碼比較多,因此,我們可以使用mvc的注解驅(qū)動(dòng)代替上述配置。使用<mvc:annotation-driven>自動(dòng)加載 RequestMappingHandlerMapping(處理映射器)和RequestMappingHandlerAdapter( 處 理 適 配 器 ),可用在Spring-xml.xml配置文件中使用<mvc:annotation-driven>替代注解處理器和適配器的配置。
<!--mvc的注解驅(qū)動(dòng)--> <mvc:annotation-driven/>
到此這篇關(guān)于SpringMVC中的數(shù)據(jù)響應(yīng)的文章就介紹到這了,更多相關(guān)SpringMVC數(shù)據(jù)響應(yīng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JAVA將中文轉(zhuǎn)換為拼音簡(jiǎn)單實(shí)現(xiàn)方法
拼音轉(zhuǎn)換是中文處理的常見(jiàn)需求,TinyPinyin、HanLP、pinyin4j是常用的本地拼音轉(zhuǎn)換庫(kù),各有特點(diǎn),開(kāi)發(fā)者可根據(jù)具體需求選擇合適的拼音轉(zhuǎn)換工具,需要的朋友可以參考下2024-10-10
如何在Spring?Boot中使用MyBatis訪問(wèn)數(shù)據(jù)庫(kù)
MyBatis可以通過(guò)簡(jiǎn)單的XML或者注解來(lái)配置和映射原始類型,接口,和Java POJO為數(shù)據(jù)庫(kù)中記錄,使用MyBatis幫助我們解決各種問(wèn)題,本文介紹如何在Spring?Boot中使用MyBatis訪問(wèn)數(shù)據(jù)庫(kù),感興趣的朋友一起看看吧2023-11-11
Springboot3+Redis實(shí)現(xiàn)消息隊(duì)列的多種方法小結(jié)
本文主要介紹了Springboot3+Redis實(shí)現(xiàn)消息隊(duì)列的多種方法小結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-03-03
Maven項(xiàng)目分析剔除無(wú)用jar引用的方法步驟
這篇文章主要介紹了Maven項(xiàng)目分析剔除無(wú)用jar引用的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
解決Mybatis-plus自定義TypeHandler查詢映射結(jié)果一直為null問(wèn)題
這篇文章主要介紹了解決Mybatis-plus自定義TypeHandler查詢映射結(jié)果一直為null問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
Java去重排序之Comparable與Comparator的使用及說(shuō)明
這篇文章主要介紹了Java去重排序之Comparable與Comparator的使用及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04

