实验六
login.html 登录页面
<!DOCTYPE html>
<html>
<head>
<title>登录页面</title>
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
$(document).ready(function () {
$("#button").click(function () {
var username = $("#Username").val();
var password = $("#Password").val();
$.ajax({
type: "POST",
url: "http://114.67.241.121:8088/user/login?password=" + password + "&username=" + username,
dataType: "json",
contentType: "application/json;charset=utf-8",
success: function (data) {
var token = data.data.token;
sessionStorage["token"] = token;
window.location.href = "./index.html";
},
error : function () { alert("登录失败"); }
})
})
})
</script>
<style>
body {
background-color: #f8f9fa;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
.container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
}
h1 {
margin-bottom: 50px;
color: #007bff;
text-shadow: 1px 1px 5px rgba(0, 0, 0, 0.2);
font-size: 36px;
font-weight: 700;
letter-spacing: 2px;
}
form {
display: flex;
flex-direction: column;
align-items: center;
background-color: #fff;
padding: 30px;
border-radius: 10px;
box-shadow: 1px 1px 10px rgba(0, 0, 0, 0.3);
width: 400px;
max-width: 100%;
margin: 0 auto;
}
label {
font-size: 18px;
font-weight: 600;
color: #555;
margin-bottom: 10px;
}
input[type="text"], input[type="password"] {
padding: 10px;
margin-bottom: 20px;
border-radius: 5px;
border: none;
box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.2);
font-size: 16px;
width: 100%;
max-width: 100%;
background-color: #f2f2f2;
}
#button {
padding: 10px 20px;
background-color: #007bff;
color: #fff;
border: none;
border-radius: 5px;
font-size: 16px;
cursor: pointer;
transition: all 0.3s ease-in-out;
}
#button:hover {
background-color: #0062cc;
}
</style>
</head>
<body>
<div class="container">
<h1>登录页面</h1>
<form>
<label>用户名:</label>
<input type="text" id="Username" placeholder="请输入用户名">
<label>密码:</label>
<input type="password" id="Password" placeholder="请输入密码">
<input type="button" id="button" value="登录">
</form>
</div>
</body>
</html>
index.html 首页
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>首页</title>
<script src="../js/jquery-3.1.1.min.js"></script>
<script>
var token = "";
$.ajax({
type: "get",
url: "http://114.67.241.121:8088/stu/list",
dataType: "json",
contentType: "application/json;charset=utf-8",
data: JSON.stringify(),
beforeSend: function (request) {
token = sessionStorage["token"];
request.setRequestHeader("Authorization", sessionStorage["token"]);
},
success: function (data) {
console.log(data);
for (var i = 0; i < data.data.length; i++) {
if (data.data[i] == null) {
continue;
}
var stuid = data.data[i].stuid;
var stuno = data.data[i].stuno;
var stuname = data.data[i].stuname;
var stumajor = data.data[i].stumajor;
var stugender = data.data[i].stugender;
var stugrade = data.data[i].stugrade;
var stuphone = data.data[i].stuphone;
var stuaddess = data.data[i].stuaddess;
var stunative = data.data[i].stunative;
$("table").append("<tr>" +
"<td>" + stuid + "</td>" +
"<td>" + stuno + "</td>" +
"<td>" + stuname + "</td>" +
"<td>" + stumajor + "</td>" +
"<td>" + stugender + "</td>" +
"<td>" + stugrade + "</td>" +
"<td>" + stuphone + "</td>" +
"<td>" + stuaddess + "</td>" +
"<td>" + stunative + "</td>" +
"<td>" + "<span id='span1'> 删除 </span>|<span id='span2'> 修改 </span>" + "</td>" +
+"</tr>");
}
},
error: function () { alert("error"); }
})
$(function () {
$('#add-btn').click(function () {
sessionStorage["token"] = token;
window.location.href = "./add.html";
});
});
$(document).on('click', 'span:contains("删除")', function () {
var stuid = $(this).parents('tr').find('td:eq(0)').text();
$.ajax({
type: "POST",
url: "http://114.67.241.121:8088/stu/del?stuid=" + stuid,
dataType: "json",
contentType: "application/json;charset=utf-8",
beforeSend: function (request) {
request.setRequestHeader("Authorization", sessionStorage["token"]);
},
success: function () {
window.location.reload();
},
error: function () { alert("delete error") }
})
});
$(document).on('click', 'span:contains("修改")', function () {
sessionStorage["token"] = token;
var stuid = $(this).parents('tr').find('td:eq(0)').text();
var stuno = $(this).parents('tr').find('td:eq(1)').text();
var stuname = $(this).parents('tr').find('td:eq(2)').text();
var stumajor = $(this).parents('tr').find('td:eq(3)').text();
var stugender = $(this).parents('tr').find('td:eq(4)').text();
var stugrade = $(this).parents('tr').find('td:eq(5)').text();
var stuphone = $(this).parents('tr').find('td:eq(6)').text();
var stuaddess = $(this).parents('tr').find('td:eq(7)').text();
var stunative = $(this).parents('tr').find('td:eq(8)').text();
var student = {
"stuid": stuid,
"stuno": stuno,
"stuname": stuname,
"stumajor": stumajor,
"stugender": stugender,
"stugrade": stugrade,
"stuphone": stuphone,
"stuaddess": stuaddess,
"stunative": stunative,
}
sessionStorage.setItem("student",JSON.stringify(student));
window.location.href = "./update.html";
});
</script>
<style>
body {
font-family: Arial, sans-serif;
font-size: 14px;
margin: 0;
padding: 0;
}
#div1 {
height: 80px;
width: 500px;
border: #000 solid 1px;
padding: 10px;
}
table {
margin: 20px auto;
border-collapse: collapse;
width: 90%;
max-width: 1200px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.15);
}
th {
border: 1px solid #ddd;
padding: 10px;
background-color: #f5f5f5;
text-align: left;
}
td {
border: 1px solid #ddd;
padding: 10px;
}
tr:hover {
background-color: #f5f5f5;
}
#span1,
#span2 {
cursor: pointer;
color: #007bff;
text-decoration: underline;
margin-right: 10px;
}
#span1:hover,
#span2:hover {
color: #0056b3;
text-decoration: none;
}
#add-btn {
padding: 10px 20px;
background-color: #007bff;
color: #fff;
border: none;
border-radius: 5px;
font-size: 16px;
cursor: pointer;
transition: all 0.3s ease-in-out;
}
#add-btn:hover {
background-color: #0062cc;
}
</style>
</head>
<body>
<div style="text-align:center;margin-top:20px;">
<button id="add-btn">新增学生信息</button>
</div>
<table>
<thead>
<tr>
<th>ID</th>
<th>学号</th>
<th>名字</th>
<th>专业</th>
<th>性别</th>
<th>年级</th>
<th>手机号</th>
<th>地址</th>
<th>籍贯</th>
<th></th>
</tr>
</thead>
<tbody></tbody>
</table>
</body>
</html>
add.html 添加学生信息页面
<!DOCTYPE html>
<html>
<head>
<title>添加页面</title>
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
var token = sessionStorage["token"];
$(function () {
$('#add-btn').click(function () {
var student = {
"stuid": 0,
"stuno": $("#student_id").val(),
"stuname": $("#name").val(),
"stumajor": $("#major").val(),
"stugender": $("#gender").val(),
"stugrade": $("#grade").val(),
"stuphone": $("#phone").val(),
"stuaddess": $("#address").val(),
"stunative": $("#hometown").val(),
}
console.log(student);
$.ajax({
type: "POST",
url: "http://114.67.241.121:8088/stu/add",
dataType: "json",
contentType: "application/json;charset=utf-8",
data: JSON.stringify(student),
beforeSend: function (request) {
request.setRequestHeader("Authorization", sessionStorage["token"]);
},
success: function () {
window.location.href = "./index.html";
},
error: function () { alert("error") }
})
});
$('#esc-btn').click(function () {
sessionStorage["token"] = token;
window.location.href = "./index.html";
});
});
</script>
<style>
body {
background-color: #f8f9fa;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
.container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
}
form {
display: flex;
flex-direction: column;
align-items: flex-start;
background-color: #fff;
padding: 30px;
border-radius: 10px;
box-shadow: 1px 1px 10px rgba(0, 0, 0, 0.3);
width: 500px;
max-width: 100%;
margin: 0 auto;
}
label {
font-size: 18px;
font-weight: 600;
color: #555;
margin-bottom: 10px;
}
input[type="text"] {
padding: 10px;
margin-bottom: 20px;
border-radius: 5px;
border: none;
box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.2);
font-size: 16px;
width: 100%;
max-width: 100%;
background-color: #f2f2f2;
}
.button-group {
display: flex;
justify-content: space-between;
margin-top: 20px;
}
button {
padding: 10px 20px;
background-color: #007bff;
color: #fff;
border: none;
border-radius: 5px;
font-size: 16px;
cursor: pointer;
transition: all 0.3s ease-in-out;
}
#add-btn {
background-color: #007bff;
color: #fff;
margin-right: 10px;
}
#esc-btn {
background-color: #fff;
color: #007bff;
border: 1px solid #007bff;
margin-left: auto;
}
button:hover {
background-color: #0062cc;
}
</style>
</head>
<body>
<div class="container">
<h1>添加学生信息</h1>
<form>
<label>学号:</label>
<input type="text" id="student_id" placeholder="Enter your student ID...">
<label>姓名:</label>
<input type="text" id="name" placeholder="Enter your name...">
<label>专业:</label>
<input type="text" id="major" placeholder="Enter your major...">
<label>性别:</label>
<input type="text" id="gender" placeholder="Enter your gender...">
<label>年级:</label>
<input type="text" id="grade" placeholder="Enter your grade...">
<label>手机号:</label>
<input type="text" id="phone" placeholder="Enter your phone number...">
<label>地址:</label>
<input type="text" id="address" placeholder="Enter your address...">
<label>籍贯:</label>
<input type="text" id="hometown" placeholder="Enter your hometown...">
<div class="button-group">
<button type="button" id="add-btn">添加</button>
<button type="button" id="esc-btn">返回</button>
</div>
</form>
</div>
</body>
</html>
update.html 修改学生信息页面
<!DOCTYPE html>
<html>
<head>
<title>修改页面</title>
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
var token = sessionStorage["token"];
$(function () {
var student = JSON.parse(sessionStorage.getItem("student"));
function setValues() {
$("#id").val(student.stuid);
$("#student_id").val(student.stuno);
$("#name").val(student.stuname);
$("#major").val(student.stumajor);
$("#gender").val(student.stugender);
$("#grade").val(student.stugrade);
$("#phone").val(student.stuphone);
$("#address").val(student.stuaddess);
$("#hometown").val(student.stunative);
}
setValues();
$('#update-btn').click(function () {
var student = {
"stuid": $("#id").val(),
"stuno": $("#student_id").val(),
"stuname": $("#name").val(),
"stumajor": $("#major").val(),
"stugender": $("#gender").val(),
"stugrade": $("#grade").val(),
"stuphone": $("#phone").val(),
"stuaddess": $("#address").val(),
"stunative": $("#hometown").val(),
}
$.ajax({
type: "POST",
url: "http://114.67.241.121:8088/stu/edit",
dataType: "json",
contentType: "application/json;charset=utf-8",
data: JSON.stringify(student),
beforeSend: function (request) {
request.setRequestHeader("Authorization", sessionStorage["token"]);
},
success: function () {
window.location.href = "./index.html";
},
error: function () { alert("updata error") }
})
});
$('#esc-btn').click(function () {
sessionStorage["token"] = token;
window.location.href = "./index.html";
});
});
</script>
<style>
body {
background-color: #f8f9fa;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
.container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
}
form {
display: flex;
flex-direction: column;
align-items: flex-start;
background-color: #fff;
padding: 30px;
border-radius: 10px;
box-shadow: 1px 1px 10px rgba(0, 0, 0, 0.3);
width: 500px;
max-width: 100%;
margin: 0 auto;
}
label {
font-size: 18px;
font-weight: 600;
color: #555;
margin-bottom: 10px;
}
input[type="text"] {
padding: 10px;
margin-bottom: 20px;
border-radius: 5px;
border: none;
box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.2);
font-size: 16px;
width: 100%;
max-width: 100%;
background-color: #f2f2f2;
}
.button-group {
display: flex;
justify-content: space-between;
margin-top: 20px;
}
button {
padding: 10px 20px;
background-color: #007bff;
color: #fff;
border: none;
border-radius: 5px;
font-size: 16px;
cursor: pointer;
transition: all 0.3s ease-in-out;
display: inline-block;
}
#update-btn {
background-color: #007bff;
color: #fff;
margin-right: 10px;
}
#esc-btn {
background-color: #fff;
color: #007bff;
border: 1px solid #007bff;
margin-left: auto;
}
button:hover {
background-color: #0062cc;
}
</style>
</head>
<body>
<div class="container">
<h1>修改学生信息</h1>
<form>
<label>ID:</label>
<input type="text" id="id" placeholder="Enter ID...">
<label>学号:</label>
<input type="text" id="student_id" placeholder="Enter your student ID...">
<label>姓名:</label>
<input type="text" id="name" placeholder="Enter your name...">
<label>专业:</label>
<input type="text" id="major" placeholder="Enter your major...">
<label>性别:</label>
<input type="text" id="gender" placeholder="Enter your gender...">
<label>年级:</label>
<input type="text" id="grade" placeholder="Enter your grade...">
<label>手机号:</label>
<input type="text" id="phone" placeholder="Enter your phone number...">
<label>地址:</label>
<input type="text" id="address" placeholder="Enter your address...">
<label>籍贯:</label>
<input type="text" id="hometown" placeholder="Enter your hometown...">
<div class="button-group">
<button type="button" id="update-btn">修改</button>
<button type="button" id="esc-btn">返回</button>
</div>
</form>
</div>
</body>
</html>