SpringMVC(十七-二十) ModelAttribute 注解
有点难理解。
修饰方法是表示在该控制器的所有目标方法执行前都执行该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 注解的更多相关文章
- Java笔记(二十) 注解
注解 一.内置注解 Java内置了一些常用注解: 1.@Override 该注解修饰一个方法,表示当前类重写了父类的该方法. 2.@Deprecated 该注解可以修饰类.方法.字段.参数等.表示对 ...
- SpringMVC(二)--处理数据模型、ModelAndView、Model、Map、重定向、@ModelAttribute、
1.处理模型数据 Spring MVC 提供了以下几种途径输出模型数据: – ModelAndView:处理方法返回值类型为 ModelAndView 时, 方法体即可通过该对象添加模型数据 ...
- 7、SpringMVC源码分析(2):分析HandlerAdapter.handle方法,了解handler方法的调用细节以及@ModelAttribute注解
从上一篇 SpringMVC源码分析(1) 中我们了解到在DispatcherServlet.doDispatch方法中会通过 mv = ha.handle(processedRequest, res ...
- springmvc在使用@ModelAttribute注解获取Request和Response会产生线程并发不安全问题(转)
springmvc在获取Request和Response有很多方式:具体请看:https://www.cnblogs.com/wade-luffy/p/8867144.html 产生线程问题的代码如下 ...
- SpringMVC @ModelAttribute注解
/** * 1. 有 @ModelAttribute 标记的方法, 会在每个目标方法执行之前被 SpringMVC 调用! * 2. @ModelAttribute 注解也可以来修饰 ...
- @ModelAttribute注解(SpringMVC)
在方法定义上使用 @ModelAttribute 注解:Spring MVC 在调用目标处理方法前,会先逐个调用在方法级上标注了 @ModelAttribute 的方法. 在方法的入参前使用 @Mod ...
- SpringMvc中@ModelAttribute注解的使用
一.绑定请求参数到指定对象 public String test1(@ModelAttribute("user") UserModel user) 只是此处多了一个注解@Model ...
- SpringMVC的@ModelAttribute注解简单使用(用户修改信息)
例如有一个User对象,我们要修改他的值,但是不能修改他的密码!通过表单提交数据之后,password为null,会把原对象的passwod覆盖掉.这时候可以用@ModelAttribute注解处理. ...
- 二十七 集合!!!!!!!!set 二十八 文件
0: 不知道啊 去除重复元素? 1:frozenset 2:len(x) 3:直接一个错的报 4:好像一样 错 完全不一样的说 set = {[1,2]}这是想干嘛 :列表怎么可以进 ...
随机推荐
- iis配置问题
最近调试程序时发现一直用的是vs自带的服务器 当我切换成iis时,发现虽然能显示界面,却连不上数据库 (程序数据库的一系列操作是通过wcf ria完成的) 以前在winserver2012上也遇到过这 ...
- ajax执行成功后,在success回调函数中把后台返回的list还原到html的table中
需求描述:前台通过onclick触发ajax,到后台返回一个list(json格式的),把list插入到html的table中. 思路简介: ̄□ ̄|| 刚开始的时候,是没有思路的,就卡在了,怎么把 a ...
- Python基础之模块与包
一.模块 1.什么是模块? 一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 2.为何要使用模块? 如果你退出python解释器然后重新进入,那么你之前定义的函 ...
- Deal with Warning: mysqli::__construct(): (HY000/2002)
1.安装XAMPP之后,如果之前安装过MySQL或者 apache,启动XAMPP中的响应的服务的时候回出现报错, 出错的原因,具体看报错的原因,如果是端口占用,在配置中修改端口号,如果是提示 “Fo ...
- MySQL数据库查询中的特殊命令
第一: MySQL的安装 下载MySQL软件,修改安装路径之后 安装数据库MySQL5.7.18 第一步:数据库MySQL5.7.18可以在官网上下载对应的版本,下载地址:http://www.f ...
- tensorflow(1) 基础: 神经网络基本框架
1.tensorflow 的计算得到的是计算图graph import tensorflow as tf a=tf.constant([1.0,2.0]) b=tf.constant([3.0,4.0 ...
- 线性空间和异或空间(线性基)bzoj4004贪心+高斯消元优秀模板
线性空间:是由一组基底构成的所有可以组成的向量空间 对于一个n*m的矩阵,高斯消元后的i个主元可以构成i维的线性空间,i就是矩阵的秩 并且这i个主元线性无关 /* 每个向量有权值,求最小权极大线性无关 ...
- log4j2的log输出到tomcat/logs目录下及使用(转)
原文链接:http://blog.csdn.net/honghailiang888/article/details/50370252 原文作者: Herman-Hong 一.环境配置 log4j2. ...
- jQuery 常用的方法
<!DOCTYPE html><html lang="en"><head> <meta charset="utf-8" ...
- excel生成数据
Sub function1()Dim i As LongFor i = 1 To 1000000Cells(i, 1) = "A" & iCells(i, 2) = &qu ...