这几天写带图片上传的表单提交,一个配置小程序活动弹出框样式的功能,记录一下一些需要注意的地方

首先是 前端 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表单提交数据包含文件上传的更多相关文章

  1. Yii2表单提交(带文件上传)

    今天写一个php的表单提交接口,除了基本的字符串数据,还带文件上传,不用说前端form标签内应该有这些属性 <form enctype="multipart/form-data&quo ...

  2. Java后台使用httpclient入门HttpPost请求(form表单提交,File文件上传和传输Json数据)

    一.HttpClient 简介 HttpClient 是 Apache Jakarta Common 下的子项目,用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 ...

  3. js_ajax模拟form表单提交_多文件上传_支持单个删除

    需求场景: 用一个input type="file"按钮上传多张图片,可多次上传,可单独删除,最后使用ajax模拟form表单提交功能提交到指定方法中: 问题:由于只有一个file ...

  4. ANDROID使用MULTIPARTENTITYBUILDER实现类似FORM表单提交方式的文件上传

    最近在做 Android 端文件上传,要求采用 form 表单的方式提交,项目使用的 afinal 框架有文件上传功能,但是始终无法与php写的服务端对接上,无法上传成功.读源码发现:afinal 使 ...

  5. 用div漂浮快实现与表单无关的多文件上传功能。

    我项目有这个需求,多文件上传,而且要及时显示到表单上,这样的话就不能与表单相关. 由于我对前端不熟,我就实现了这么一个功能,通过button触发一个div漂浮块,然后多文件上传,之后通过js把文件名显 ...

  6. c# 模拟表单提交,post form 上传文件、大数据内容

    表单提交协议规定:要先将 HTTP 要求的 Content-Type 设为 multipart/form-data,而且要设定一个 boundary 参数,这个参数是由应用程序自行产生,它会用来识别每 ...

  7. c# 模拟表单提交,post form 上传文件、数据内容

    转自:https://www.cnblogs.com/DoNetCShap/p/10696277.html 表单提交协议规定:要先将 HTTP 要求的 Content-Type 设为 multipar ...

  8. 给ajax表单提交数据前面加上实体名称

    有时候我们后台做了一个引用类型例如: 下面的实体以C#为例 public class Order{ public string orderId{get;set;} public OrderItem o ...

  9. form 表单提交数据和文件(fromdata的使用方法)

    <!-- 数据和文件一次性提交 --> <form class="form_meren" id="mainForm" name="m ...

随机推荐

  1. Git很麻烦?只要掌握这几个命令,轻松将代码提交远程仓库

    在上一章节,跟大家介绍了拉取代码的操作,简单暴力.这一章节要介绍的是如何将现有的项目,直接提交到仓库. 现在,如果大家有一个项目要提交到GitHub仓库,安装上一张的方法,需要先在GitHub上建一个 ...

  2. uiautomatorviewer提示Unable to connect to adb. Check if adb is installed correctly解决方法

    转自:https://www.jianshu.com/p/c8581a70d1bc 解决方案: 1.打开  "E:\android-sdk_r24.4.1-windows\android-s ...

  3. AudioFormat.Encoding

    https://docs.oracle.com/javase/7/docs/api/javax/sound/sampled/AudioFormat.Encoding.html

  4. System.out.println高并发下导致应用暂停

    今天在进行序列号压测的时候,通过调用genSeqId方法来获取序列号,100并发,16台压测机器,发现无论怎么压测,调用量达到140w左右的时候,此方法的ops直接掉到了0,具体图示如下: 从上图可以 ...

  5. 求连通块的面积 - BFS、DFS实现

    本文以Leetcode中695.岛屿的最大面积题目为基础进行展开(题目

  6. 【ftp服务】配置ftp用户不能返回上级目录,只能在指定的目录

    500 OOPS: vsftpd: both local and anonymous access disabled! 出现这个错,需要修改配置:local_enable=YES 500 OOPS: ...

  7. Java连接MySQL数据库及简单的增删查改操作

    主要摘自 https://www.cnblogs.com/town123/p/8336244.html https://www.runoob.com/java/java-mysql-connect.h ...

  8. 转载 SAP用户权限控制设置及开发

    创建用户SU01 事务码:SU01,用户主数据的维护,可以创建.修改.删除.锁定.解锁.修改密码等 缺省:可以设置用户的起始菜单.登录的默认语言.数字显示格式.以及日期和时间的格式设置 参数:SAP很 ...

  9. PHP命令执行漏洞初探

    PHP命令执行漏洞初探 Mirror王宇阳 by PHP 命令执行 PHP提供如下函数用于执行外部应用程序:例如:system().shell_exec().exec().passthru() sys ...

  10. iOS中截取字符串指定位置

    直接上代码: NSString *string = @"今天是个好日子,忘记穿秋裤了"; NSString *string1 = [];//截取掉下标7之后的字符串 NSStrin ...