学无先后,达者为师

网站首页 编程语言 正文

Spring MVC @ModelAttribute注解

作者:ezezeez 更新时间: 2022-07-16 编程语言

用来将请求参数绑定到 Model 对象。

1. 应用在方法上

1)应用在无返回值的方法
创建 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 会先执行 myModel 方法,将 name 的值存入到 Model 中。然后执行 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. 应用在方法的参数上

@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";
}
  • 将请求参数的输入封装到 user 对象中
  • 创建 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. 方法的返回值不再是方法的访问路径,访问路径会变为 @RequestMapping 的 value 值,例如:@RequestMapping(value = “/index”) 跳转的页面是 index.jsp 页面。

Model和ModelView的区别⭐

Model:每次请求中都存在的默认参数,利用其 addAttribute() 方法即可将服务器的值传递到客户端页面中。

ModelAndView:包含 model 和 view 两部分,使用时需要自己实例化,利用 ModelMap 来传值,也可以设置 view 的名称。

实列⭐

创建 BaseController,

public class BaseController {
    @ModelAttribute
    public void isLogin(HttpSession session) throws Exception {
        if (session.getAttribute("user") == null) {
            throw new Exception("没有权限");
        }
    }
}

创建 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 请求处理方法执行时,首先执行父类 BaseController 中的 isLogin 方法判断登录权限,可以通过地址“http://localhost:8080/springMVCDemo2/admin/add”测试登录权限。

原文链接:https://blog.csdn.net/qq_44174346/article/details/125780681

栏目分类
最近更新