日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Spring MVC @ModelAttribute注解

作者:ezezeez 更新時間: 2022-07-16 編程語言

用來將請求參數(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 注解同時應用在方法上時,有以下作用:

  1. 方法的返回值會存入到 Model 對象中,key 為 ModelAttribute 的 value 屬性值
  2. 方法的返回值不再是方法的訪問路徑,訪問路徑會變?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

欄目分類
最近更新