练习任务
- 登录页面,与后台交互,登录判断;
- 登陆成功后进入主页,实现主页的级联菜单。
bootCDN引入资源
<script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.11/vue.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.19.2/axios.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/qs/6.9.4/qs.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/element-ui/2.13.2/index.js"></script>
<link href="https://cdn.bootcdn.net/ajax/libs/element-ui/2.13.2/theme-chalk/index.css" rel="stylesheet">
axios vue推荐的ajax组件
axios.post('login', subData)
.then(response => {
console.log(response);
}).catch(function(error) {
console.log(error);
});
axios.get('提交地址')
.then(response => {
console.log(response);
}).catch(function(error) {
console.log(error);
});
window.Qs.stringify("将json拼成键值对")
页面效果
登录页面(axios的异步请求,以及登录信息回显的两种方式)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>登录页面</title>
<script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.11/vue.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.19.2/axios.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/qs/6.9.4/qs.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/element-ui/2.13.2/index.js"></script>
<link href="https://cdn.bootcdn.net/ajax/libs/element-ui/2.13.2/theme-chalk/index.css" rel="stylesheet">
<style type="text/css">
.text {
font-size: 14px;
}
.item {
margin-bottom: 18px;
}
.clearfix:before,
.clearfix:after {
display: table;
content: "";
}
.clearfix:after {
clear: both
}
.box-card {
width: 480px;
}
.myCard {
margin: 200px auto;
}
.wrongMsg{
font-size: small;
color: lightcoral;
margin-left: 10px;
}
</style>
</head>
<body>
<div id="main">
<el-card class="box-card myCard">
<div slot="header" class="clearfix">
<span>登录</span><span class="wrongMsg" v-if="isAble">{{wrongMsg}}</span>
</div>
<el-form :label-position="labelPosition" label-width="80px" :model="formLabelAlign">
<el-form-item label="用户名">
<el-input v-model="formLabelAlign.name"></el-input>
</el-form-item>
<el-form-item label="密码">
<el-input type="password" v-model="formLabelAlign.pass"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="onSubmit">登录</el-button>
<el-button>重置</el-button>
</el-form-item>
</el-form>
</el-card>
</div>
</body>
<script type="text/javascript">
var myvue = new Vue({
el: "#main",
data: {
labelPosition: 'right',
formLabelAlign: {
name: '',
pass: ''
},
wrongMsg:"",
isAble:true
},
methods: {
onSubmit() {
var subData = window.Qs.stringify(this.formLabelAlign);
console.log(subData);
axios.post('/day10/login', subData)
.then(response => {
console.log(response.data);
console.log(response.data.returnCode);
if (response.data.returnCode == 200) {
location.href = "/day10/main.html";
}else {
this.wrongMsg = response.data.returnMsg;
}
}).catch(function(error) {
console.log(error);
});
}
}
})
</script>
</html>
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String name = req.getParameter("name");
String pass = req.getParameter("pass");
PrintWriter pw = resp.getWriter();
if ("123".equals(name)&&"123".equals(pass)){
pw.print("{\"returnCode\":\"200\",\"returnMsg\":\"登陆成功\"}");
}else {
pw.print("{\"returnCode\":\"500\",\"returnMsg\":\"用户名或密码错误\"}");
}
pw.flush();
pw.close();
}
}
首页页面(v-for的双重遍历)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>首页</title>
<script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.11/vue.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.19.2/axios.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/element-ui/2.13.2/index.js"></script>
<link href="https://cdn.bootcdn.net/ajax/libs/element-ui/2.13.2/theme-chalk/index.css" rel="stylesheet">
<style>
*{
margin: 0px;
}
.el-header, .el-footer {
background-color: #B3C0D1;
color: #333;
text-align: center;
line-height: 40px;
padding: 80px;
}
.el-aside {
background-color: #D3DCE6;
color: #333;
text-align: center;
line-height: 400px;
}
.el-main {
background-color: #E9EEF3;
color: #333;
text-align: center;
line-height: 160px;
padding: 280px;
}
body > .el-container {
margin-bottom: 40px;
}
.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
line-height: 260px;
}
.el-container:nth-child(7) .el-aside {
line-height: 320px;
}
.asideModel{
height: 160px;
}
a{
text-decoration: none;
}
</style>
</head>
<body>
<div id="mydiv">
<el-container>
<el-aside width="280px">
<el-row class="tac">
<el-col :span="24">
<h5 class="asideModel"></h5>
<el-menu default-active="1102" class="el-menu-vertical-demo" @open="handleOpen" @close="handleClose"
background-color="#545c64" text-color="#fff" active-text-color="#ffd04b">
<el-submenu :index="menus.menuid" v-for="menus in usermenus">
<template slot="title">
<i :class="menus.menuicon"></i>
<span>{{menus.menuname}}</span>
</template>
<el-menu-item-group v-for="menu in menus.submenu">
<a :href="menu.menuurl">
<el-menu-item :index="menu.menuid">{{menu.menuname}}</el-menu-item>
</a>
</el-menu-item-group>
</el-submenu>
</el-menu>
</el-col>
</el-row>
</el-aside>
<el-container>
<el-header>用户管理系统</el-header>
<el-main>详情页面</el-main>
</el-container>
</el-container>
</div>
</body>
<script type="text/javascript">
var myVue = new Vue({
el: "#mydiv",
data: {
usermenus: [{
"menuid": "11",
"menuname": "系统管理",
"menuicon": "el-icon-setting",
"submenu": [{
"menuid": "1101",
"menuname": "系统参数",
"menuurl": "test1-1.html"
}, {
"menuid": "1102",
"menuname": "菜单配置",
"menuurl": "test1-2.html"
}]
},
{
"menuid": "12",
"menuname": "用户管理",
"menuicon": "el-icon-user",
"submenu": [{
"menuid": "1201",
"menuname": "用户参数",
"menuurl": "test2-1.html"
}, {
"menuid": "1202",
"menuname": "配置权限",
"menuurl": "test2-2.html"
}]
}
]
},
methods: {
handleOpen(key, keyPath) {
console.log(key, keyPath);
},
handleClose(key, keyPath) {
console.log(key, keyPath);
}
}
})
</script>
</html>