你好,我现在要做一个程序,需要把前端HTML中Form表单的数据传递到后端的Java代码里头。HTML利用的是Thymleaf模板。不能用JSP代码,只能用JavaScript+Html+CSS。怎么把Form表单的数据转化为json并转到Java代码里头。
这是前端HTML部分代码,网页地址是insertcp.html。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style type="text/css">
label
{
display: inline-block;
width: 120px;
text-align: justify;
text-align-last: justify;
margin-right: 10px;
}
</style>
</head>
<body>
<form id="form" action="@insertcps">
<label>序号</label>
<input type="text" id="xuhao" name="序号"></input><br>
<label>图片路径</label>
<input type="text" id="path2" name="图片路径"></input><br>
<label>系列</label>
<input type="text" id="sys" name="系列"></input><br>
<label>藏品名称</label>
<input type="text" id="cangpinname" name="藏品名称"></input><br>
<label>原价</label>
<input type="text" id="yuanjia" name="原价"></input><br>
<label>现价</label>
<input type="text" id="xianjia" name="现价"></input><br>
<label>数量</label>
<input type="text" id="num" name="数量"></input><br>
<label>总数量</label>
<input type="text" id="totalnum" name="总数量"></input><br>
<label>图标路径</label>
<input type="text" id="icon" name="图标路径"></input><br>
<label>公司或个人</label>
<input type="text" id="company" name="公司或个人"></input><br>
<label>类型</label>
<input type="text" id="type2" name="类型"></input><br>
<label>起始时间</label>
<input type="text" id="began" name="起始时间"></input><br>
<input type="submit" id="sub"></input>
</form>
</body>
</html>
这是后端的Java代码,Java的地址与HTML的地址是一致的
@RequestMapping("/insertcp")
@ResponseBody
public Model insert(Model mp){
return mp;
}
怎样才能把HTML中的Form表单数据打包成Json形式传到Java代码里呢?而且最好用POST方法,不要用Get方法。因为Get方法会把输入的东西显示在地址栏里,太不安全了。用Post方法。用Ajax什么的可以吗?怎么办?