SpringBoot封裝響應(yīng)數(shù)據(jù)實(shí)現(xiàn)過(guò)程詳解
業(yè)務(wù)處理
這是通過(guò) Spring 在 Controller中注入Service模型層
而在 Service模型層 結(jié)合 Mybatis / Mybatis-Plus 進(jìn)行數(shù)據(jù)加工, 數(shù)據(jù)持久化
封裝響應(yīng)值
將 業(yè)務(wù)處理得到數(shù)據(jù)封裝到 Model作用域中, 伴隨著轉(zhuǎn)頁(yè)將信息傳遞到頁(yè)面
傳值容器
Model
在Controller中新建立 方法 test08, 并在參數(shù)中增加 Model, 注意導(dǎo)包
通過(guò) Model 的 .addAttribute(key, value); 封裝數(shù)據(jù)
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.ui.Model;
@Controller
public class TestController {
@RequestMapping("/test/test08")
public String test08(Model model){
// 封裝數(shù)據(jù)
model.addAttribute("data", "這是要響應(yīng)的動(dòng)態(tài)信息");
System.out.println(" controller 中的測(cè)試方法 test 08 ");
return "ref";
}
}修改ref.html頁(yè)面 使用 thymeleaf 接值
<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
hello spring boot <br>
<span th:text="${data}"></span>
</body>
</html>在 瀏覽器中測(cè)試, 頁(yè)面顯示接收到的信息

ModelMap
Model 類 有個(gè)簡(jiǎn)化版本 ModelMap ,
因?yàn)榇祟愂抢^承自 HashMap, 所以可以使用.put( "key", value);進(jìn)行數(shù)據(jù)封裝
當(dāng)然還是可以使用 .addAttribute("key", value); , 推薦使用這個(gè)方法 , 相比 put()方法, 這個(gè)方法增加驗(yàn)證代碼
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.ui.ModelMap;
@Controller
public class TestController {
@RequestMapping("/test/test09")
public String test09(ModelMap modelMap){
// 封裝數(shù)據(jù)
// modelMap.put("data", "這是要響應(yīng)的動(dòng)態(tài)信息");
modelMap.addAttribute("data", "這是要響應(yīng)的動(dòng)態(tài)信息");
System.out.println(" controller 中的測(cè)試方法 test 09 ");
return "ref";
}
}HttpServletRequest
本質(zhì)上 Model 相當(dāng)于 Request 作用域 , SpringBoot 也提供了 Request的使用
同樣 可以使用參數(shù)傳入 , 如 : test10
也可以通過(guò) Spring 的依賴注入方式 , 如 : test11
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
@Controller
public class TestController {
@Autowired
private HttpServletRequest request;
@RequestMapping("/test/test11")
public String test11(){
// 封裝數(shù)據(jù)
request.setAttribute("data", "這是要響應(yīng)的動(dòng)態(tài)信息");
System.out.println(" controller 中的測(cè)試方法 test 11 ");
return "ref";
}
@RequestMapping("/test/test10")
public String test10(HttpServletRequest request){
// 封裝數(shù)據(jù)
request.setAttribute("data", "這是要響應(yīng)的動(dòng)態(tài)信息");
System.out.println(" controller 中的測(cè)試方法 test 10 ");
return "ref";
}
}重定向傳值
在SpringBoot 中 重定向 就是 一個(gè)方法執(zhí)行完, 再對(duì)另一個(gè)方法發(fā)請(qǐng)求
這時(shí)通過(guò) Model 就不能傳遞值, 可以通過(guò) RedirectAttributes 傳值
從 test12 重定向 到 test13 以 data 為標(biāo)識(shí)進(jìn)行傳值
@RequestMapping("/test/test12")
public String test12(RedirectAttributes redirectAttributes){
// 封裝數(shù)據(jù)
redirectAttributes.addAttribute("data", "這是重定向傳遞的信息");
System.out.println(" controller 中的測(cè)試方法 test 12 ");
return "redirect:test13";
}
@RequestMapping("/test/test13")
public String test13(String data){
System.out.println("data = " + data);
System.out.println(" controller 中的測(cè)試方法 test 13 ");
return "ref";
}到此這篇關(guān)于SpringBoot封裝響應(yīng)數(shù)據(jù)實(shí)現(xiàn)過(guò)程詳解的文章就介紹到這了,更多相關(guān)SpringBoot封裝響應(yīng)數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Windows系統(tǒng)中Java調(diào)用cmd命令及執(zhí)行exe程序的方法
這篇文章主要介紹了Windows系統(tǒng)中Java調(diào)用cmd命令及執(zhí)行exe程序的方法,主要用到了IOException類,需要的朋友可以參考下2016-03-03
mybatis中字段名與關(guān)鍵字相同問(wèn)題
這篇文章主要介紹了mybatis中字段名與關(guān)鍵字相同問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
SpringBoot中EasyExcel實(shí)現(xiàn)Excel文件的導(dǎo)入導(dǎo)出
這篇文章主要介紹了SpringBoot中EasyExcel實(shí)現(xiàn)Excel文件的導(dǎo)入導(dǎo)出,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
java輕量級(jí)規(guī)則引擎easy-rules使用介紹
這篇文章主要介紹了java輕量級(jí)規(guī)則引擎easy-rules使用介紹,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
簡(jiǎn)單講解Java設(shè)計(jì)模式編程中的單一職責(zé)原則
這篇文章主要介紹了Java設(shè)計(jì)模式編程中的單一職責(zé)原則,這在團(tuán)隊(duì)開發(fā)編寫接口時(shí)經(jīng)常使用這樣的約定,需要的朋友可以參考下2016-02-02
深入學(xué)習(xí)spring cloud gateway 限流熔斷
這篇文章主要介紹了深入學(xué)習(xí)spring cloud gateway 限流熔斷,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04

