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";}@ModelAttributepublic 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:

package com.tiekui.springmvc.pojo;public class UserNew {private String username;private String password;private String email;private int age;private Integer id;public UserNew(String username, String password, String email, int age, Integer id, Address address) {super();this.username = username;this.password = password;this.email = email;this.age = age;this.id = id;this.address = address;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}private Address address;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 String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public Address getAddress() {return address;}public void setAddress(Address address) {this.address = address;}@Overridepublic String toString() {return "UserNew [username=" + username + ", password=" + password + ", email=" + email + ", age=" + age+ ", id=" + id + ", address=" + address + "]";}}
View Code

 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;}@Overridepublic 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;}
}

 

转载于:https://www.cnblogs.com/zhoutiekui/p/6351254.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/270092.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

硬件知识:U盘缩水是怎么回事,如何恢复U盘真实容量?

使用缩水的U盘会造成什么后果呢&#xff1f;使用缩水U盘拷贝资料&#xff0c;很有可能造成重要资料文件的丢失&#xff0c;同时“无限制”拷贝资料&#xff0c;造成不可预知的错误。那要怎么办呢&#xff1f;其实最有效方法就是还原缩水U盘真实容量&#xff0c;这样在拷贝资料时…

我知道我要在这条路上走下去。 我不知道我能走多远&#xff0c;能走到什么程度。 所以&#xff0c;在我还尚未开始之时&#xff0c;留下一笔。 也给此后的&#xff0c;若想要放弃的日子&#xff0c;一点勇气。 若有一天&#xff0c;我能站在高一点的地方向下看&#xff0c;我定…

网络知识:内网、外网、宽带、带宽、流量、网速之间的联系?

相信很多人都不知道内网、外网、宽带、带宽、流量、网速之间的区别与联系&#xff0c;今天小编给大家介绍一下&#xff0c;希望对大家能有所帮助&#xff01; 一.带宽与宽带的区别是什么&#xff1f; 带宽是量词&#xff0c;指的是网速的大小&#xff0c;比如1Mbps的意思是一兆…

马云正传——读书笔记

总结 网上有人说&#xff0c;马云是靠忽悠起家的&#xff0c;之前也是半信半疑&#xff0c;毕竟对他了解的不多&#xff0c;直到看了这本书&#xff0c;才对马云才有了比较深的理解。至于忽悠也不能说完全没有&#xff0c;但如果说全靠忽悠&#xff0c;那是不可能的&#xff0c…

Java技术:SpringBoot实现邮件发送功能

目录 1、创建一个基本的SpringBoot项目&#xff0c;pom文件导入发送邮件的依赖 2、application.yml 文件配置配置邮件发送信息 3、创建IEmailService 接口文件&#xff0c;定义邮件发送的接口 4、创建IEmailService接口的实现类EmailService.java 文件 5、新建邮件发送模板 ema…

电脑技巧:Win10无线投屏功能介绍

Win10操作系统可以将电脑中的内容投屏到其他显示设备&#xff0c;比如将电脑屏幕投屏到电视上&#xff0c;这是通过Miracast技术来实现的。其实Win10电脑自身也可以作为被投屏的那一方&#xff01;比如可以将手机屏幕投屏到电脑屏幕上去&#xff0c;方便给客户演示APP功能或者将…

电脑技巧:Win10系统中的这六种模式介绍

目录 1. 夜间模式 2. 游戏模式 3. 飞行模式 4. 卓越模式 5. 专注模式 6. 平板模式 在Win10中&#xff0c;隐藏着很多不为人知的小秘密。有些小功能虽然看起来不起眼&#xff0c;但关键时候却能让我们的Win10跑得更爽&#xff0c;比如win10中六种不同的模式&#xff0c;该什么时…

php 常用的系统函数

字符串函数 strlen&#xff1a;获取字符串长度&#xff0c;字节长度 substr_count 某字符串出现的次数 substr&#xff1a;字符串截取&#xff0c;获取字符串&#xff08;按照字节进行截取&#xff09; mb_strlenmb_substr strchr&#xff1a;与substr相似&#xff0c;从指定位…

电脑卡顿,最先升级这个硬件,运行速度可快速提升

电脑用着用着就变慢了&#xff0c;不少人为之苦恼。有钱的人儿早早换上了新电脑&#xff0c;没钱的人儿仍然在苦苦地支撑着~ 但是&#xff0c;电脑卡顿就跟汽车变速箱坏了一样&#xff0c;我们可以为其更换零件&#xff0c;从而“治好了”它&#xff0c;使用寿命不就延长了吗&a…

Java技术:SpringBoot集成FreeMarker生成word文件

今天给大家分享SpringBoot集成FreeMarker模板引擎生成word文件的用法&#xff0c;感兴趣的可以学一下&#xff0c;完整源码地址在文章末尾处&#xff0c;欢迎互相沟通交流&#xff01; 一、什么是FreeMarker&#xff1f; FreeMarker 是一款开源的模板引擎&#xff1a;是一种基于…

LinkedHashMap和HashMap的比较使用 详解

由于现在项目中用到了LinkedHashMap&#xff0c;并不是太熟悉就到网上搜了一下。 import java.util.HashMap;import java.util.Iterator;import java.util.LinkedHashMap;import java.util.Map;public class TestLinkedHashMap {public static void main(String args[]){System…

电脑技巧:Win10自带远程控制软件介绍

Win10本身就自带了远程协助功能&#xff01;这在Win10中叫做“快速助手”&#xff0c;你是不是从来没有注意到呢&#xff1f; Win10的快速助手使用非常简单。通过搜索功能&#xff0c;直接就可以找到它。 通过搜索可以找到快速助手 说明&#xff1a;快速助手使用的是Windows的远…

网络技巧:手机信号满格,上网却很慢,教你关闭双频优选开关,网速飞快

目前大部分家里都安装了宽带&#xff0c;大家最常用的就是百兆千兆的宽带网络&#xff0c;但是也避免不了家里面使用WiFi上网的时候&#xff0c;网络会时好时坏&#xff0c;有些时候就算是我们手机信号显示的是满格&#xff0c;但是上网的速度也还是很慢&#xff0c;今天小编就…