1、@ModelAttribute注解

@ModelAttribute主要有三个用途,对方法进行注解,对参数进行注解,还有@ModelAttribute和@RequestMapping一起对方法进行注解。

(1) 对方法进行注解

@ModelAttribute对方法进行注解,有两个作用,一是在调用@RequestMapping注解的方法之前,先调用@ModelAttribute注解的方法,二是在@ModelAttribute注解的方法中,所有Map的对象都放入ImpliciteModel中,key就是Map的key。在后面讲解POJO传参的过程中,会讲解ImpliciteModel的作用。测试代码如下:

两个POJO代码如下:、

User.java

 package com.hxg.springmvc.entries;

 public class User {
private String username;
private String password;
private int id; public int getId() {
return id;
} @Override
public String toString() {
return "User [username=" + username + ", password=" + password
+ ", id=" + id + "]";
} public void setId(int id) {
this.id = id;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public User(int id, String username, String password) {
super();
this.username = username;
this.password = password;
this.id = id;
} public User() {
super();
} }

Cat.java

 package com.hxg.springmvc.entries;

 public class Cat {
private int speed; public int getSpeed() {
return speed;
} public void setSpeed(int speed) {
this.speed = speed;
} }

Controller的代码如下

 package com.hxg.springmvc.web.controller;

 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.SessionAttributes; import com.hxg.springmvc.entries.Cat;
import com.hxg.springmvc.entries.User; @Controller
public class HandlerController { @RequestMapping("/hello")
public String hello(@ModelAttribute("users")User user, Map<String, Object> map, Cat cat, String password)
{
cat.setSpeed(100);
System.out.println(password);
return "hello";
} @ModelAttribute
public String preUser(Cat cat, User user, Map<String, Object> map, String username)
{
System.out.println(username);
cat.setSpeed(110);
user.setId(1);
User user1 = new User(2, "username1", "password1");
map.put("users", user1);
return "abc";
} }

提交表单的部分代码

 <form action="hello.do" method="post">
<input name="username" type="text"/>
<input name="password" type="text"/>
<input type="submit" value="submit" >
</form>

页面显示代码:

 <body>
user:
${user.username }
${user.password }
${user.id }
<br><br>
users:
${users.username }
${users.password }
${users.id }
<br><br>
string:
${string}
<br><br>
cat:
${cat.speed }
<br><br>
username:
${username }
<br><br>
password:
${password }
</body>

在表单中输入username, password,页面显示的结果如下:

控制台中输出:username  password

从结果可以看出:@ModelAttribute注解的方法在@RequestMapping注解的方法之前执行,并且preUser方法中,一共有四个对象放入map中,相当于:

map.put("cat", cat)、map.put("user", user)、map.put("users", user1)和map.put("string", "abc");

POJO在传参的过程中,springmvc会默认的把POJO放入到map中,其中键值就是类名的第一个字母小写。在@ModelAttribute注解的方法里,POJO放入到Map的同时,也放入ImpliciteModel中, 比如上面代码中的user和cat。@ModelAttribute注解的方法里,返回类型不是void,则返回的值也会被放到Map中,其中键值为返回类型的第一个字母小写。比如上述代码中,返回的"abc",就会被放入到Map中,相当于map.put("string", "abc")。

  在执行@ModelAttribute注解的方法里,表单的数据会被当作参数传到@ModelAttribute注解的方法,和@RequestMapping注解的方法传参是一样的。

(2) @ModelAttribute对参数进行注解

比如上面的代码,@ModelAttribute("users")User user。在传参的过程中,首先检查ImpliciteModel有没有键值为users,有的话,直接从ImpliciteModel中取出该对象,然后在把表单传过来的数据赋值到该对象中,最后把该对象当作参数传入到@RequestMapping注解方法里,也就是hello方法。当检查到键值的话,并不会创建新的对象,而是直接从ImpliciteModel直接取出来。

(3) @ModelAttribute和@RequestMapping一起对方法进行注解

@ModelAttribute和@RequestMapping对方法进行注解时,其中返回类型被到Map中,并不会被当作视图的路径进行解析。把controller代码改变成如下:

 package com.hxg.springmvc.web.controller;

 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.SessionAttributes; import com.hxg.springmvc.entries.Cat;
import com.hxg.springmvc.entries.User; @Controller
public class HandlerController { @ModelAttribute
@RequestMapping("/hello")
public String hello(@ModelAttribute("users")User user, Map<String, Object> map, Cat cat, String password)
{
cat.setSpeed(100);
System.out.println(password);
return "bbbbb";
} @ModelAttribute
public String preUser(Cat cat, User user, Map<String, Object> map, String username)
{
System.out.println(username);
cat.setSpeed(110);
user.setId(1);
User user1 = new User(2, "username1", "password1");
map.put("users", user1);
return "abc";
} }

其中返回的视图路径就是@RequestMapping("/hello")中的hello,显示结果如下:

从结果中,bac变为了bbbbb,主要是因为返回值会被放入到map,键值为返回类型第一个字母的小写,原来的map.put("string", "abc")被覆盖掉,变成map.put("string", "bbbbb")。

2、POJO传参的过程

POJO传参的过程中,先检查ImpliciteModel中是否有相对应的键值,有的话就把该键值的对象取出来,把表单传过来的数据传到该对象,然后把该对象作为参数传到目标方法中,也就是@RequestMapping注解的方法中。在ImpliciteModel没有相对应的键值,假如controller用SessionAttribute进行注解,则就会在Session attribute查找相对应的key,假如找到了key,却没有对象,则会报异常。在ImpliciteModel和SessionAttribute都没有查找到key,才会创建新的对象,把表单传过来的数据赋值给新的对象,最后把新的对象作为参数传到目标方法中。

以下是报异常的代码

 package com.hxg.springmvc.web.controller;

 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.SessionAttributes; import com.hxg.springmvc.entries.Cat;
import com.hxg.springmvc.entries.User; @SessionAttributes("user")
@Controller
public class HandlerController { @ModelAttribute
@RequestMapping("/hello")
public String hello(@ModelAttribute User user, Map<String, Object> map, Cat cat, String password)
{
cat.setSpeed(100);
System.out.println(password);
return "bbbbb";
}
}

会报如下异常:

只要把@SessionAttributes("user")改成@SessionAttributes("users")就可以消去异常,或者把public String hello(@ModelAttribute User user, Map<String, Object> map, Cat cat, String password)中的@ModelAttribute 去掉,只有用@ModelAttribute注解参数,才会从session attribute中查找相对应的key。

@ModelAttribute注解和POJO传参过程的更多相关文章

  1. @ModelAttribute 注解及 POJO入参过程

    一.modelattribute注解 @ModelAttribute注解的方法有两种,一种无返回值,一种有返回值,方法的可以用@RequestParam注解来获取请求的参数,如果不获取参数,可以不用此 ...

  2. C/C++函数调用时传参过程与可变参数实现原理

    C/C++函数调用时传参过程与可变参数实现原理 C语言的经典swap问题 在学习C语言的时候,我们大都遇到过一些经典例题,这些经典例题背后所代表的是往往是C/C++背后的一些运行原理,比如下面这个示例 ...

  3. Spring MVC POJO传参方式

    有两POJO类 Address.java package com.proc; public class Address { private String province; private Strin ...

  4. SpringMVC POJO传参方式

    有两POJO类 Address.java package com.proc; public class Address { private String province; private Strin ...

  5. url传参过程中文字需编码、解码使用

    1.链接进行编码跳转:window.location.href = encodeURI(url) 2.获取当前链接进行解码:decodeURI(window.location); 3.获取url中参数 ...

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

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

  7. 关于url传参中文乱码问题

    之前都一直很不了解中文编码得问题,之前在做项目中没碰到那么头痛的问题.所以一直没有了解中文乱码的问题. 问题描述: 地址: http://localhost:8080/sun-government/c ...

  8. 学习笔记:JavaScript传参方式———ECMAScript中所有函数的参数都是按值传递

    我们把命名参数(arguments)视为局部变量,在向参数传递基本类型值时,如同基本类型变量的复制一样,传递一个副本,参数在函数内部的改变不会影响外部的基本类型值.如: function add10( ...

  9. (1)构造方法和方法重载 (2)this关键字 (3)方法的传参和递归调用

    1.构造方法和方法重载如: Person p = new Person(); - 声明Person类型的引用p指向Person类型的对象 p.show(); - 调用名字为show()的成员方法 1. ...

随机推荐

  1. How to Restart Qt Application

    How to restart QtApplication As we know, Restarting Application means to exit current application, t ...

  2. [Linux] Big-endian and Little-endian (大小端模式)

    Big-endian Little-endian 大小端模式   https://en.wikipedia.org/wiki/Endianness 大端模式,是指数据的高字节保存在内存的低地址中,而数 ...

  3. Android 集成支付宝第三方登录

    前言: 在集成支付宝支付的时候遇到一点小麻烦,先在此记录供大家参考 1.授权 支付宝第三方登录需要在后台进行授权,在查看授权的时候我们一定要看清楚时候真的已经获得了权限(我在没有获取权限的情况下集成的 ...

  4. event.preventDefault() vs. return false

    使用jquery方式的话,以下是等效的 return false === event.stopPropagation + event.preventDefault() //1. event.preve ...

  5. 6 Django 的视图层

    视图函数 一个视图函数,简称视图,是一个简单的 Python 函数,它接受 Web 请求并且返回 Web 响应.响应可以是一张网页的 HTML 内容,一个重定向,一个 404 错误,一个 XML 文档 ...

  6. Linux系统下安装ncurses库

    ncurses库是一个Linux系统下的图形支持的函数库,字符终端处理库,包括面板和菜单. 今天在安装ncurses库的时候遇到了一些问题,现将遇到的问题所叙如下: 首先说明:本次安装采用的是源码包的 ...

  7. 三)Wiring up jobs using triggers and the SchedulerFactoryBean

    示例地址: https://github.com/witaste/quartz.git │ pom.xml │ └─src └─main ├─java │ └─cn │ └─zno │ └─job │ ...

  8. PHP(八)数组

  9. TogetherJS本地部署,基于websocket的网页即时视频、语音、文字聊天

    TogetherJS分为两大部分,一个是hu文件夹中的服务端:另外一个是TogetherJS文件夹中的Together.JS文件,包含了所有的网页文字.语音等操作. 需要预先安装Node.js,可以百 ...

  10. R12 更新对应用户的字符集

    R12 更新对应用户的字符集     症状:应用系统数据导出操作,经常发生导出文件(XLS / TSV)产生简体中文乱码. 方案:针对客户端当前用户进行字符集更新 ZHS16GBK,而不影响其他用户. ...