SpringBoot封裝響應(yīng)數(shù)據(jù)實(shí)現(xiàn)過程詳解
業(yè)務(wù)處理
這是通過 Spring 在 Controller中注入Service模型層
而在 Service模型層 結(jié)合 Mybatis / Mybatis-Plus 進(jìn)行數(shù)據(jù)加工, 數(shù)據(jù)持久化
封裝響應(yīng)值
將 業(yè)務(wù)處理得到數(shù)據(jù)封裝到 Model作用域中, 伴隨著轉(zhuǎn)頁將信息傳遞到頁面
傳值容器
Model
在Controller中新建立 方法 test08, 并在參數(shù)中增加 Model, 注意導(dǎo)包
通過 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 中的測試方法 test 08 "); return "ref"; } }
修改ref.html頁面 使用 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>
在 瀏覽器中測試, 頁面顯示接收到的信息
ModelMap
Model 類 有個(gè)簡化版本 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 中的測試方法 test 09 "); return "ref"; } }
HttpServletRequest
本質(zhì)上 Model 相當(dāng)于 Request 作用域 , SpringBoot 也提供了 Request的使用
同樣 可以使用參數(shù)傳入 , 如 : test10
也可以通過 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 中的測試方法 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 中的測試方法 test 10 "); return "ref"; } }
重定向傳值
在SpringBoot 中 重定向 就是 一個(gè)方法執(zhí)行完, 再對(duì)另一個(gè)方法發(fā)請(qǐng)求
這時(shí)通過 Model 就不能傳遞值, 可以通過 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 中的測試方法 test 12 "); return "redirect:test13"; } @RequestMapping("/test/test13") public String test13(String data){ System.out.println("data = " + data); System.out.println(" controller 中的測試方法 test 13 "); return "ref"; }
到此這篇關(guān)于SpringBoot封裝響應(yīng)數(shù)據(jù)實(shí)現(xiàn)過程詳解的文章就介紹到這了,更多相關(guān)SpringBoot封裝響應(yīng)數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java8中對(duì)于LocalDateTime的序列化和反序列化問題
這篇文章主要介紹了Java8中對(duì)于LocalDateTime的序列化和反序列化問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06SpringMVC深入講解文件的上傳下載實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了springMVC實(shí)現(xiàn)文件上傳和下載的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06