使用bean接收ajax表单提交数据包含文件上传
这几天写带图片上传的表单提交,一个配置小程序活动弹出框样式的功能,记录一下一些需要注意的地方
首先是 前端 JSP 文件的表单
<form class="search-wrapper" id="spEsActivitydialogForm" method="post" role="form">
<table width="100%" class="table6" id="spEsActivitydialogTable">
<tbody>
<tr>
<td>编号</td>
<td>
<input type="text" name="id" id="id" value="2" readonly="readonly" placeholder="自动生成"/>
</td>
<td>弹出框类型<span class="xi">*</span></td>
<td><select name="type" id="type" msgg="弹出框类型" >
<option value="1">统一弹出框</option>
</select></td>
<td>是否显示按钮2<span class="xi">*</span></td>
<td><select name="hasbtntwo" id="hasbtntwo" msgg="是否显示按钮2" onchange="hasbtntwoChange(this)">
<option value="0">否</option>
<option value="1">是</option>
</select></td>
</tr>
<tr>
<td>按钮一文字<span class="xi">*</span></td>
<td><input type="text" name="btnonetext" id="btnonetext" msgg="按钮一文字"/></td>
<td>按钮一颜色<span class="xi">*</span></td>
<td><input type="text" name="btnonecolor" id="btnonecolor" msgg="按钮一颜色"/></td>
<td>按钮二文字<span class="xi">*</span></td>
<td><input type="text" name="btntwotext" id="btntwotext" /></td>
</tr>
<tr>
<td>按钮二颜色<span class="xi">*</span></td>
<td><input type="text" name="btntwocolor" id="btntwocolor" /></td>
<td>按钮文字颜色<span class="xi">*</span></td>
<td><input type="text" name="btntextcolor" id="btntextcolor" msgg="按钮文字颜色"/></td>
<td>是否显示<span class="xi">*</span></td>
<td><select name="show" id="show" msgg="是否显示" >
<option value="0">否</option>
<option value="1">是</option>
</select></td>
</tr>
<tr>
<td>提示文字</td>
<td><input type="text" name="text" id="text" /></td>
<td>提示文字颜色</td>
<td><input type="text" name="textcolor" id="textcolor" /></td>
<td>模板ID<span class="xi">*</span></td>
<td><input type="text" name="csstemplate" id="csstemplate" class="e_number" msgg="模板ID" /></td>
</tr>
<tr>
<td>开始日期</td>
<td ><input type="text" name="begintime" id="begintime" class="e_ymdhms"/></td>
<td>结束日期</td>
<td ><input type="text" name="endtime" id="endtime" class="e_ymdhms"/></td>
<td>按钮一跳转链接</td>
<td><input type="text" name="gopageurl" id="gopageurl" /></td>
</tr>
<tr> <td>录入日期</td>
<td ><input type="text" name="bookdate" id="bookdate" class="e_ymdhms" /></td>
<td>录入人</td>
<td><input type="text" name="inputman" id="inputman" /></td>
<td>背景图片<span class="xi">*</span></td>
<td>
<a href="javascript:;" class="file">选择文件<input type="file" name="picture" id="picture" accept="image/*" multiple/></a>
<!-- <span class="showFileName" name="picture">图片上传尺寸大小</span> -->
<span class="fileerrorTip"></span><!-- 提示 -->
</td>
</tr>
</tbody>
</table>
</form>
然后是bean类,SpEsActivitydialog.java
public class SpEsActivitydialog implements java.io.Serializable { private Long id;
private Byte type;
private String bgimgurl;
private Byte hasbtntwo;
private String btnonetext;
private String btntwotext;
private String btntextcolor;
private String btnonecolor;
private String btntwocolor;
private String text;
private String textcolor;
private Byte csstemplate;
private String gopageurl;
private Byte show;
private Date begintime;
private Date endtime;
private Date bookdate;
private Long inputmanid;
private String inputman; public SpEsActivitydialog() {
} public SpEsActivitydialog(Long id, Byte type, String bgimgurl,
Byte hasbtntwo, String btnonetext, String btntwotext,
String btntextcolor, String btnonecolor, String btntwocolor,
String text, String textcolor, Byte csstemplate, String gopageurl,
Byte show, Date begintime, Date endtime,Date bookdate,Long inputmanid,String inputman) {
this.id = id;
this.type = type;
this.bgimgurl = bgimgurl;
this.hasbtntwo = hasbtntwo;
this.btnonetext = btnonetext;
this.btntwotext = btntwotext;
this.btntextcolor = btntextcolor;
this.btnonecolor = btnonecolor;
this.btntwocolor = btntwocolor;
this.text = text;
this.textcolor = textcolor;
this.csstemplate = csstemplate;
this.gopageurl = gopageurl;
this.show = show;
this.begintime = begintime;
this.endtime = endtime;
this.bookdate = bookdate;
this.inputmanid = inputmanid;
this.inputman = inputman;
}
@Id
@Column(name = "id", unique = true, nullable = false, precision = 10, scale = 0)
public Long getId() {
return this.id;
} public void setId(Long id) {
this.id = id;
}
@Column(name = "type", precision = 2, scale = 0)
public Byte getType() {
return this.type;
} public void setType(Byte type) {
this.type = type;
}
@Column(name = "bgimgurl", length = 500)
public String getBgimgurl() {
return this.bgimgurl;
} public void setBgimgurl(String bgimgurl) {
this.bgimgurl = bgimgurl;
}
@Column(name = "hasbtntwo", precision = 2, scale = 0)
public Byte getHasbtntwo() {
return this.hasbtntwo;
} public void setHasbtntwo(Byte hasbtntwo) {
this.hasbtntwo = hasbtntwo;
}
@Column(name = "btnonetext", length = 15)
public String getBtnonetext() {
return this.btnonetext;
} public void setBtnonetext(String btnonetext) {
this.btnonetext = btnonetext;
}
@Column(name = "btntwotext", length = 15)
public String getBtntwotext() {
return this.btntwotext;
} public void setBtntwotext(String btntwotext) {
this.btntwotext = btntwotext;
}
@Column(name = "btntextcolor", length = 10)
public String getBtntextcolor() {
return this.btntextcolor;
} public void setBtntextcolor(String btntextcolor) {
this.btntextcolor = btntextcolor;
}
@Column(name = "btnonecolor", length = 10)
public String getBtnonecolor() {
return this.btnonecolor;
} public void setBtnonecolor(String btnonecolor) {
this.btnonecolor = btnonecolor;
}
@Column(name = "btntwocolor", length = 10)
public String getBtntwocolor() {
return this.btntwocolor;
} public void setBtntwocolor(String btntwocolor) {
this.btntwocolor = btntwocolor;
}
@Column(name = "text", length = 100)
public String getText() {
return this.text;
} public void setText(String text) {
this.text = text;
}
@Column(name = "textcolor", length = 10)
public String getTextcolor() {
return this.textcolor;
} public void setTextcolor(String textcolor) {
this.textcolor = textcolor;
}
@Column(name = "csstemplate", precision = 2, scale = 0)
public Byte getCsstemplate() {
return this.csstemplate;
} public void setCsstemplate(Byte csstemplate) {
this.csstemplate = csstemplate;
}
@Column(name = "gopageurl", length = 30)
public String getGopageurl() {
return this.gopageurl;
} public void setGopageurl(String gopageurl) {
this.gopageurl = gopageurl;
}
@Column(name = "show", precision = 2, scale = 0)
public Byte getShow() {
return this.show;
} public void setShow(Byte show) {
this.show = show;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "begintime", length = 29)
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
public Date getBegintime() {
return this.begintime;
}
@JsonDeserialize(using=CustomDateDeserialize.class)
public void setBegintime(Date begintime) {
this.begintime = begintime;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "endtime", length = 29)
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
public Date getEndtime() {
return this.endtime;
}
@JsonDeserialize(using=CustomDateDeserialize.class)
public void setEndtime(Date endtime) {
this.endtime = endtime;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "bookdate", length = 29)
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
public Date getBookdate() {
return bookdate;
}
@JsonDeserialize(using=CustomDateDeserialize.class)
public void setBookdate(Date bookdate) {
this.bookdate = bookdate;
}
@Column(name = "inputmanid", precision = 10, scale = 0)
public Long getInputmanid() {
return inputmanid;
} public void setInputmanid(Long inputmanid) {
this.inputmanid = inputmanid;
}
@Column(name = "inputman", length = 10)
public String getInputman() {
return inputman;
} public void setInputman(String inputman) {
this.inputman = inputman;
} }
然后是JS 中ajax提交数据
//保存表单方法
function submitForm(){
var formData = new FormData($("#spEsActivitydialogForm")[0]); //直接把表单放入FormData()对象中
showLoading();
$.ajax({
type : "POST", //必须是POST
url : "saveSpEsActivitydialog?"+_csrfName+"="+_csrfValue,
data:formData,
async: false,
cache: false,
contentType: false, //必须有
processData: false, //必须有
success : function(data) {
if(data.code== 1){
location.href = "spEsActivitydialog";
}else{
clearLoading();
SimplePop.alert(data.msg);
}
},
error: function(){
clearLoading();
SimplePop.alert("操作失败");
}
}); }
最后 Controller 里配置
@ResponseBody
@RequestMapping(value = { "/SpEsActivitydialogAPI/saveSpEsActivitydialog" }, method = {RequestMethod.GET, RequestMethod.POST })
public ResultMsg saveSpEsActivitydialog(@RequestParam(value = "picture") MultipartFile[] picture,SpEsActivitydialog spEsActivitydialog, HttpServletRequest request) {
ResultMsg msg = new ResultMsg(0, "操作失败");
try {
...省略...
}catch (Exception e) {
msg =new ResultMsg(0, e.getMessage());
}
return msg;
}
过程中遇到几点
传到后台一直报400错误,就是转为javabean有问题,找了很久原来是时间类型的参数传递问题,javabean里在时间类型的参数上加上这两句就成功了
@JsonDeserialize
此注解用于setter方法上,用于在反序列化时可以嵌入我们自定义的代码
贴一下 CustomDateDeserialize.class里面的代码
public class CustomDateDeserialize extends JsonDeserializer<Date> { private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @Override
public Date deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException { Date date = null;
try {
if(!StringUtils.isBlank(jp.getText())){
date = sdf.parse(jp.getText());
}
} catch (ParseException e) {
sdf = new SimpleDateFormat("yyyy-MM-dd");
if(jp!=null){
try {
date = sdf.parse(jp.getText());
} catch (ParseException e1) {
}
}
}
return date;
}
}
使用bean接收ajax表单提交数据包含文件上传的更多相关文章
- Yii2表单提交(带文件上传)
今天写一个php的表单提交接口,除了基本的字符串数据,还带文件上传,不用说前端form标签内应该有这些属性 <form enctype="multipart/form-data&quo ...
- Java后台使用httpclient入门HttpPost请求(form表单提交,File文件上传和传输Json数据)
一.HttpClient 简介 HttpClient 是 Apache Jakarta Common 下的子项目,用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 ...
- js_ajax模拟form表单提交_多文件上传_支持单个删除
需求场景: 用一个input type="file"按钮上传多张图片,可多次上传,可单独删除,最后使用ajax模拟form表单提交功能提交到指定方法中: 问题:由于只有一个file ...
- ANDROID使用MULTIPARTENTITYBUILDER实现类似FORM表单提交方式的文件上传
最近在做 Android 端文件上传,要求采用 form 表单的方式提交,项目使用的 afinal 框架有文件上传功能,但是始终无法与php写的服务端对接上,无法上传成功.读源码发现:afinal 使 ...
- 用div漂浮快实现与表单无关的多文件上传功能。
我项目有这个需求,多文件上传,而且要及时显示到表单上,这样的话就不能与表单相关. 由于我对前端不熟,我就实现了这么一个功能,通过button触发一个div漂浮块,然后多文件上传,之后通过js把文件名显 ...
- c# 模拟表单提交,post form 上传文件、大数据内容
表单提交协议规定:要先将 HTTP 要求的 Content-Type 设为 multipart/form-data,而且要设定一个 boundary 参数,这个参数是由应用程序自行产生,它会用来识别每 ...
- c# 模拟表单提交,post form 上传文件、数据内容
转自:https://www.cnblogs.com/DoNetCShap/p/10696277.html 表单提交协议规定:要先将 HTTP 要求的 Content-Type 设为 multipar ...
- 给ajax表单提交数据前面加上实体名称
有时候我们后台做了一个引用类型例如: 下面的实体以C#为例 public class Order{ public string orderId{get;set;} public OrderItem o ...
- form 表单提交数据和文件(fromdata的使用方法)
<!-- 数据和文件一次性提交 --> <form class="form_meren" id="mainForm" name="m ...
随机推荐
- React路由基本的使用(一)
路由的基本使用 下载 npm i -D react-router react-router-dom APP.js ps==>NavLink 选中的时候 可以高亮 demo 是你自己添加的一个类哈 ...
- Android odex,oat文件的反编译,回编译
现在,许多Android手机的ROM包在生成过程中都启用优化,把jar文件抽空,生成odex/oat和vdex文件,以在运行时省掉编译时间.如果想对这些jar进行修改,就要修改它们所对应的odex或者 ...
- 魔兽争霸RPG地图开发速成教程
魔兽争霸RPG地图开发速成教程 1 打开WE编辑器 下载地址 http://rpg.dz.blizzard.cn/authors-home/editor-download 然后新建地图 2 打开工 ...
- Java编程思想——第17章 容器深入研究(一)
这一章将学习散列机制是如何工作的,以及在使用散列容器时怎么样编写hashCode()和equals()方法. 一.容器分类 先上两张图 来概况完整的容器分类 再细说都为什么会有那些特性. 二.Coll ...
- Spring Cloud @RefreshScope刷新问题
问题 使用@RefreshScope会刷新在sprign ioc中所有bean中使用@Value的值,但是在配置类中使用方法去配置的其他类参数并不会改变例如 解决方案 //使用此方法监听事件 @Eve ...
- [追热点]Rust学习资源整理
为什么选择Rust 在一次演讲中,谈到微软为解决相应内存问题所做的工作,微软研究人员 Matthew Parkinson 提到了微软正在开发的基于 Rust 的新编程语言 Verona. 摘自:[Ru ...
- python3 tornado api + angular8 + nginx 跨域问题
问题: 上一个博客部署好了api之后,前端开始吊发现了跨域的问题. 接口地址: http://111.231.201.164/api/houses 服务器上使用的是nginx转发 数据: 前端ang ...
- Android8.1 源码修改之插入SIM卡默认启用Volte功能
前言 公用电话产品,插入SIM卡后要求自动打开Volte功能,即插即用,用完拔卡就走 实现 第一步 开关对应的代码 通过打印日志和全局查找,源码位置 vendor/mediatek/proprieta ...
- 数据库表数据统计及数据表的数据大小统计SQL
USE [YourDBName] -- replace your dbname GO SELECT s.Name AS SchemaName, t.Name AS TableName, p.rows ...
- 阻止保存要求重新创建表的更改-只需设置SQLServer的一个设置