有点难理解。

修饰方法是表示在该控制器的所有目标方法执行前都执行该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. js小方法积累,将一个数组按照n个一份,分成若干数组

    // 把一个数组按照一定长度分割成若干数组 function group(array, subGroupLength) { let index = 0; let newArray = []; whil ...

  2. vue.js 监听属性的学习/ 千米、米的转换 /时、分、秒 的转换

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  3. 【python】ftp连接,主被动,调试等级

    示例代码如下: #!/usr/bin/env python # -*- coding: utf-8 -*- import os from ftplib import FTP def ftp_down( ...

  4. 《剑指offer》 反转链表

    本题来自<剑指offer> 反转链表 题目: 输入一个链表,反转链表后,输出新链表的表头. 思路: 需要三个变量,来保存当前节点的,前面节点和反转后的节点. C++ Code: /* st ...

  5. ubuntu MySQL配置文件

    查看配置文件: locate my.cnf 查看配置文件路径: /记得要在配置环境变量后执行下列命令 which mysqld /usr/local/mysql/bin/mysqld --verbos ...

  6. Redis扩展

    Redis扩展下载地址:https://windows.php.net/downloads/pecl/releases/redis/ PHP怎么安装redis扩展 http://www.php.cn/ ...

  7. linux下安装pip与pip安装

    在执行脚本的时候,说有库找不到 pip安装的时候说不认识pip 安装pip 使用脚本安装和升级pip wget https://bootstrap.pypa.io/get-pip.py 运行脚本pyt ...

  8. java/springboot自定义注解实现AOP

    java注解 即是注释了,百度解释:也叫元数据.一种代码级别的说明. 个人理解:就是内容可以被代码理解的注释,一般是一个类. 元数据 也叫元注解,是放在被定义的一个注解类的前面 ,是对注解一种限制. ...

  9. 该问题是需要导包!!!需要pom中添加依赖The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application

    <!-- https://mvnrepository.com/artifact/org.apache.taglibs/taglibs-standard-impl --><depend ...

  10. hdfs数据到hbase过程

    需求:将HDFS上的文件中的数据导入到hbase中 实现上面的需求也有两种办法,一种是自定义mr,一种是使用hbase提供好的import工具 一.hdfs中的数据是这样的 hbase创建好表 cre ...