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對象,通過其對應的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設置跳轉(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設置的值:${username}<br/>
獲取@ModelAttribute設置的值2:${modelAttributeTest}<br/>
獲取request設置的值3:${requestTest}<br/>
獲取session設置的值4:${sessionTest}
</body>
</html>
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java 創(chuàng)建并應用PPT幻燈片母版的方法示例
幻燈片母版可供用戶設置幻燈片的樣式,本文將介紹如何用Java創(chuàng)建并應用單個或多個幻燈片母版。感興趣可以了解一下2020-06-06
Spring boot 使用JdbcTemplate訪問數(shù)據(jù)庫
SpringBoot 是為了簡化 Spring 應用的創(chuàng)建、運行、調(diào)試、部署等一系列問題而誕生的產(chǎn)物。本文重點給大家介紹spring boot 使用JdbcTemplate訪問數(shù)據(jù)庫,需要的朋友可以參考下2018-05-05
Netty中的DelimiterBasedFrameDecoder使用方法詳解
這篇文章主要介紹了Netty中的DelimiterBasedFrameDecoder使用方法詳解,DelimiterBasedFrameDecoder與LineBasedFrameDecoder類似,只不過更加通用,允許我們指定任意特殊字符作為分隔符,我們還可以同時指定多個分隔符,需要的朋友可以參考下2023-12-12
解決IDEA 2020.1版本 maven Test命令出現(xiàn)導包錯誤的問題
這篇文章主要介紹了IDEA 2020.1版本 maven Test命令出現(xiàn)導包錯誤的問題及解決方法,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08
Spring MVC 中獲取session的幾種方法(小結(jié))
這篇文章主要介紹了Spring MVC 中獲取session的幾種方法(小結(jié)),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-09-09
springBoot+webMagic實現(xiàn)網(wǎng)站爬蟲的實例代碼
這篇文章主要介紹了springBoot+webMagic實現(xiàn)網(wǎng)站爬蟲的實例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-05-05

