html页面

<form method='post' action='url'>
用户名 <input type='text' name='name'>
用户id <input type='text' name='id'>
食品名 <input type='text' name='name'>
食品id <input type='text' name='id'>
<input type='text' name='age'>
<input type='text' name='price'>
</form>

实体类

public class User{
  private String name;
   private String id;
  private Integer age;
 //getter 和setter
}
----------------------------------
public class Food{
   private String name;
   private String id;
  private Double price,
  //getter 和setter
}

controller

@requestMap(value={'/order/book'})
public string show(User u,Food f){}

在上述情况下User 和food都不能得到正确的name和id,或者说更本得不到

1.建立一个中间桥梁, 拆分有所的属性

建立一个中间桥梁UserFoodDto对象

public class UserFoodDto{
  private String uid; //用户id
  private String uname; //用户名
  private String fid; //食物id
  private String fname; //食物名称
  private Double price, //食物价格
  private Integer age; //用户年龄   
  //getter 和setter
}

修改前台页面的 name值

<form method='post' action='url'>
  用户id <input type='text' name='uid'>
  用户名 <input type='text' name='uname'>
  食品id <input type='text' name='fid'>
  食品名 <input type='text' name='fname'>
  <input type='text' name='age'>
  <input type='text' name='price'>
</form>

controller

@requestMapping(value={'/order/book'})
public string show(UserFoodDto dto){
  //创建用户和食物实例
  User u = new User();
  Food f = new Food();
//分别设置属性
  u.setUid(dto.getUid());
  f.setFid(dto.getFid());
  u.setName(dto.getUname());
  f.setName(dto.getFname());   u.setAge(dto.getAge);
  f.setPrice(dto.getPrice);
  .....
}

缺点是:如果数据量大,100百个字段,修改的地方很多,而且一个dto,拆分也很费力,因此不建议在数据量大的情况下使用

2.使用桥连接,拆分冲突的属性

前端页面

<form method='post' action='url'>
  用户名 <input type='text' name='uname'>
  用户id <input type='text' name='uid'>
  食品名 <input type='text' name='fname'>
  食品id <input type='text' name='fid'>
  <input type='text' name='age'>
  <input type='text' name='price'>
</form>

中间桥梁类

---将冲突的字段专门建立一个javaBean
public Class UFBridge{
  private String uname;
  private String uid;
  private String fname;
  private String fid;
}

controller

@requestMapping(value={'/order/book'})
public string show(User u,Food f,UFBridge ufb){
  u.setId(ufb.getUid);
  u.setName(ufb.getUname());   f.setId(ufb.getFid);
  f.setName(ufb.getUname());
}

3.创建一个类包含User和Food

vo对象

public class UserFoodVo{
  private User user;
  private Food food;
  //省略getter和setter方法
}

前台页面

<form method='post' action='url'>
  用户名 <input type='text' name='user.name'>
  用户id <input type='text' name='user.id'>
  食品名 <input type='text' name='food.name'>
  食品id <input type='text' name='food.id'>
  <input type='text' name='user.age'>
  <input type='text' name='food.price'>
</form>

controller

@requestMapping(value={'/order/book'})
public string show(UserFoodVo vo){}

解决Spring Mvc中接受参数绑定重名的方法的更多相关文章

  1. MVC中Action参数绑定的过程

    一.题外话 上一篇:MVC中Action的执行过程 ControllerContext 封装有了与指定的 RouteBase 和 ControllerBase 实例匹配的 HTTP 请求的信息. 二. ...

  2. 彻底解决Spring mvc中时间的转换和序列化等问题

    痛点 在使用Spring mvc 进行开发时我们经常遇到前端传来的某种格式的时间字符串无法用java8的新特性java.time包下的具体类型参数来直接接收. 我们使用含有java.time封装类型的 ...

  3. 彻底解决Spring mvc中时间类型的转换和序列化问题

    在使用Spring mvc 进行开发时我们经常遇到前端传来的某种格式的时间字符串无法用java8时间包下的具体类型参数来直接接收.同时还有一系列的序列化 .反序列化问题,在返回前端带时间类型的同样会出 ...

  4. spring mvc:练习 @RequestParam(参数绑定到控制器)和@PathVariable(参数绑定到url模板变量)

    spring mvc:练习 @RequestParam和@PathVariable @RequestParam: 注解将请求参数绑定到你的控制器方法参数 @PathVariable: 注释将一个方法参 ...

  5. Spring MVC的各种参数绑定方式(请求参数用基础类型和包装类型的区别)(转)

    1.基本数据类型(以int为例,其他类似): Controller代码: @RequestMapping("saysth.do") public void test(int cou ...

  6. Spring MVC 的请求参数获取的几种方法

    通过@PathVariabl注解获取路径中传递参数 @RequestMapping(value = "/{id}/{str}") public ModelAndView hello ...

  7. 关于spring MVC中加载多个validator的方法。

    首先讲下什么叫做validator: validator是验证器,可以验证后台接受的数据,对数据做校验. SpringMVC服务器验证有两种方式,一种是基于Validator接口,一种是使用Annot ...

  8. Spring MVC系列之模型绑定(SpringBoot)(七)

    前言 上一节我们在SpringBoot中启用了Spring MVC最终输出了HelloWorld,本节我们来讲讲Spring MVC中的模型绑定,这个名称来源于.NET或.NET Core,不知是否恰 ...

  9. Spring MVC中forward请求转发2种方式(带参数)

    Spring MVC中forward请求转发2种方式(带参数) http://www.51gjie.com/javaweb/956.html  

随机推荐

  1. python基础面试题1

    Python面试重点(基础篇) 注意:只有必答题部分计算分值,补充题不计算分值. 第一部分 必答题(每题2分) 简述列举了解的编程语言及语言间的区别? c语言是编译型语言,运行速度快,但翻译时间长py ...

  2. Linux centosVMware shell脚本中的逻辑判断、文件目录属性判断、if特殊用法、case判断

    一.shell脚本中的逻辑判断 格式1:if 条件 ; then 语句; fi 格式2:if 条件; then 语句; else 语句; fi 格式3:if …; then … ;elif …; th ...

  3. boost::property_tree 读取ini配置

    应用场景: 在后端服务器项目开发中,需要初始化一个Socket服务器,需要IP地址与对应端口号等参数:另外还可能因为对接数据库,就还需要数据库的相关配置参数,如我使用的是MySql数据库,就需要数据库 ...

  4. C语言数组成绩排序

    #include<stdio.h> #define N 10 int main() { int s,i,j,tmp; int a[10]={78,56,38,99,81,86,39,100 ...

  5. spark动态资源(executor)分配

    spark动态资源调整其实也就是说的executor数目支持动态增减,动态增减是根据spark应用的实际负载情况来决定. 开启动态资源调整需要(on yarn情况下) 1.将spark.dynamic ...

  6. 吴裕雄--天生自然JAVA面向对象高级编程学习笔记:instanceof关键字

    class A{ // 定义类A public void fun1(){ // 定义fun1()方法 System.out.println("A --> public void fun ...

  7. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 图片:缩略图功能

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  8. java程序题目解析

    (选择一项) A: 不能有括号 B: C: 确定最后一位 D: 正确答案是 B  本题考查的是Java数组概念,数组下标是从零开始的,但是数据下标的总量和数据长度相同 (选择一项) A: B: 顺序不 ...

  9. springboot项目启动报错 Failed to configure a DataSource: 'url' attribute is not specified and no embedde

    参考:https://blog.csdn.net/Coyotess/article/details/80637837

  10. 看雪hello

    在看雪做了一道题目很简单,但是还是记录一下自己的学习. 用ida打开,然后shift+F12查看 这里可以看到基本的结构,转到pass查看 发现ATA XREF: sub_401770+Bo打开这里 ...