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. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 表格:表示信息变化的操作

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

  2. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 按钮:按钮大小

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

  3. [经验] 使用 jQuery+JSON 实现国际化

    技术选型关键词:  [spring boot] [jQuery] [JSON] [JSP] 前言: 关于国际化, 我在一开始的时候就有一个误解, 我认为所谓国际化就是编写一段高技术含量的代码, 然后这 ...

  4. django 自定义模版过滤器

    自定义的模版过滤器必须要放在app中,并且该app必须在INSTALLED_APPS中进行安装.然后再在这个app下面创建一个python包叫做templatetags(这个名字是固定的,不能随意更改 ...

  5. IPython 自动重载魔术

    在开启IPython 后输入下列命令就可以开启Ipython 的自动重载 %load_ext autoreload %autoreload 2 当你在IPython中导入的函数或类发生修改时,IPyt ...

  6. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 辅助类:快速浮动

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

  7. docker-lnmp 多容器部署 Laravel 方案分享(转)

    docker lnmp 多容器部署方案.完全基于 docker 官方镜像,遵循最佳实践,一容器一进程. github 项目地址 https://github.com/March7/docker-lnm ...

  8. 字符串题汇总(python3)

    1.最小编辑距离 假设有两个字符串s1和s2,计算通过增添.删除.替换三种操作后,从s1转变为s2所需要的操作次数. #coding=utf-8 class Solution: def editDis ...

  9. java随机函数用法Random

     原文地址:http://blog.csdn.net/wpjava/article/details/6004492  import java.util.Random; public class Ran ...

  10. 单选与多选与label

    单选radio和多选checkbox是用name属性关联的 相同的name就相当于同一道题 <input type="radio" name="radio" ...