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>
时间: 2024-11-08 14:02:32

SpringMVC(六)POJO类作为 @RequestMapping方法的参数的相关文章

批量产生ssh2项目中hibernate带注解的pojo类的快捷方法

近几个月一直在忙于项目组的ios应用项目的开发,没有太多时间去研究web应用方面的问题了.刚好,昨天有网友问到如何批量产生hibernate带注解的pojo类的快捷方法,所谓批量就是指将当前数据库中所有数据表全部一次性生成带注解的pojo类,假设当前数据库friend有50个数据表,如果一个个的新建产生pojo类的话,那就是说要手工输入新建50个pojo类后,还有输入每个类带注解的代码,并且还要写好有关联的类之间的映射关系,虽然,从学习的角度来说,手工写是个不错的主意,但是从开发效率和开发成本来

springmvc使用pojo和servlet原生api作为参数

一.Pojo作为参数: 实体: package com.hy.springmvc.entities; public class User { private String username; private String password; private String email; private Address address; public String getUsername() { return username; } public void setUsername(String us

C#通过反射获取类中的方法和参数个数,反射调用方法带参数

using System; using System.Reflection; namespace ConsoleApp2 { class Program { static void Main(string[] args) { //反射获取 命名空间+类名 string className = "ConsoleApp2.ClassSample"; string methodName = "test1"; //传递参数 Object[] paras = new Obje

第五课 Struts的控制器【续】Action类的execute()方法

1.Action类的execute()方法: public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException : 2.Action类的execute()方法的参数和返回值: – execute()方法包含以下参数: –ActionMap

python接口自动化测试六:类和方法

# coding:utf-8 import requests import re from bs4 import BeautifulSoup # s = requests.session()  # 全局的s def get_token(s): ''' fuction: 获取token args: s 参数 ->s = requests.session() :return  anti_token  ->{'X-Anit-Forge-Token': 'xx', 'X-Anit-Forge-Code

详解SpringMVC中Controller的方法中参数的工作原理

前言 SpringMVC是目前主流的Web MVC框架之一. 如果有同学对它不熟悉,那么请参考它的入门blog:http://www.cnblogs.com/fangjian0423/p/springMVC-introduction.html SpringMVC中Controller的方法参数可以是Integer,Double,自定义对象,ServletRequest,ServletResponse,ModelAndView等等,非常灵活.本文将分析SpringMVC是如何对这些参数进行处理的,

详解SpringMVC中Controller的方法中参数的工作原理[附带源码分析] good

目录 前言 现象 源码分析 HandlerMethodArgumentResolver与HandlerMethodReturnValueHandler接口介绍 HandlerMethodArgumentResolver与HandlerMethodReturnValueHandler接口的具体应用 常用HandlerMethodArgumentResolver介绍 常用HandlerMethodReturnValueHandler介绍 本文开头现象解释以及解决方案 编写自定义的HandlerMet

【MVC - 参数原理】详解SpringMVC中Controller的方法中参数的工作原理[附带源码分析]

前言 SpringMVC是目前主流的Web MVC框架之一. 如果有同学对它不熟悉,那么请参考它的入门blog:http://www.cnblogs.com/fangjian0423/p/springMVC-introduction.html SpringMVC中Controller的方法参数可以是Integer,Double,自定义对象,ServletRequest,ServletResponse,ModelAndView等等,非常灵活.本文将分析SpringMVC是如何对这些参数进行处理的,

详解SpringMVC中Controller的方法中参数的工作原理——基于maven

转自:http://www.tuicool.com/articles/F7byQn 前言 SpringMVC是目前主流的Web MVC框架之一. 如果有同学对它不熟悉,那么请参考它的入门blog:http://www.cnblogs.com/fangjian0423/p/springMVC-introduction.html SpringMVC中Controller的方法参数可以是Integer,Double,自定义对象,ServletRequest,ServletResponse,ModelA