Springmvc如何實現(xiàn)向前臺傳遞數(shù)據(jù)
1) 在springmvc方法的形參中定義Map,Model,ModelMap,并在方法中通過這三個對象進行值的傳遞;
①其中Map和ModelMap使用方式是一致的;
@RequestMapping("/detail") public String detail(Integer id, //ModelMap modelMap Map modelMap ){ HashMap<String,String> conditions=new HashMap<>(); conditions.put("sal","88888888"); conditions.put("age","35"); //todo 去數(shù)據(jù)庫查詢用戶信息 System.out.println("查詢id為"+id+"的用戶記錄"); User user=new User(id,"詹姆斯",18,"男","美國克利夫蘭", new Role("小前鋒",23), conditions, Arrays.asList("打籃球","打游戲")); //通過modelMap或map向前臺傳值==>request.setAttribute(key,value) modelMap.put("user",user); return "detail.jsp"; }
②Model只是通過addAttribute進行傳值;
@RequestMapping("/detail") public String detail(Integer id, Model model){ HashMap<String,String> conditions=new HashMap<>(); conditions.put("sal","88888888"); conditions.put("age","35"); //todo 去數(shù)據(jù)庫查詢用戶信息 System.out.println("查詢id為"+id+"的用戶記錄"); User user=new User(id,"詹姆斯",18,"男","美國克利夫蘭", new Role("小前鋒",23), conditions, Arrays.asList("打籃球","打游戲")); //通過Model對象傳遞數(shù)據(jù) model.addAttribute("user",user); return "detail.jsp"; }
2) 定義方法的返回值類型為ModelAndView,在方法中創(chuàng)建ModelAndView 并指定跳轉(zhuǎn)的頁面和傳遞的數(shù)據(jù),最后返回ModelAndView對象;
3) 通過注解的方式 @ModelAttribute;
4) 在方法參數(shù)中定義Request或session對象,通過其對應(yīng)的API;
下面2),3),4)的情況都在下面的代碼內(nèi);
//演示通過ModelAndView向頁面?zhèn)髦? //@ModelAttribute:注解將對象傳遞到request域中 1)加在方法參數(shù)上,將對象傳遞到request域中,或向request域中取值 // 2)加在方法上,將方法的返回值放入request域中 @RequestMapping("/detail2") public ModelAndView detail2(Integer id, @ModelAttribute("username") String username, HttpServletRequest request, HttpSession session, HttpServletResponse response ){ request.setAttribute("requestTest","請求域數(shù)據(jù)"); session.setAttribute("sessionTest","session域數(shù)據(jù)"); HashMap<String,String> conditions=new HashMap<>(); conditions.put("sal","88888888"); conditions.put("age","35"); //todo 去數(shù)據(jù)庫查詢用戶信息 System.out.println("查詢id為"+id+"的用戶記錄"); User user=new User(id,"詹姆斯",18,"男","美國克利夫蘭", new Role("小前鋒",23), conditions, Arrays.asList("打籃球","打游戲")); //通過ModelAndView設(shè)置跳轉(zhuǎn)的頁面和值 ModelAndView modelAndView=new ModelAndView(); //向頁面?zhèn)髦? modelAndView.addObject("user",user); //指定跳轉(zhuǎn)的頁面 以/開頭,則直接到資源根目錄下找(即webapp下) // 不以/開頭,跟在RequestMapping最后一個/后面 modelAndView.setViewName("detail.jsp"); return modelAndView; } //將方法返回值放入request域中 @ModelAttribute(name = "modelAttributeTest") public String test(){ return "我是@ModelAttribute的測試"; }
detail.jsp中代碼如下:
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>用戶詳情頁面</title> </head> <body> <h1>用戶詳細信息</h1> <table> <tr> <td>用戶名</td> <td>${user.name}</td> <td>年齡</td> <td>${user.age}</td> </tr> <tr> <td>性別</td> <td>${user.sex}</td> <td>地址</td> <td>${user.addr}</td> </tr> <tr> <td>角色ID</td> <td>${user.role.id}</td> <td>角色名</td> <td>${user.role.name}</td> </tr> <tr> <td>條件1</td> <td>${user.conditions.sal}</td> <td>條件2</td> <td>${user.conditions.age}</td> </tr> <tr> <td>愛好</td> <td>${user.hobbies}</td> </tr> </table> 獲取@ModelAttribute設(shè)置的值:${username}<br/> 獲取@ModelAttribute設(shè)置的值2:${modelAttributeTest}<br/> 獲取request設(shè)置的值3:${requestTest}<br/> 獲取session設(shè)置的值4:${sessionTest} </body> </html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringMVC處理數(shù)據(jù)輸出的實例代碼
- 利用springmvc處理模型數(shù)據(jù)
- Springmvc ResponseBody響應(yīng)json數(shù)據(jù)實現(xiàn)過程
- springMVC如何對輸入數(shù)據(jù)校驗實現(xiàn)代碼
- Springmvc如何返回xml及json格式數(shù)據(jù)
- SpringMvc響應(yīng)數(shù)據(jù)及結(jié)果視圖實現(xiàn)代碼
- SPRINGMVC JSON數(shù)據(jù)交互如何實現(xiàn)
- SpringMVC數(shù)據(jù)輸出相關(guān)知識總結(jié)
相關(guān)文章
Java程序順序結(jié)構(gòu)中邏輯控制語句詳解流程
在程序開發(fā)的過程之中一共會存在有三種程序邏輯:順序結(jié)構(gòu)、分支結(jié)構(gòu)、循環(huán)結(jié)構(gòu),對于之前所編寫的代碼大部分都是順序結(jié)構(gòu)的定義,即:所有的程序?qū)凑斩x的代碼順序依次執(zhí)行2021-10-10java 線程中start方法與run方法的區(qū)別詳細介紹
這篇文章主要介紹了java 線程中start方法與run方法的區(qū)別詳細介紹的相關(guān)資料,在java線程中調(diào)用start方法與run方法的區(qū)別在哪里? 這兩個問題是兩個非常流行的初學(xué)者級別的多線程面試問題,這里進行詳細說明,需要的朋友可以參考下2016-11-11詳解在Spring MVC中使用注解的方式校驗RequestParams
本篇文章主要介紹了詳解在Spring MVC中使用注解的方式校驗RequestParams ,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-03-03Spring實戰(zhàn)之ResourceLoader接口資源加載用法示例
這篇文章主要介紹了Spring實戰(zhàn)之ResourceLoader接口資源加載用法,結(jié)合實例形式分析了Spring使用ResourceLoader接口加載資源的相關(guān)配置與使用技巧,需要的朋友可以參考下2020-01-01Spring Boot Logging Level設(shè)置為off時的Bug
這篇文章主要介紹了Spring Boot Logging Level設(shè)置為off時的Bug,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09Java SpringMVC 異常處理SimpleMappingExceptionResolver類詳解
這篇文章主要介紹了SpringMVC 異常處理SimpleMappingExceptionResolver類詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-09-09Spring Cloud OpenFeign REST服務(wù)客戶端原理及用法解析
這篇文章主要介紹了Spring Cloud OpenFeign REST服務(wù)客戶端原理及用法解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-10-10