一、不带文件的form表单上传方式
前端页面
<form id="form" name="form" method="post">
<input type="text" name="name" >
<input type="text" name="password" >
<input type="text" name="phone" >
</form>
使用JQuery进行ajax提交数据到后台,代码如下:
1、直接第form表单进行系列化提交
$.ajax({
type: "post",
url: "/addForm",
data: $("#form").serialize(),
success: function(result){
alert(result);
},
});
2、手动进行编写成json数据进行提交
var name = $("").val();
var password = $("").val();
var phone = $("").val();
$.ajax({
type: "post",
url: "/addForm",
data: {"name":name,"password":password,"phone":phone},
success: function(result){
alert(result);
},
});
后端代码
SpringMvc控制器进行接收form表单数据:
1、使用实体对象进行接收表单数据
public Map<String,Object> insertUser(User user){
}
2、使用基本数据类型对数据进行逐条接收(当表单数据过多时,不推荐使用)
public Map<String,Object> insertUser(String name,String password,String phone){
}
3、使用HttpServletRequest、HttpServletResponse获取数据和响应(当表单数据过多时,也不推荐使用)
public Map<String,Object> insertUser(HttpServletRequest request,HttpServletResponse response){
String name = request.getParameter("name")
String password= request.getParameter("password")
String phone = request.getParameter("phone")
}
二、带文件的form表单上传方式
前端页面
<form id="form" class="form" method="post" action="/upload" enctype="multipart/form-data">
<input id="name" name="name" type="text">
<input id="age" name="a