網站首頁 編程語言 正文
用來將請求參數(shù)綁定到 Model 對象。
1. 應用在方法上
1)應用在無返回值的方法
創(chuàng)建 ModelAttributeController
@controller
public class myModelAttributeController{
@ModelAttribute
public void myModel(@RequestParam(required=false) String name,Model model){
model.addttribute("name",name);
}
@RequstMapping(value="/model")
public String model(){
return "index";
}
}
Spring MVC 會先執(zhí)行 myModel 方法,將 name 的值存入到 Model 中。然后執(zhí)行 model 方法,這樣 name 的值就被帶到了 model 方法中。
index.jsp 頁面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>
</head>
<body>${name}</body>
2)應用在有返回值的方法
修改 ModelAttributeController 控制類:
@Controller
public class ModelattributeController{
public String myModel(@Request(required=false) String name){
return name;
}
@RequestMapping(value="/model")
public String model(){
return "index"
}
}
修改 index.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>編程幫</title>
</head>
<body>
${string }
</body>
</html>
對于以上情況,返回值對象 name 會被默認放到隱含的 Model 中,在 Model 中 key 為返回值首字母小寫,value 為返回的值。等同于 model.addAttribute(“string”, name);。
2. 應用在方法的參數(shù)上
@RequestMapping("/register")
public String register(@ModelAttribute("user") UserForm user) {
if ("zhangsan".equals(uname) && "123456".equals(upass)) {
logger.info("成功");
return "login";
} else {
logger.info("失敗");
return "register";
}
- 將請求參數(shù)的輸入封裝到 user 對象中
- 創(chuàng)建 UserForm 實例
3. ModelAttribute+RequestMapping?
修改 ModelAttributeController
@Controller
public class ModelAttributeController {
// @ModelAttribute和@RequestMapping同時放在方法上
@RequestMapping(value = "/index")
@ModelAttribute("name")
public String model(@RequestParam(required = false) String name) {
return name;
}
}
index.jsp 代碼
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>編程幫</title>
</head>
<body>
${name }
</body>
</html>
@ModelAttribute 和 @RequestMapping 注解同時應用在方法上時,有以下作用:
- 方法的返回值會存入到 Model 對象中,key 為 ModelAttribute 的 value 屬性值。
- 方法的返回值不再是方法的訪問路徑,訪問路徑會變?yōu)?@RequestMapping 的 value 值,例如:@RequestMapping(value = “/index”) 跳轉的頁面是 index.jsp 頁面。
Model和ModelView的區(qū)別?
Model:每次請求中都存在的默認參數(shù),利用其 addAttribute() 方法即可將服務器的值傳遞到客戶端頁面中。
ModelAndView:包含 model 和 view 兩部分,使用時需要自己實例化,利用 ModelMap 來傳值,也可以設置 view 的名稱。
實列?
創(chuàng)建 BaseController,
public class BaseController {
@ModelAttribute
public void isLogin(HttpSession session) throws Exception {
if (session.getAttribute("user") == null) {
throw new Exception("沒有權限");
}
}
}
創(chuàng)建 ModelAttributeController ,
@RequestMapping("/admin")
public class ModelAttributeController extends BaseController {
@RequestMapping("/add")
public String add() {
return "addSuccess";
}
@RequestMapping("/update")
public String update() {
return "updateSuccess";
}
@RequestMapping("/delete")
public String delete() {
return "deleteSuccess";
}
}
在上述 ModelAttributeController 類中的 add、update、delete 請求處理方法執(zhí)行時,首先執(zhí)行父類 BaseController 中的 isLogin 方法判斷登錄權限,可以通過地址“http://localhost:8080/springMVCDemo2/admin/add”測試登錄權限。
原文鏈接:https://blog.csdn.net/qq_44174346/article/details/125780681
相關推薦
- 2022-01-06 npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to
- 2022-07-10 uniapp下拉刷新和上拉加載的實現(xiàn)
- 2022-07-19 springboot繼承swagger3
- 2022-09-03 Redis實現(xiàn)Session共享與單點登錄_Redis
- 2022-03-20 基于ABP框架實現(xiàn)RBAC(角色訪問控制)_實用技巧
- 2022-04-01 SQL?Server?事務,異常和游標詳解_MsSql
- 2023-11-23 python的相對路徑表示方式
- 2022-05-27 C++帶頭雙向循環(huán)鏈表超詳細解析_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支