SpringMVC(六)POJO类作为 @RequestMapping方法的参数
Command or form objects to bind request parameters to bean properties (via setters) or directly to fields, with customizable type conversion, depending on @InitBinder methods and/or the HandlerAdapter configuration. See the webBindingInitializer property on RequestMappingHandlerAdapter. Such command objects along with their validation results will be exposed as model attributes by default, using the command class class name - e.g. model attribute "orderAddress" for a command object of type "some.package.OrderAddress". The ModelAttribute annotation can be used on a method argument to customize the model attribute name used.
可以使用POJO类作为@RequestMapping方法的参数,SpringMVC会自动的将值,注入到POJO属性名与URL上参数名一致的属性中。并支持联级属性。
代码:
1: public class User {
2: private String name;
3: private Integer age;
4: private Address address;
5:
6: public String getName() {
7: return name;
8: }
9:
10: public void setName(String name) {
11: this.name = name;
12: }
13:
14: public Integer getAge() {
15: return age;
16: }
17:
18: public void setAge(Integer age) {
19: this.age = age;
20: }
21:
22: public Address getAddress() {
23: return address;
24: }
25:
26: public void setAddress(Address address) {
27: this.address = address;
28: }
29:
30: @Override
31: public String toString() {
32: return "User{" +
33: "name='" + name + '\'' +
34: ", age=" + age +
35: ", address=" + address +
36: '}';
37: }
38: }
39:
40: public class Address {
41: private String province;
42: private String city;
43:
44: public String getProvince() {
45: return province;
46: }
47:
48: public void setProvince(String province) {
49: this.province = province;
50: }
51:
52: public String getCity() {
53: return city;
54: }
55:
56: public void setCity(String city) {
57: this.city = city;
58: }
59:
60: @Override
61: public String toString() {
62: return "Address{" +
63: "province='" + province + '\'' +
64: ", city='" + city + '\'' +
65: '}';
66: }
67: }
68:
69: @Controller
70: public class TestPOJO {
71:
72: @RequestMapping("/testPOJO")
73: public String testPOJO(User user) {
74: System.out.println(user);
75: return "success";
76: }
77: }
URL:
1: <form action="testPOJO">
2: <label>name:</label> <input type="text" name="name"/>
3: <br/>
4: <label>age:</label> <input type="text" name="age"/>
5: <br/>
6: <label>province:</label> <input type="text" name="address.province"/>
7: <br/>
8: <label>city:</label> <input type="text" name="address.city"/>
9: <br/>
10: <input type="submit" value="testPOJO"/>
11: </form>
SpringMVC(六)POJO类作为 @RequestMapping方法的参数的更多相关文章
- 批量产生ssh2项目中hibernate带注解的pojo类的快捷方法
近几个月一直在忙于项目组的ios应用项目的开发,没有太多时间去研究web应用方面的问题了.刚好,昨天有网友问到如何批量产生hibernate带注解的pojo类的快捷方法,所谓批量就是指将当前数据库中所 ...
- springmvc使用pojo和servlet原生api作为参数
一.Pojo作为参数: 实体: package com.hy.springmvc.entities; public class User { private String username; priv ...
- C#通过反射获取类中的方法和参数个数,反射调用方法带参数
using System; using System.Reflection; namespace ConsoleApp2 { class Program { static void Main(stri ...
- Object类的wait方法带参数和notifyAll方法
相当于sleep( 5000 ) , 效果一样:
- SpringMVC中在Controller类的每个方法执行前调用某个方法的实现
在使用SpringMVC做项目的时候,如果想在@Controller类中每个@RequestMapping方法执行前都调用某个方法,要怎么实现呢?答案是使用Spring的@ModelAttribute ...
- python(六):面型对象--类的特殊方法
一.跟实例创建和执行有关的 __new__.__init__.__call__. 类加括号调用了__init__方法来创建一个实例对象.这一过程分成了两步: 类调用__new__来创建实例对象,__n ...
- 零基础学习java------38---------spring中关于通知类型的补充,springmvc,springmvc入门程序,访问保护资源,参数的绑定(简单数据类型,POJO,包装类),返回数据类型,三大组件,注解
一. 通知类型 spring aop通知(advice)分成五类: (1)前置通知[Before advice]:在连接点前面执行,前置通知不会影响连接点的执行,除非此处抛出异常. (2)正常返回通知 ...
- 详解SpringMVC中Controller的方法中参数的工作原理[附带源码分析]
目录 前言 现象 源码分析 HandlerMethodArgumentResolver与HandlerMethodReturnValueHandler接口介绍 HandlerMethodArgumen ...
- SpringMVC 基于注解的Controller @RequestMapping @RequestParam..
概述 继 Spring 2.0 对 Spring MVC 进行重大升级后,Spring 2.5 又为 Spring MVC 引入了注解驱动功能.现在你无须让 Controller 继承任何接口,无需在 ...
随机推荐
- 创建一个dynamics CRM workflow (六) - Debugging Custom Workflows
我们也deploy部署了custom workflows, debugging是开发当中不可或缺的一个步骤. debug workflow的步骤和debug有些许不一样: 1. install pro ...
- Elasticsearch学习(一)————简单命令
Elasticsearch一.简介**Elasticsearch 是一个分布式的搜索和分析引擎,可以用于全文检索.结构化检索和分析,并能将这三者结合起来.Elasticsearch 基于 Lucene ...
- 【leecode】宝石与石头
给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石. J 中的字母不重复,J 和 S中的所有字符都是字母 ...
- twemproxy 安装与配置
转自:http://blog.csdn.net/u011183653/article/details/21240749 说到twemproxy就不得不说要一下redis,这是因为twemproxy是为 ...
- web service服务是查询QQ用户是否在线
使用php5开发客户端: <?php try { //$client = new SoapClient("HelloService.wsdl",array('encoding ...
- 自己对WEBGL坐标系的转换过程的理解【如图】
- Project Euler 39 Integer right triangles( 素勾股数 )
题意:若三边长 { a , b , c } 均为整数的直角三角形周长为 p ,当 p = 120 时,恰好存在三个不同的解:{ 20 , 48 , 52 } , { 24 , 45 , 51 } , ...
- BZOJ 2161 布娃娃 (主席树)
题面 想了一个主席树做法 我们把每个区间的两个端点拆开 对$L,R$分别从小到大排序,分别从左到右依次把对应标号的$c_{i}$插入到权值主席树里 每次查询$p_{i}$,在排序后的$L,R$数组上分 ...
- [tyvj-1194]划分大理石 二进制优化多重背包
突然发现这个自己还不会... 其实也不难,就和快速幂感觉很像,把物品数量二进制拆分一下,01背包即可 我是咸鱼 #include <cstdio> #include <cstring ...
- [tyvj-1061]Mobile Service 动态规划
滚动数组优化一波. 原设计状态:表示三个员工分别的位置和执行到的任务. 考虑到:执行完第i个任务,一定有员工在pos[i],那么就可以压一维,空间复杂度就算不滚动数组也可以了. (喜闻乐见,今天第一次 ...