(转)SpringMVC表单多对象传递小技巧——@InitBinder
转:https://www.jianshu.com/p/59771cbf373d
1.问题情景
项目中前端后台的数据传递是必不可少的,比如说我们要在一张表单中提交对象,如果只是一个对象就就很好做,因为单独的对象中是很难存在相同的参数名的,但是如果我们想要提交多个对象,这些对象的参数名如果在项目的设计阶段可能会因为负责人沟通问题导致相同。
举个栗子:
再说主题之前我们先看看Struts2是如何解决这个问题的:
众所周知,Struts2采用了OGNL表达式,可以使用Object.Prarm形式对表单进行精准绑定入参,比如传student.id,后台就可以区分并接收到是student的id。Struts2我很不熟(因为没啥用),只知道这么一丢丢。
回到主题,我们主要是想看看在SpringMVC中我们怎么解决这个问题。
根据上面的情景,我们来搭建个问题,然后解决这个问题
先写Student和Course两个类,Student和Course里面都有id和note,参数名一样,但是意义很不同!!!
后台:
public class Student implements Serializable{
String id;
String note;
//get..set....
}
public class Course implements Serializable{
String id;
String note;
//set..get...
}
前端:
<form action="/test/test" method="get">
<input type="text" name="student.id" value="student_id">
<input type="text" name="student.name" value="student_name">
<input type="text" name="course.id" value="course_id">
<input type="text" name="course.name" value="course_name">
<input type="submit" value="提交">
</form>
2.解决问题
有请今天的主角——@InitBinder
有了它,我们就可以像Struts2一样,前端传Object.Prarm的形式了。当然在后台我们需要做些风骚操作!
@InitBinder("student")
public void initBinderStudent(WebDataBinder binder){
binder.setFieldDefaultPrefix("student.");
}
@InitBinder("course")
public void initBinderCourse(WebDataBinder binder){
binder.setFieldDefaultPrefix("course.");
}
@InitBinder() 中间的value值,用于指定表单属性或请求参数的名字,符合该名字的将使用此处的DataBinder。比如:student.id和student.note。student就得是中间的value值,这样才能接收得到。而且student会填充进WebDataBinder,这里binder对象就是student了。
注意binder.setFieldDefaultPrefix("student."),这里的"."千万别忘记了!!!
大家一定觉得这个操作太风骚了。先别着急用,这里说一下这个方法的缺陷:
首先,这个方法不能自定义路径,比如我想写:”/TianShenSchool/4thClass/{student.id}“ ,这就无法完成!
其次,如果给的是集合或者是数组,也无法使用。
当然如果真的出现上面两种情况的话,建议大家还是乖乖改一改数据结构和系统设计吧ㄟ( ▔, ▔ )ㄏ
3.代码
Student对象和Course对象:
public class Student implements Serializable{
String id;
String note;
//get..set....
}
public class Course implements Serializable{
String id;
String note;
//set..get...
}
html页面:
<form action="/test/test" method="get">
<input type="text" name="student.id" value="student_id">
<input type="text" name="student.name" value="student_name">
<input type="text" name="course.id" value="course_id">
<input type="text" name="course.name" value="course_name">
<input type="submit" value="提交">
</form>
Controller:
@Controller
@RequestMapping("/classtest")
public class TestController {
// 绑定变量名字和属性,参数封装进类
@InitBinder("student")
public void initBinderUser(WebDataBinder binder) {
binder.setFieldDefaultPrefix("student.");
}
// 绑定变量名字和属性,参数封装进类
@InitBinder("course")
public void initBinderAddr(WebDataBinder binder) {
binder.setFieldDefaultPrefix("course.");
} @RequestMapping("/methodtest")
@ResponseBody
public Map<String,Object> test(@ModelAttribute("student") Student student,@ModelAttribute("course") Course course){
Map<String,Object> map=new HashMap<String,Object>();
map.put("student", student);
map.put("course", course);
return map;
}
(转)SpringMVC表单多对象传递小技巧——@InitBinder的更多相关文章
- (转载)SPRINGMVC表单标签简介
SpringMVC表单标签简介 在使用SpringMVC的时候我们可以使用Spring封装的一系列表单标签,这些标签都可以访问到ModelMap中的内容.下面将对这些标签一一介绍. 在正式介绍Spri ...
- SpringMVC表单标签简介
在使用SpringMVC的时候我们可以使用Spring封装的一系列表单标签,这些标签都可以访问到ModelMap中的内容.下面将对这些标签一一介绍. 在正式介绍SpringMVC的表单标签之前,我们需 ...
- SpringMVC 表单验证
SpringMVC 表单验证 本章节内容很丰富,主要有基本的表单操作,数据的格式化,数据的校验,以及提示信息的国际化等实用技能. 首先看效果图 项目结构图 接下来用代码重点学习SpringMVC的表单 ...
- MyEclipse Spring 学习总结三 SpringMVC 表单处理
SpringMVC 表单处理的项目结构如下图所示: Student.java 文件 public class Student { private Integer age; private String ...
- 只有设置了 name 属性的表单元素才能在提交表单时传递它们的值
$(function () { var wait = $("<img src=\"\" alt=\"正在上传\"/>"); $( ...
- YII用户注冊和用户登录(二)之登录和注冊在视图通过表单使用YII小物件并分析
2 登录和注冊在视图通过表单使用YII小物件并分析 <?php $form = $this -> beginWidget('CActiveForm', array( 'enableClie ...
- SpringMVC表单或Json中日期字符串与JavaBean的Date类型的转换
SpringMVC表单或Json中日期字符串与JavaBean的Date类型的转换 场景一:表单中的日期字符串和JavaBean的Date类型的转换 在使用SpringMVC的时候,经常会遇到表单中的 ...
- Spring MVC(十)--通过表单序列化传递参数
通过表单序列化传递参数就是将表单数据转化成字符串传递到后台,序列化之后参数请求变成这种模式param1=value1&¶m2=value2,下面用代码实现. 1.创建表单 &l ...
- SpringMVC听课笔记(SpringMVC 表单标签 & 处理静态资源)
1.springmvc表单标签,可以快速开发,表单回显,但是感触不深 2.静态资源的获取,主要是要配置这个
随机推荐
- [Go] 在golang中使用正则表达式捕获子表达式
正则匹配并且可以捕获到()这个里面的子表达式的值,linux的grep命令没办法捕获子表达式的值,只能获取到整条正则匹配的内容 package main import "regexp&quo ...
- CodeForces 862B(思维+二分图染色)
题意 https://vjudge.net/problem/CodeForces-862B 给出n个点,n-1条边,求再最多再添加多少边使得二分图的性质成立 思路 因为题目是求的最多添加多少边,所以可 ...
- python操作队列
进行队列的操作,首先要引入queue这个库 一:设置队列(括号中是队列可容纳数据的多少,如果不设置,则可以一直增加) import queue q = queue.Queue(10) 二:添加/获取元 ...
- echarts常用说明
import { Injectable } from '@angular/core'; //模板option通用 let fff7 = '#fff'; //字体统一颜色rgba(255,255,255 ...
- 【分布式存储】Glusterfs快速搭建
目录 环境准备 步骤1,保证至少有三台服务器 步骤2,格式化和配置硬盘 步骤3,安装GlusterFS 步骤4,配置防火墙 步骤5,配置 trusted pool 步骤6,设置GlusterFS卷 步 ...
- 计算机组成原理——cache高速缓存存储器
转载自https://blog.csdn.net/chen1083376511/article/details/8187481 cache-高速缓存存储器 在主存与CPU之间插入一级或多级SRAM组成 ...
- 【linux】linux命令lsof和grep命令的配合使用---linux根据端口查看PID,根据PID关键字高亮显示
lsof命令,根据端口,查看进程PID lsof -i: ps命令+grep命令 --color参数,根据PID查看进程详情,高亮显示关键字 ps -ef | grep --color=always
- Spring Boot配置过滤器的两种方式
过滤器(Filter)是Servlet中常用的技术,可以实现用户在访问某个目标资源之前,对访问的请求和响应进行拦截,常用的场景有登录校验.权限控制.敏感词过滤等,下面介绍下Spring Boot配置过 ...
- c# winfrom 更新控件时停止刷新,解决闪烁问题
static Dictionary<Control, bool> m_lstFreezeControl = new Dictionary<Control, bool>(); / ...
- .net core入门-跨域访问配置
Asp.net Core 跨域配置 一般情况WebApi都是跨域请求,没有设置跨域一般会报以下错误 No 'Access-Control-Allow-Origin' header is prese ...