Spring MVC @ModelAttribute注解

用来将请求参数绑定到 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”测试登录权限。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Gary jie

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值