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

首先是 前端 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. 面试连环炮系列(十二):说说Atomiclnteger的使用场景

    说说Atomiclnteger的使用场景 AtomicInteger提供原子操作来进行Integer的使用,适合并发情况下的使用,比如两个线程对同一个整数累加. 为什么Atomiclnteger是线程 ...

  2. Your activation code could not be validated (error 1653219)

    很多小伙伴私聊我反应说,完全按照了你的 idea 激活教程走的,可最后还是没能激活成功,提示错误信息为: Your activation code could not be validated (er ...

  3. flex招式心法

    flex布局绝对是我们平常在布局中用的最多的一个属性,我们来看看它在can i use中的浏览器支持度:(截止到2019/11/16) 挖藕,凹森!支持度居然这么好,好到连ie也能支持大部分的flex ...

  4. sqlserver默认隔离级别下并发批量update同一张表引起的死锁

    提到死锁,最最常规的场景之一是Session1 以排它锁的方式锁定A表,请求B表,session2以排它锁的方式锁定B表,请求A表之类的,访问顺序不一致导致死锁的情况本文通过简化,测试这样一种稍显特殊 ...

  5. Appium使用总结

    目前在使用appium过程中遇到的一些问题及规避方法总结如下: Appium使用总结:1.在熄屏下启动测试,会自动唤醒屏幕2.Appium只针对单个应用测试3.使用unittest框架,该框架中每条用 ...

  6. linux上安装jenkins过程

    最近在学到jenkins分布式构建时,需要一台部署jenkins的主机服务器master,自己用的win10作为slave,所以我想在虚拟机上先部署jenkins. centos还是ubuntu呢,算 ...

  7. Codeforces 1238 D. AB-string

    思维题 这次cf思维题好多啊 定义了good string 指一个串,其中每一个字符都属于一个长度>=2 的回文串的一部分.叫你求一个串中有几个good substring. 显然ab串 goo ...

  8. Java replace() 方法 替换字符 也可用于 js 中

    JAVA中: public class Test { public static void main(String args[]) { String Str = new String("he ...

  9. Failed to execute ‘createObjectURL’ on ‘URL’: No function was found that matched the signature provided.

    这个报错是因为在浏览器上开启了手机模拟调试.Web SDK 不支持手机模拟调试的.

  10. 双链表算法原理【Java实现】(八)

    前言 前面两节内容我们详细介绍了ArrayList,一是手写实现ArrayList数据结构,而是通过分析ArrayList源码看看内置实现,关于集合内容一如既往,本节课我们继续学习集合LinkedLi ...