生活随笔
收集整理的這篇文章主要介紹了
Spring MVC @ModelAttribute 详解
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1.@ModelAttribute注釋void返回值的方法
[java]?view plaincopy print?
@Controller??public?class?HelloModelController?{????????????@ModelAttribute???????public?void?populateModel(@RequestParam?String?abc,?Model?model)?{???????????model.addAttribute("attributeName",?abc);????????}??????????@RequestMapping(value?=?"/helloWorld")????????public?String?helloWorld()?{???????????return?"helloWorld.jsp";????????}??????}?? 在這個(gè)代碼中,訪問控制器方法helloWorld時(shí),會首先調(diào)用populateModel方法,將頁面參數(shù)abc(/helloWorld.ht?abc=text)放到model的attributeName屬性中,在視圖中可以直接訪問。
jsp頁面頁面如下:
[html]?view plaincopy print?
<%@?page?language="java"?contentType="text/html;?charset=utf-8"??????pageEncoding="utf-8"%>??<%@taglib?prefix="c"?uri="http://java.sun.com/jsp/jstl/core"%>??<html>??<head>??</head>??<body>??<c:out?value="${attributeName}"></c:out>??</body>??</html>?? 2.@ModelAttribute注釋返回具體類的方法
[java]?view plaincopy print?
@Controller??public?class?Hello2ModelController?{????????????@ModelAttribute???????public?User?populateModel()?{???????????User?user=new?User();?????????user.setAccount("ray");?????????return?user;??????}????????@RequestMapping(value?=?"/helloWorld2")????????public?String?helloWorld()?{???????????return?"helloWorld.jsp";????????}????}?? 當(dāng)用戶請求 http://localhost:8080/test/helloWorld2.html時(shí),首先訪問populateModel方法,返回User對象,model屬性的名稱沒有指定,它由返回類型隱含表示,如這個(gè)方法返回User類型,那么這個(gè)model屬性的名稱是user。?
這個(gè)例子中model屬性名稱有返回對象類型隱含表示,model屬性對象就是方法的返回值。它無須要特定的參數(shù)。
jsp 中如下訪問:
[html]?view plaincopy print?
<c:out?value="${user.account}"></c:out>?? 也可以指定屬性名稱
[java]?view plaincopy print?
@Controller??public?class?Hello2ModelController?{????????????@ModelAttribute(value="myUser")??????public?User?populateModel()?{???????????User?user=new?User();?????????user.setAccount("ray");?????????return?user;??????}????????@RequestMapping(value?=?"/helloWorld2")????????public?String?helloWorld(Model?map)?{???????????return?"helloWorld.jsp";????????}????}?? jsp中如下訪問:
[html]?view plaincopy print?
<c:out?value="${myUser.account}"></c:out>?? 對象合并:
[java]?view plaincopy print?
@Controller??public?class?Hello2ModelController?{????????????@ModelAttribute??????public?User?populateModel()?{???????????User?user=new?User();?????????user.setAccount("ray");?????????return?user;??????}??????????????@RequestMapping(value?=?"/helloWorld2")????????public?String?helloWorld(User?user)?{??????????user.setName("老王");?????????return?"helloWorld.jsp";????????}????}?? 對象合并指定對象名稱:
[java]?view plaincopy print?
@Controller??public?class?Hello2ModelController?{????????????@ModelAttribute("myUser")??????public?User?populateModel()?{???????????User?user=new?User();?????????user.setAccount("ray");?????????return?user;??????}??????????????@RequestMapping(value?=?"/helloWorld2")????????public?String?helloWorld(@ModelAttribute("myUser")?User?user)?{??????????user.setName("老王");?????????return?"helloWorld.jsp";????????}????}?? 這樣在jsp中可以使用如下方式訪問
[html]?view plaincopy print?
<c:out?value="${myUser.name}"></c:out>??<c:out?value="${myUser.account}"></c:out>?? 3.通過此特性控制權(quán)限.
我們可以在基類方法中控制寫此注解,需要控制權(quán)限的控制器,繼承控制器就可以了。
[java]?view plaincopy print?
public?class?BaseController?{????????????@ModelAttribute??????public?void?populateModel()?throws?Exception?{???????????SysUser?user=ContextUtil.getCurrentUser();?????????if(user.getAccount().equals("admin")){?????????????throw?new?Exception("沒有權(quán)限");?????????}??????}????}?? 需要控制權(quán)限的類繼承BaseController
[java]?view plaincopy print?
@Controller??public?class?Hello2ModelController?extends?BaseController?{????????????@RequestMapping(value?=?"/helloWorld2")????????public?String?helloWorld(@ModelAttribute("myUser")?User?user)?{??????????user.setName("老王");?????????return?"helloWorld.jsp";????????}????}?? 這樣就可以控制權(quán)限了,當(dāng)然控制權(quán)限的方法有很多,比如通過過濾器等。這里只是提供一種思路。
總結(jié):
@ModelAttribute具有如下三個(gè)作用:
①綁定請求參數(shù)到命令對象:放在功能處理方法的入?yún)⑸蠒r(shí),用于將多個(gè)請求參數(shù)綁定到一個(gè)命令對象,從而簡化綁定流程,而且自動(dòng)暴露為模型數(shù)據(jù)用于視圖頁面展示時(shí)使用。其實(shí)@ModelAttribute此處對于供視圖頁面展示來說與model.addAttribute("attributeName",?abc);功能類似。
[java]?view plaincopy print?
public?String?test(@ModelAttribute("user")?UserModel?user)??? 此處多了一個(gè)注解@ModelAttribute("user"),它的作用是將該綁定的命令對象以“user”為名稱添加到模型對象中供視圖頁面展示使用。我們此時(shí)可以在視圖頁面使用${user.username}來獲取綁定的命令對象的屬性。
②暴露@RequestMapping 方法返回值為模型數(shù)據(jù):放在功能處理方法的返回值上時(shí),是暴露功能處理方法的返回值為模型數(shù)據(jù),用于視圖頁面展示時(shí)使用。
[java]?view plaincopy print?
public?@ModelAttribute("user2")?UserModel?test3(@ModelAttribute("user2")?UserModel?user)?? 大家可以看到返回值類型是命令對象類型,而且通過@ModelAttribute("user2")注解,此時(shí)會暴露返回值到模型數(shù)據(jù)( 名字為user2 ) 中供視圖展示使用
@ModelAttribute 注解的返回值會覆蓋@RequestMapping 注解方法中的@ModelAttribute 注解的同名命令對象
③暴露表單引用對象為模型數(shù)據(jù):放在處理器的一般方法(非功能處理方法)上時(shí),是為表單準(zhǔn)備要展示的表單引用對象,如注冊時(shí)需要選擇的所在城市等,而且在執(zhí)行功能處理方法(@RequestMapping 注解的方法)之前,自動(dòng)添加到模型對象中,用于視圖頁面展示時(shí)使用;
總結(jié)
以上是生活随笔為你收集整理的Spring MVC @ModelAttribute 详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。