解决Spring Mvc中接受参数绑定重名的方法
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中接受参数绑定重名的方法的更多相关文章
- MVC中Action参数绑定的过程
一.题外话 上一篇:MVC中Action的执行过程 ControllerContext 封装有了与指定的 RouteBase 和 ControllerBase 实例匹配的 HTTP 请求的信息. 二. ...
- 彻底解决Spring mvc中时间的转换和序列化等问题
痛点 在使用Spring mvc 进行开发时我们经常遇到前端传来的某种格式的时间字符串无法用java8的新特性java.time包下的具体类型参数来直接接收. 我们使用含有java.time封装类型的 ...
- 彻底解决Spring mvc中时间类型的转换和序列化问题
在使用Spring mvc 进行开发时我们经常遇到前端传来的某种格式的时间字符串无法用java8时间包下的具体类型参数来直接接收.同时还有一系列的序列化 .反序列化问题,在返回前端带时间类型的同样会出 ...
- spring mvc:练习 @RequestParam(参数绑定到控制器)和@PathVariable(参数绑定到url模板变量)
spring mvc:练习 @RequestParam和@PathVariable @RequestParam: 注解将请求参数绑定到你的控制器方法参数 @PathVariable: 注释将一个方法参 ...
- Spring MVC的各种参数绑定方式(请求参数用基础类型和包装类型的区别)(转)
1.基本数据类型(以int为例,其他类似): Controller代码: @RequestMapping("saysth.do") public void test(int cou ...
- Spring MVC 的请求参数获取的几种方法
通过@PathVariabl注解获取路径中传递参数 @RequestMapping(value = "/{id}/{str}") public ModelAndView hello ...
- 关于spring MVC中加载多个validator的方法。
首先讲下什么叫做validator: validator是验证器,可以验证后台接受的数据,对数据做校验. SpringMVC服务器验证有两种方式,一种是基于Validator接口,一种是使用Annot ...
- Spring MVC系列之模型绑定(SpringBoot)(七)
前言 上一节我们在SpringBoot中启用了Spring MVC最终输出了HelloWorld,本节我们来讲讲Spring MVC中的模型绑定,这个名称来源于.NET或.NET Core,不知是否恰 ...
- Spring MVC中forward请求转发2种方式(带参数)
Spring MVC中forward请求转发2种方式(带参数) http://www.51gjie.com/javaweb/956.html
随机推荐
- Linux 运维常用命令
参考: https://segmentfault.com/a/1190000009745139 http://blog.51cto.com/xuqq999/774714 .查看有多少个IP访问: aw ...
- 新闻网大数据实时分析可视化系统项目——11、MySQL安装
1.修改yum源 鉴于用国外的Yum源,速度比较慢,所以想到将国外的yum源改为国内的Yum源,这里选择使用比较多的阿里云源.具体修改方法可以参考此连接 2.在线安装mysql 通过yum在线mysq ...
- Hadoop操作经验
系统日志文件写入到MySQL中,NoSQL中一般存储独立的关联性不大的非业务数据. 单个NameNode也可以恢复,从SecondaryNameNode恢复:两个NameNode是可以做负载均衡:更高 ...
- sentinel控制台
下载sentinel源码包:https://github.com/alibaba/Sentinel/tree/master,根据自己需要下载不同版本的分支,博主下载得是1.6 下载后解压,然后进入se ...
- formatTime() 时间戳,返回数据是计算距离现在的时间
const formatTime=function(tiem) {//时间转换 const timestamp = Date.now(); return function (tiem) { ...
- 网站Webshell大马密码极速暴力爆破工具-cheetah
Cheetah是一个基于字典的暴力密码webshell工具,运行速度与猎豹猎物一样快. Cheetah的工作原理是能根据自动探测出的web服务设置相关参数一次性提交大量的探测密码进行爆破,爆破效率 ...
- 码云上部署hexo博客框架
title: 码云上部署hexo博客框架 Hexo框架在码云上实现个人博客 本文受 https://www.jianshu.com/p/84ae2ba1c133 启发编写 本地调试 安装完Node.j ...
- HDFS 命令行基本操作
1.hdfs命令行 (1)查看帮助 hdfs dfs -help (2)查看当前目录信息 hdfs dfs -ls / (3)上传文件 hdfs dfs -put /本地路径 /hdfs路径 (4)剪 ...
- docker-compose 修改zabbix images 添加微信报警插件 时间同步 中文乱码 添加grafana美化zabbix
我们先来看一下我们要修改得 zabbix.yaml github https://github.com/bboysoulcn/awesome-dockercompose ve ...
- R语言 方差稳定化变换与线性变换 《回归分析与线性统计模型》page96
> rm(list = ls()) > A=read.csv("data96.csv") > A Y N 1 11 0.0950 2 7 0.1920 3 7 0 ...