1.新建maven项目将相关包导入,结构如下:
2.建立index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" isELIgnored="false"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Insert</title>
<script src="js/jquery.min.js"></script>
<script src="js/YuxiSlider.jQuery.min.js"></script>
<script src="js/jquery.validate-1.13.1.js"></script>
<script src="js/test.js"></script>
</head>
<body>
<div>
<h5>Insert bootstrap laybool</h5>
</div>
<div>
<input id="name" name="name" type="text"/>//接收参数
<input id="age" name="age" type="text">
<button type="button" onclick="insert()">insert</button>//定义ajax请求方法
</div>
</body>
</html>
3.建立ajax与前端交互
function insert() {
var name = $("#name").val();
var age = $("#age").val();
$.ajax({
url: "insert",//controller接口
type: "post",//数据传送方式
data: {"name": name, "age": age},
dataType: "text",
success: function (data) {
if (data == "success") {
alert("insert success");
} else {
alert("failed");
}
},
});
}
前后台交互结果:
ajax与后台交互:
完成数据传递,代码(包含数据库)以上传。
https://download.csdn.net/download/LieYingZhiYan/12530387