SpringMVC中Controller類數(shù)據(jù)響應的方法
上篇博客我們了解了請求參數(shù)的獲取,那么獲取到請求參數(shù)之后,需要對參數(shù)進行出來,然后進行數(shù)據(jù)響應。那么這篇博客我們就來了解 Controller 類如何進行數(shù)據(jù)響應。
1. 方法返回值類型
在 web 階段我們也了解過數(shù)據(jù)響應,我們可以簡單的將數(shù)據(jù)響應分為:頁面跳轉(zhuǎn)和回寫數(shù)據(jù)
Controller 類的業(yè)務返回的返回值類型有很多,但歸根結(jié)底就是用于完成頁面跳轉(zhuǎn)和回寫數(shù)據(jù)。我們了解一下常用的幾個返回值類型:ModelAndView, Model,ModelMap,Map,View, String, void ,@ResponseBody,HttpHeaders 。
2. 頁面跳轉(zhuǎn)
在 SpringMVC 中完成頁面跳轉(zhuǎn)有兩種方式:直接返回字符串和返回 ModelAndView 對象
2.1 直接返回字符串
當直接返回一個字符串時,會自動通過視圖解析器解析為物理視圖地址。
@RequestMapping("/user")
public class MyController {
//請求地址:localhost:8080/user/testReturnString
@RequestMapping("/testReturnString")
public String testReturnString() {
System.out.println("MyController 的 testReturnString 方法執(zhí)行 了。。。。");
return "success.jsp";
}
}
當你的視圖不是位于 user 文件夾下時,客戶端會報 404 錯誤,因為在找不到該視圖。這種方式設置是視圖的相對地址,相對 MyController 類的請求地址,所以我們可以將其設置為絕對地址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();
//設置視圖名稱
modelAndView.setViewName("/user.jsp");
//設置模型數(shù)據(jù)
modelAndView.addObject("user","zhangsan");
return modelAndView;
}
也可以不手動創(chuàng)建 ModelAndView 對象,直接在方法上添加形參,這種方式的ModelAndView 對象創(chuàng)建實參
@RequestMapping("test02")
public ModelAndView test02(ModelAndView modelAndView){
//設置視圖名稱
modelAndView.setViewName("/user");
//設置模型數(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è)務方法中就可以省略前綴和后綴,SpringMVC 將返回的字符串與在視圖解析器的前后綴拼接后跳轉(zhuǎn):
@RequestMapping("test01")
public ModelAndView test01(){
//創(chuàng)建 modelAndView 對象
ModelAndView modelAndView =new ModelAndView();
//設置視圖名稱
modelAndView.setViewName("user");
//設置模型數(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)容不變。重
- 定向:是指由原請求地址重新定位到某個新地址,原有的請求失效,客戶端看到的是新的請求返回的相應結(jié)果。
在SpringMVC 中默認是通過轉(zhuǎn)發(fā)完成跳轉(zhuǎn)的,當然也可以設置為重定向:
//轉(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基礎階段,客戶端訪問服務器端,如果想直接回寫字符串作為響應體返回的話,只需要使用response.getWriter().print(“hello world”) 即可,所以我們通過SpringMVC框架注入的response對象,此時不需要視圖跳轉(zhuǎn),業(yè)務方法返回值為void。
@RequestMapping("test05")
public void test05(HttpServletResponse response) throws IOException {
response.getWriter().println("zhangsan");
}
除了這種方式,還有通過@ResponseBody注解告知SpringMVC框架,方法返回的字符串不是跳轉(zhuǎn)是直接在http響應體中返回:
@RequestMapping("test06")
@ResponseBody //告知 SpringMVC框架 不進行視圖跳轉(zhuǎn),直接進行數(shù)據(jù)響應
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ù)響應
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è)務方法中進行手動轉(zhuǎn)換了:
@RequestMapping("test08")
@ResponseBody //告知 SpringMVC框架 不進行視圖跳轉(zhuǎn),直接進行數(shù)據(jù)響應
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/>
到此這篇關于SpringMVC中的數(shù)據(jù)響應的文章就介紹到這了,更多相關SpringMVC數(shù)據(jù)響應內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
JAVA將中文轉(zhuǎn)換為拼音簡單實現(xiàn)方法
拼音轉(zhuǎn)換是中文處理的常見需求,TinyPinyin、HanLP、pinyin4j是常用的本地拼音轉(zhuǎn)換庫,各有特點,開發(fā)者可根據(jù)具體需求選擇合適的拼音轉(zhuǎn)換工具,需要的朋友可以參考下2024-10-10
如何在Spring?Boot中使用MyBatis訪問數(shù)據(jù)庫
MyBatis可以通過簡單的XML或者注解來配置和映射原始類型,接口,和Java POJO為數(shù)據(jù)庫中記錄,使用MyBatis幫助我們解決各種問題,本文介紹如何在Spring?Boot中使用MyBatis訪問數(shù)據(jù)庫,感興趣的朋友一起看看吧2023-11-11
Springboot3+Redis實現(xiàn)消息隊列的多種方法小結(jié)
本文主要介紹了Springboot3+Redis實現(xiàn)消息隊列的多種方法小結(jié),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2025-03-03
解決Mybatis-plus自定義TypeHandler查詢映射結(jié)果一直為null問題
這篇文章主要介紹了解決Mybatis-plus自定義TypeHandler查詢映射結(jié)果一直為null問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07
Java去重排序之Comparable與Comparator的使用及說明
這篇文章主要介紹了Java去重排序之Comparable與Comparator的使用及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04

