網(wǎng)站首頁 編程語言 正文
用來將請求參數(shù)綁定到 Model 對象。
1. 應(yīng)用在方法上
1)應(yīng)用在無返回值的方法
創(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)應(yīng)用在有返回值的方法
修改 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 會被默認(rèn)放到隱含的 Model 中,在 Model 中 key 為返回值首字母小寫,value 為返回的值。等同于 model.addAttribute(“string”, name);。
2. 應(yīng)用在方法的參數(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 實(shí)例
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 注解同時應(yīng)用在方法上時,有以下作用:
- 方法的返回值會存入到 Model 對象中,key 為 ModelAttribute 的 value 屬性值。
- 方法的返回值不再是方法的訪問路徑,訪問路徑會變?yōu)?@RequestMapping 的 value 值,例如:@RequestMapping(value = “/index”) 跳轉(zhuǎn)的頁面是 index.jsp 頁面。
Model和ModelView的區(qū)別?
Model:每次請求中都存在的默認(rèn)參數(shù),利用其 addAttribute() 方法即可將服務(wù)器的值傳遞到客戶端頁面中。
ModelAndView:包含 model 和 view 兩部分,使用時需要自己實(shí)例化,利用 ModelMap 來傳值,也可以設(shè)置 view 的名稱。
實(shí)列?
創(chuàng)建 BaseController,
public class BaseController {
@ModelAttribute
public void isLogin(HttpSession session) throws Exception {
if (session.getAttribute("user") == null) {
throw new Exception("沒有權(quán)限");
}
}
}
創(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 方法判斷登錄權(quán)限,可以通過地址“http://localhost:8080/springMVCDemo2/admin/add”測試登錄權(quán)限。
原文鏈接:https://blog.csdn.net/qq_44174346/article/details/125780681
相關(guān)推薦
- 2022-07-25 詳解docker進(jìn)行數(shù)據(jù)掛載的三種模式_docker
- 2022-08-03 Android開發(fā)手冊自定義Switch開關(guān)按鈕控件_Android
- 2022-11-01 go語言中for?range使用方法及避坑指南_Golang
- 2022-01-26 關(guān)于(NOTICE)iconv(): Detected an illegal character i
- 2023-07-26 webpack打包優(yōu)化之減少代碼體積(Tree shaking、babel)
- 2022-06-24 React如何使用refresh_token實(shí)現(xiàn)無感刷新頁面_React
- 2022-05-31 詳解Docker?Compose配置文件參數(shù)_docker
- 2022-05-11 使用kettle的數(shù)據(jù)庫增量備份與全量備份
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支