有点难理解。

修饰方法是表示在该控制器的所有目标方法执行前都执行该modelattribute注解的方法。

修饰参数是表示什么?修饰参数时@modelattributes(value="xxxx") User user  中的value值需要和@modelattribute修改的方法加入的键的值一致。

Controller Code:

package com.tiekui.springmvc.handlers;

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import com.tiekui.springmvc.pojo.Address;
import com.tiekui.springmvc.pojo.User;
import com.tiekui.springmvc.pojo.UserNew; @Controller
public class ModelAttributes { @RequestMapping("testModelAttribute")
public String targetMethod(UserNew user){
System.out.println("Target method" + user);
return "afterModelAttributes";
} @ModelAttribute
public void getuser(@RequestParam(value="id") String id,Map<String, UserNew> map) { if ((id != null)) {
UserNew user = new UserNew("tiekui","1234","zhoutiekui@huawei.com",30,1, new Address("guangzhou", "zhenzhen"));
System.out.println("Get an object" + user);
map.put("userNew", user);
}
}
}

Response View:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body> userAge: ${requestScope.userNew.age} </body>
</html>

View:

    <form action="testModelAttribute">
<br>
password : <input type="text" name="username" value="tiekui">
<br>
password : <input type="password" name="password" value="1234">
<br>
email : <input type="text" name="email" value="zhoutiekui@huawei.com">
<br>
age : <input type="text" name="age" value="18">
<br>
<input type="hidden" name="id" value="1">
<br>
Address Province: <input type="text" name="address.province" value="guangzhou">
<br>
Address City: <input type="text" name="address.city" value="shenzhen">
<br>
<input type="submit" value="Test ModelAttributes">
</form>

POJO userNew:

 

POJO Address:

package com.tiekui.springmvc.pojo;

public class Address {
private String province;
private String city; public Address() {
super();
}
public Address(String province, String city) {
super();
this.province = province;
this.city = city;
}
@Override
public String toString() {
return "Address [province=" + province + ", city=" + city + "]";
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}

SpringMVC(十七-二十) ModelAttribute 注解的更多相关文章

  1. Java笔记(二十) 注解

    注解 一.内置注解 Java内置了一些常用注解: 1.@Override 该注解修饰一个方法,表示当前类重写了父类的该方法. 2.@Deprecated  该注解可以修饰类.方法.字段.参数等.表示对 ...

  2. SpringMVC(二)--处理数据模型、ModelAndView、Model、Map、重定向、@ModelAttribute、

    1.处理模型数据 Spring MVC 提供了以下几种途径输出模型数据:      – ModelAndView:处理方法返回值类型为 ModelAndView 时, 方法体即可通过该对象添加模型数据 ...

  3. 7、SpringMVC源码分析(2):分析HandlerAdapter.handle方法,了解handler方法的调用细节以及@ModelAttribute注解

    从上一篇 SpringMVC源码分析(1) 中我们了解到在DispatcherServlet.doDispatch方法中会通过 mv = ha.handle(processedRequest, res ...

  4. springmvc在使用@ModelAttribute注解获取Request和Response会产生线程并发不安全问题(转)

    springmvc在获取Request和Response有很多方式:具体请看:https://www.cnblogs.com/wade-luffy/p/8867144.html 产生线程问题的代码如下 ...

  5. SpringMVC @ModelAttribute注解

    /**     * 1. 有 @ModelAttribute 标记的方法, 会在每个目标方法执行之前被 SpringMVC 调用!      * 2. @ModelAttribute 注解也可以来修饰 ...

  6. @ModelAttribute注解(SpringMVC)

    在方法定义上使用 @ModelAttribute 注解:Spring MVC 在调用目标处理方法前,会先逐个调用在方法级上标注了 @ModelAttribute 的方法. 在方法的入参前使用 @Mod ...

  7. SpringMvc中@ModelAttribute注解的使用

    一.绑定请求参数到指定对象 public String test1(@ModelAttribute("user") UserModel user) 只是此处多了一个注解@Model ...

  8. SpringMVC的@ModelAttribute注解简单使用(用户修改信息)

    例如有一个User对象,我们要修改他的值,但是不能修改他的密码!通过表单提交数据之后,password为null,会把原对象的passwod覆盖掉.这时候可以用@ModelAttribute注解处理. ...

  9. 二十七 集合!!!!!!!!set 二十八 文件

    0: 不知道啊 去除重复元素? 1:frozenset 2:len(x) 3:直接一个错的报 4:好像一样       错  完全不一样的说  set = {[1,2]}这是想干嘛  :列表怎么可以进 ...

随机推荐

  1. leetcode(js)算法之914卡牌分组

    给定一副牌,每张牌上都写着一个整数. 此时,你需要选定一个数字 X,使我们可以将整副牌按下述规则分成 1 组或更多组: 每组都有 X 张牌. 组内所有的牌上都写着相同的整数. 仅当你可选的 X > ...

  2. cf14d 树的直径,枚举删边

    #include<bits/stdc++.h> using namespace std; #define maxn 300 ]; int n,head[maxn],tot,a,b,dis[ ...

  3. Java SimpleDateFormat 中英文时间格式化转换

    2015年08月29日 17:37:43 阅读数:32459 SimpleDateFormat是一个以与语言环境有关的方式来格式化和解析日期的具体类.它允许进行格式化(日期 -> 文本).解析( ...

  4. oracle 12c ORA-01017:invalid username/password; logon denied

    Oracle 12C 中,想通过操作系统认证登录Oracle 数据库,有一些要注意的地方.不然就会遇到 ORA-01017:invalid username/password; logon denie ...

  5. ActiveSync 学习记录

    协议就是一种规范.它是高效团队协作的依据.有的人可能不爱看团队规范之类的文档,一方面是个人意识问题,另外也和文档的组织.协作的效果相关. 写好文档: 看好文档. 1. 处理XML转码 抓包后,发现邮件 ...

  6. C# 不使用递归遍历目录树中的文件和文件夹

    public class StackBasedIteration { static void Main(string[] args) { // Specify the starting folder ...

  7. Chrome开发者控制台操作教程

    1清空控制台 在控制台下有个clear console的按钮,点击的时候会清空控制台. 清空控制台  2让Chrome中的页面可编辑 有的时候我们需要临时改变页面上的文字,图案等信息,一种常见的方法是 ...

  8. mysql 5.7 安装

    linux(CentOS6.7) 环境Mysql 5.7.17安装教程分享给大家,供大家参考,具体内容如下: 1系统约定 安装文件下载目录:/data/software Mysql目录安装位置:/us ...

  9. SQL查询数据并插入新表

    SQL查询数据并插入新表 --如果接受数据导入的表已经存在 insert into 表 select * from tablename --如果导入数据并生成表 select * into 表 fro ...

  10. 微信小程序如何自动弹出提示微信授权?

    我想在一进入页面的时候就进行判断提示并且弹出提示授权!请问该如何处理比较合理 wx.authorize({}) //可以通过 wx.getSetting 先查询一下用户是否授权了 "scop ...