1 VUE
1.1 示例
新建test_vue.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<div id="app">
<input name="username" v-model="username">
{{username}}
</div>
<script src="js/vue.js"></script>
<script>
new Vue({
el:"#app",
data() {
return {
username: ""
}
}
});
</script>
</body>
</html>
el: 用于指定和数据绑定的id,如#app表示app是与VUE绑定的id属性值
data: 用于定义数据模型
在浏览器中的效果
1.2 Vue指令
v-bind 为html标签绑定属性值,如设置href, css样式等
v-model 在表单元素上创建双向数据绑定
v-on 为HTML标签绑定事件
v-if 条件性的渲染某元素,判定为true时渲染,否则不渲染
v-else
v-else if
v-show 根据条件展示某元素,区别在于切换的是display属性的值
v-for 列表渲染,遍历容器的元素或者对象属性
1.2.1 v-bind & v-model指令
代码示例
<body>
<div id="app">
<input name="username" v-model="username">
{{username}} <br> <br>
<a v-bind:href="url">百度一下</a> <br> //给标签绑定属性值
<input v-model="url"> //数据双向绑定
</div>
<script src="js/vue.js"></script>
<script>
new Vue({
el:"#app",
data() {
return {
username: "",
url:"http://www.baidu.com"
}
}
});
</script>
</body>
效果展示
1.2.2 v-on指令
<input type="button" value="按钮" v-on:click="show()">
methods: {
show() {
alert("点击了button");
}
}
//v-on:click="show()" 也可以写成@click="show()"
1.2.3 v-if指令
<div v-if="count == 1">div1</div>
<div v-else-if="count == 2">div2</div>
<div v-else>div3</div>
<hr>
<input v-model="count">
data() {
return {
username: "",
url:"http://www.baidu.com",
count: 2
}
}
效果展示
1.2.4 v-for指令
<%-- v-for --%>
<div v-for="add in addrs">
{{add}} <br>
</div>
<hr>
<div v-for="(addr, i) in addrs">
{{i + 1}} -- {{addr}} <br>
</div>
addrs:["北京", "上海", "广州"]
效果展示
完整代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<div id="app">
<%-- v-model --%>
<input name="username" v-model="username">
{{username}} <br> <br>
<%-- v-bind --%>
<a v-bind:href="url">百度一下</a> <br>
<input v-model="url"> <br><br>
<%-- v-on --%>
<input type="button" value="按钮" v-on:click="show()"> <br><br>
<%-- v-if --%>
<div v-if="count == 1">div1</div>
<div v-else-if="count == 2">div2</div>
<div v-else>div3</div>
<hr>
<input v-model="count"> <br><br>
<%-- v-for --%>
<div v-for="add in addrs">
{{add}} <br>
</div>
<hr>
<div v-for="(addr, i) in addrs">
{{i + 1}} -- {{addr}} <br>
</div>
</div>
<script src="js/vue.js"></script>
<script>
new Vue({
el:"#app",
data() {
return {
username: "",
url:"http://www.baidu.com",
count: 2,
addrs:["北京", "上海", "广州"]
}
},
methods: {
show() {
alert("点击了button");
}
},
});
</script>
</body>
</html>
1.3 生命周期
beforeCreate 创建前
created 创建后
beforeMount 载入前
mounted 挂载完成
beforeUpdate 更新前
updated 更新后
beforeDestroy 销毁前
destroyed 销毁后
mouted示例,加载brand数据
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>所有品牌</title>
</head>
<body>
<h1>${user.username},欢迎您</h1>
<hr>
<div id="app">
<a href="addBrand.jsp"> <input type="button" value="新增"></a><br>
<br>
<table id="brandTable" border="1" cellspacing="0" width="80%">
<tr>
<th>序号</th>
<th>品牌名称</th>
<th>企业名称</th>
<th>排序</th>
<th>品牌介绍</th>
<th>状态</th>
<th>操作</th>
</tr>
<tr v-for="(brand, i) in brands" align="center">
<td>{{i + 1}}</td>
<td>{{brand.brandName}}</td>
<td>{{brand.companyName}}</td>
<td>{{brand.ordered}}</td>
<td>{{brand.description}}</td>
<td>{{brand.status}}</td>
<td><a href="#">修改</a> <a href="#">删除</a> </td>
</tr>
</table>
</div>
<script src="js/axios-0.18.0.js"></script>
<script src="js/vue.js"></script>
<script>
new Vue({
el: "#app",
data() {
return {
brands:[]
}
},
mounted() {
var _this = this;
axios ({
method: "get",
url : "http://localhost:8080/Mvc-Demo/selectAllServlet"
}).then(function (resp) {
_this.brands = resp.data;
})
}
})
</script>
</body>
</html>
2 Element
示例参考饿了吗组件Element
2.1 Element使用入门
代码示例
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<div id="app">
<el-row>
<el-button disabled>默认按钮</el-button>
<el-button type="primary" disabled>主要按钮</el-button>
<el-button type="success" disabled>成功按钮</el-button>
<el-button type="info" disabled>信息按钮</el-button>
<el-button type="warning" disabled>警告按钮</el-button>
<el-button type="danger" disabled>危险按钮</el-button>
</el-row>
<el-row>
<el-button plain disabled>朴素按钮</el-button>
<el-button type="primary" plain disabled>主要按钮</el-button>
<el-button type="success" plain disabled>成功按钮</el-button>
<el-button type="info" plain disabled>信息按钮</el-button>
<el-button type="warning" plain disabled>警告按钮</el-button>
<el-button type="danger" plain disabled>危险按钮</el-button>
</el-row>
<br>
<el-row>
<el-button round>圆角按钮</el-button>
<el-button type="primary" round>主要按钮</el-button>
<el-button type="success" round>成功按钮</el-button>
<el-button type="info" round>信息按钮</el-button>
<el-button type="warning" round>警告按钮</el-button>
<el-button type="danger" round>危险按钮</el-button>
</el-row>
<br>
<el-row>
<el-button icon="el-icon-search" circle></el-button>
<el-button type="primary" icon="el-icon-edit" circle></el-button>
<el-button type="success" icon="el-icon-check" circle></el-button>
<el-button type="info" icon="el-icon-message" circle></el-button>
<el-button type="warning" icon="el-icon-star-off" circle></el-button>
<el-button type="danger" icon="el-icon-delete" circle></el-button>
</el-row>
</div>
<script src="js/vue.js"></script>
<script src="element-ui/lib/index.js"></script>
<link rel="stylesheet" href="element-ui/lib/theme-chalk/index.css">
<script>
new Vue({
el: "#app"
})
</script>
</body>
</html>
效果展示
2.2 Element布局
2.2.1 Layout布局
示例代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<style>
.el-row {
margin-bottom: 20px;
}
.el-col {
border-radius: 4px;
}
.bg-purple-dark {
background: #99a9bf;
}
.bg-purple {
background: #d3dce6;
}
.bg-purple-light {
background: #e5e9f2;
}
.grid-content {
border-radius: 4px;
min-height: 36px;
}
.row-bg {
padding: 10px 0;
background-color: #f9fafc;
}
</style>
</head>
<body>
<div id="app">
<el-row>
<el-col :span="24"><div class="grid-content bg-purple-dark"></div></el-col>
</el-row>
<el-row>
<el-col :span="12"><div class="grid-content bg-purple"></div></el-col>
<el-col :span="12"><div class="grid-content bg-purple-light"></div></el-col>
</el-row>
<el-row>
<el-col :span="8"><div class="grid-content bg-purple"></div></el-col>
<el-col :span="8"><div class="grid-content bg-purple-light"></div></el-col>
<el-col :span="8"><div class="grid-content bg-purple"></div></el-col>
</el-row>
<el-row>
<el-col :span="6"><div class="grid-content bg-purple"></div></el-col>
<el-col :span="6"><div class="grid-content bg-purple-light"></div></el-col>
<el-col :span="6"><div class="grid-content bg-purple"></div></el-col>
<el-col :span="6"><div class="grid-content bg-purple-light"></div></el-col>
</el-row>
<el-row>
<el-col :span="4"><div class="grid-content bg-purple"></div></el-col>
<el-col :span="4"><div class="grid-content bg-purple-light"></div></el-col>
<el-col :span="4"><div class="grid-content bg-purple"></div></el-col>
<el-col :span="4"><div class="grid-content bg-purple-light"></div></el-col>
<el-col :span="4"><div class="grid-content bg-purple"></div></el-col>
<el-col :span="4"><div class="grid-content bg-purple-light"></div></el-col>
</el-row>
</div>
<script src="js/vue.js"></script>
<script src="element-ui/lib/index.js"></script>
<link rel="stylesheet" href="element-ui/lib/theme-chalk/index.css">
<script>
new Vue({
el: "#app"
})
</script>
</body>
</html>
效果展示
2.2.2 Container布局
代码示例
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<div id="app">
<el-container style="height: 500px; border: 1px solid #eee">
<el-aside width="200px" style="background-color: rgb(238, 241, 246)">
<el-menu :default-openeds="['1', '3']">
<el-submenu index="1">
<template slot="title"><i class="el-icon-message"></i>导航一</template>
<el-menu-item-group>
<template slot="title">分组一</template>
<el-menu-item index="1-1">选项1</el-menu-item>
<el-menu-item index="1-2">选项2</el-menu-item>
</el-menu-item-group>
<el-menu-item-group title="分组2">
<el-menu-item index="1-3">选项3</el-menu-item>
</el-menu-item-group>
<el-submenu index="1-4">
<template slot="title">选项4</template>
<el-menu-item index="1-4-1">选项4-1</el-menu-item>
</el-submenu>
</el-submenu>
<el-submenu index="2">
<template slot="title"><i class="el-icon-menu"></i>导航二</template>
<el-menu-item-group>
<template slot="title">分组一</template>
<el-menu-item index="2-1">选项1</el-menu-item>
<el-menu-item index="2-2">选项2</el-menu-item>
</el-menu-item-group>
<el-menu-item-group title="分组2">
<el-menu-item index="2-3">选项3</el-menu-item>
</el-menu-item-group>
<el-submenu index="2-4">
<template slot="title">选项4</template>
<el-menu-item index="2-4-1">选项4-1</el-menu-item>
</el-submenu>
</el-submenu>
<el-submenu index="3">
<template slot="title"><i class="el-icon-setting"></i>导航三</template>
<el-menu-item-group>
<template slot="title">分组一</template>
<el-menu-item index="3-1">选项1</el-menu-item>
<el-menu-item index="3-2">选项2</el-menu-item>
</el-menu-item-group>
<el-menu-item-group title="分组2">
<el-menu-item index="3-3">选项3</el-menu-item>
</el-menu-item-group>
<el-submenu index="3-4">
<template slot="title">选项4</template>
<el-menu-item index="3-4-1">选项4-1</el-menu-item>
</el-submenu>
</el-submenu>
</el-menu>
</el-aside>
<el-container>
<el-header style="text-align: right; font-size: 12px">
<el-dropdown>
<i class="el-icon-setting" style="margin-right: 15px"></i>
<el-dropdown-menu slot="dropdown">
<el-dropdown-item>查看</el-dropdown-item>
<el-dropdown-item>新增</el-dropdown-item>
<el-dropdown-item>删除</el-dropdown-item>
</el-dropdown-menu>
</el-dropdown>
<span>王小虎</span>
</el-header>
<el-main>
<el-table :data="tableData">
<el-table-column prop="date" label="日期" width="140">
</el-table-column>
<el-table-column prop="name" label="姓名" width="120">
</el-table-column>
<el-table-column prop="address" label="地址">
</el-table-column>
</el-table>
</el-main>
</el-container>
</el-container>
<style>
.el-header {
background-color: #B3C0D1;
color: #333;
line-height: 60px;
}
.el-aside {
color: #333;
}
</style>
</div>
<script src="../js/vue.js"></script>
<script src="../element-ui/lib/index.js"></script>
<link rel="stylesheet" href="../element-ui/lib/theme-chalk/index.css">
<script>
new Vue({
el: "#app",
data() {
const item = {
data: '2016-05-02',
name: '王小虎',
address: '上海市普陀区金沙江路 1518 弄'
};
return {
tableData: Array(20).fill(item)
}
}
})
</script>
</body>
</html>
效果
2.3 示例
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<style>
.el-table .warning-row {
background: oldlace;
}
.el-table .success-row {
background: #f0f9eb;
}
</style>
</head>
<body>
<div id="app">
<%--Dialog Form--%>
<el-button type="text" @click="dialogVisible = true">品牌添加</el-button>
<el-dialog title="编辑品牌" :visible.sync="dialogVisible" width="30%">
<el-form ref="form" :model="brand" label-width="80px">
<el-form-item label="品牌名称">
<el-input v-model="brand.brandName"></el-input>
</el-form-item>
<el-form-item label="企业名称">
<el-input v-model="brand.companyName"></el-input>
</el-form-item>
<el-form-item label="排序">
<el-input v-model="brand.order"></el-input>
</el-form-item>
<el-form-item label="产品描述">
<el-input type="textarea" v-model="brand.description"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="addBrand">提 交</el-button>
<el-button @click="dialogVisible = false">取 消</el-button>
</el-form-item>
</el-form>
</el-dialog>
<%-- 搜索表单 --%>
<el-form :inline="true" :model="brand" class="demo-form-inline">
<el-form-item label="状态">
<el-select v-model="brand.status" placeholder="状态">
<el-option label="启用" value="1"></el-option>
<el-option label="禁用" value="0"></el-option>
</el-select>
</el-form-item>
<el-form-item label="企业名称">
<el-input v-model="brand.companyName" placeholder="品牌名称"></el-input>
</el-form-item>
<el-form-item label="品牌名称">
<el-input v-model="brand.brandName" placeholder="品牌名称"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="onSubmit">查询</el-button>
</el-form-item>
</el-form>
<%-- 表格 --%>
<template>
<el-table
:data="tableData"
style="width: 100%"
:row-class-name="tableRowClassName"
tooltip-effect="dark"
@selection-change="handleSelectionChange">
<el-table-column
type="selection"
width="50"
align="center">
</el-table-column>
<el-table-column
prop="brandName"
label="品牌名称"
width="180"
align="center">
</el-table-column>
<el-table-column
prop="companyName"
label="企业名称"
width="180"
align="center">
</el-table-column>
<el-table-column
prop="order"
label="排序"
width="180"
align="center">
</el-table-column>
<el-table-column
prop="status"
label="状态"
width="180"
align="center">
</el-table-column>
<el-table-column
prop="operation"
label="操作"
width="180"
align="center">
<el-row>
<el-button type="primary">修改</el-button>
<el-button type="danger">删除</el-button>
</el-row>
</el-table-column>
</el-table>
</template>
<%-- 分页 --%>
<el-pagination
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
:current-page="currentPage4"
:page-sizes="[100, 200, 300, 400]"
:page-size="100"
layout="total, sizes, prev, pager, next, jumper"
:total="400">
</el-pagination>
</div>
<script src="../js/vue.js"></script>
<script src="../element-ui/lib/index.js"></script>
<link rel="stylesheet" href="../element-ui/lib/theme-chalk/index.css">
<script>
new Vue({
el: "#app",
methods: {
tableRowClassName({row, rowIndex}) {
if (rowIndex === 1) {
return 'warning-row';
} else if (rowIndex === 3) {
return 'success-row';
}
return '';
},
//复选框选中后回调方法
handleSelectionChange(val) {
this.multipleSelection = val;
console.log(this.multipleSelection)
},
onSubmit() {
console.log(this.brand);
},
addBrand() {
console.log(this.brand)
},
//分页
handleSizeChange(val) {
console.log(`每页${val}条`);
},
handleCurrentChange(val) {
console.log(`当前页:${val}`);
}
},
data() {
return {
dialogVisible: false,
//搜索品牌
brand: {
status: '',
brandName: '',
companyName: '',
id: '',
ordered: '',
description: ""
},
tableData: [{
brandName: '大众',
companyName: '上海大众',
order: '200',
status: '1'
}, {
brandName: '大众',
companyName: '上海大众',
order: '200',
status: '1'
}, {
brandName: '大众',
companyName: '上海大众',
order: '200',
status: '1'
}, {
brandName: '大众',
companyName: '上海大众',
order: '200',
status: '1'
}]
}
}
})
</script>
</body>
</html>
效果展示
3 综合示例
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<style>
.el-table .warning-row {
background: oldlace;
}
.el-table .success-row {
background: #f0f9eb;
}
</style>
</head>
<body>
<div id="app">
<%--Dialog Form--%>
<el-button type="text" @click="dialogVisible = true">品牌添加</el-button>
<el-dialog title="编辑品牌" :visible.sync="dialogVisible" width="30%">
<el-form ref="form" :model="brand" label-width="80px">
<el-form-item label="品牌名称">
<el-input v-model="brand.brandName"></el-input>
</el-form-item>
<el-form-item label="企业名称">
<el-input v-model="brand.companyName"></el-input>
</el-form-item>
<el-form-item label="排序">
<el-input v-model="brand.ordered"></el-input>
</el-form-item>
<el-form-item label="产品描述">
<el-input type="textarea" v-model="brand.description"></el-input>
</el-form-item>
<el-form-item label="状态">
<el-switch v-model="brand.status"
active-value="1"
inactive-value="0">
</el-switch>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="addBrand">提 交</el-button>
<el-button @click="dialogVisible = false">取 消</el-button>
</el-form-item>
</el-form>
</el-dialog>
<%-- 搜索表单 --%>
<el-form :inline="true" :model="brand" class="demo-form-inline">
<el-form-item label="状态">
<el-select v-model="brand.status" placeholder="状态">
<el-option label="启用" value="1"></el-option>
<el-option label="禁用" value="0"></el-option>
</el-select>
</el-form-item>
<el-form-item label="企业名称">
<el-input v-model="brand.companyName" placeholder="品牌名称"></el-input>
</el-form-item>
<el-form-item label="品牌名称">
<el-input v-model="brand.brandName" placeholder="品牌名称"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="onSubmit">查询</el-button>
</el-form-item>
</el-form>
<%-- 表格 --%>
<template>
<el-table
:data="tableData"
style="width: 100%"
:row-class-name="tableRowClassName"
tooltip-effect="dark"
@selection-change="handleSelectionChange">
<el-table-column
type="selection"
width="50"
align="center">
</el-table-column>
<el-table-column
prop="brandName"
label="品牌名称"
width="180"
align="center">
</el-table-column>
<el-table-column
prop="companyName"
label="企业名称"
width="180"
align="center">
</el-table-column>
<el-table-column
prop="ordered"
label="排序"
width="180"
align="center">
</el-table-column>
<el-table-column
prop="status"
label="状态"
width="180"
align="center">
</el-table-column>
<el-table-column
prop="operation"
label="操作"
width="180"
align="center">
<el-row>
<el-button type="primary">修改</el-button>
<el-button type="danger">删除</el-button>
</el-row>
</el-table-column>
</el-table>
</template>
<%-- 分页 --%>
<el-pagination
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
:current-page="currentPage"
:page-sizes="[100, 200, 300, 400]"
:page-size="100"
layout="total, sizes, prev, pager, next, jumper"
:total="400">
</el-pagination>
</div>
<script src="../js/axios-0.18.0.js"></script>
<script src="../js/vue.js"></script>
<script src="../element-ui/lib/index.js"></script>
<link rel="stylesheet" href="../element-ui/lib/theme-chalk/index.css">
<script>
new Vue({
el: "#app",
methods: {
tableRowClassName({row, rowIndex}) {
if (rowIndex === 1) {
return 'warning-row';
} else if (rowIndex === 3) {
return 'success-row';
}
return '';
},
//复选框选中后回调方法
handleSelectionChange(val) {
this.multipleSelection = val;
console.log(this.multipleSelection)
},
onSubmit() {
console.log(this.brand);
},
addBrand() {
var _this = this;
axios({
method: "post",
url: "http://localhost:8080/Mvc-Demo/addServlet",
data: _this.brand
}).then(function (resp) {
if (resp.data == "success") {
_this.dialogVisible = false;
_this.selectAll();
_this.$message({
message:'添加成功',
type:'success'
})
}
console.log("data = " + resp.data)
})
},
//分页
handleSizeChange(val) {
console.log(`每页${val}条`);
},
handleCurrentChange(val) {
console.log(`当前页:${val}`);
},
selectAll() {
var _this = this;
axios({
method: "get",
url: "http://localhost:8080/Mvc-Demo/selectAllServlet"
}).then(function (resp) {
_this.tableData = resp.data;
})
}
},
data() {
return {
dialogVisible: false,
currentPage: 1,
//搜索品牌
brand: {
status: '',
brandName: '',
companyName: '',
id: '',
ordered: '',
description: ""
},
tableData: [{
brandName: '大众',
companyName: '上海大众',
order: '200',
status: '1'
}, {
brandName: '大众',
companyName: '上海大众',
order: '200',
status: '1'
}, {
brandName: '大众',
companyName: '上海大众',
order: '200',
status: '1'
}, {
brandName: '大众',
companyName: '上海大众',
order: '200',
status: '1'
}]
}
},
mounted() {
this.selectAll();
}
})
</script>
</body>
</html>