文章目录
一、创建数据库
二、创建并启动SpringBoot项目
三、创建Vue项目
前端代码编写软件:HbuilderX
代码测试浏览器:谷歌浏览器(Chrome)
1. 创建空项目
导入vue框架文件 vue.js
导入vue联网文件 axios.min.js
2. 登录页面演示 userlogin.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<div id="app">
用户名:<input id="username">
<br>
<button v-on:click="login">登录</button>
</div>
</body>
<script src="vue.js"></script>
<script>
// 1.创建配置对象
var config = {
"el": "#app",
// mounted:定义自动调用的方法
"mounted": function() {
console.log("mounted运行");
},
"methods": {
"login": function() {
console.log("login()运行");
}
}
}
// 2.启动vue框架
var vue = new Vue(config);
</script>
</html>
运行html文件
点击登录按钮,
右键->检查->console 查看控制台输出信息
3. 自动生成跳转网站的参数
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<div id="app">
账号:<input id="username">
<br>
密码:<input id="password">
<br>
<button v-on:click="login">登录</button>
</div>
</body>
<script src="vue.js"></script>
<script src="axios.min.js"></script>
<script>
// 1.创建配置对象
var config = {
// 绑定app元素
"el": "#app",
// mounted:定义自动调用的方法
"mounted": function() {
console.log("mounted运行");
},
"methods": {
"login": function() {
console.log("login()运行");
// 1.接收用户输入的数据
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
// 2.拼出联网的地址
var serverUrl = "http:localhost:1314/user/login?" +
"username=" + username +
"&password=" + password;
console.log(serverUrl);
}
}
}
// 2.启动vue框架
var vue = new Vue(config);
</script>
</html>
复制控制台输出的地址在浏览器进行访问
4. 联网测试
若操作弹出联网失败,是跨域访问的问题
在相应的Controller控制器类中,添加跨域访问允许
@CrossOrigin // 允许别的网站用axios访问
5. 自动跳转网址
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<div id="app">
账号:<input id="username">
<br>
密码:<input id="password">
<br>
<button v-on:click="login">登录</button>
</div>
</body>
<script src="vue.js"></script>
<script src="axios.min.js"></script>
<script>
// 1.创建配置对象
var config = {
// 绑定app元素
"el": "#app",
// mounted:定义自动调用的方法
"mounted": function() {
console.log("mounted运行");
},
"methods": {
"login": function() {
console.log("login()运行");
// 1.接收用户输入的数据
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
// 2.拼出联网的地址
var serverUrl = "http:localhost:1314/user/login?" +
"username=" + username +
"&password=" + password;
console.log(serverUrl);
// 3.用axios联网
// 联网成功调用then()
// 联网失败调用catch()
axios.get(serverUrl)
.then(function(res){
window.alert("then:联网成功");
var controllerResult = res.data;
console.log("controllerResult");
if(controllerResult){
window.alert("登录成功");
}
})
.catch(function(){
window.alert("catch:联网失败")
});
}
}
}
// 2.启动vue框架
var vue = new Vue(config);
</script>
</html>
6. 端口连接测试
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<div id="app">
账号:<input id="username" />
<br>
密码:<input id="password" />
<br>
<button v-on:click="login">登录</button>
</div>
</body>
<script src="vue.js"></script>
<script src="axios.min.js"></script>
<script>
// 1.创建配置对象
var config = {
"el": "#app",
"mounted": function() {
console.log("mounted运行");
},
"methods": {
"login": function() {
console.log("login()运行")
// 1.接收用户输入的数据
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
// 2.拼出联网的地址:地址为UserController中login的网址
var serverUrl = "http://localhost:1314/user/login?" +
"username=" + username +
"&password=" + password;
console.log(serverUrl);
// 右键 检查,找开调试窗口 console,看到serverUrl,双击访问url,不能显示 404,500
// 3.用axios联网
// 联网失败调用catch()
// 联网成功调用then()
axios.get(serverUrl)
.then(function(res) {
window.alert("then:联网成功")
var controllerResult = res.data;
console.log(controllerResult);
if (controllerResult.id >= 1) {
window.alert("登录成功")
} else {
window.alert("登录失败")
}
})
.catch(function() {
window.alert("catch:联网失败")
});
}
}
}
// 2.启动vue框架
var vue = new Vue(config);
</script>
</html>
7. 首页 index.html
端口开启测试
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<div id="app">
<table width="100%">
<tr>
<td v-for="category in categoryList">
{{category.name}}
</td>
</tr>
</table>
</div>
</body>
<script src="vue.js"></script>
<script src="axios.min.js"></script>
<script>
var config={
"el":"#app",
"data":{
categoryList:[]
},
"mounted":function(){
// selectAll()联网
var serverUrl = "http://localhost:1314/category/selectAll";
axios.get(serverUrl)
.then(function(res){
window.alert("then:联网成功")
//给data中的集合赋值,赋值后,界面会自动更新
this.vue.categoryList = res.data;
})
.catch(function(){
window.alert("catch:联网失败")
});
}
}
var vue=new Vue(config);
</script>
</html>
若联网失败,一般是跨越权限问题
加入@CrossOrigin
注解,允许跨域访问
停止后,重新运行 Spring Boot 程序,刷新网页
8. 端口开启测试
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<div id="app">
<table width="100%">
<tr>
<td v-for="category in categoryList">
{{category.name}}
</td>
</tr>
</table>
</div>
</body>
<script src="vue.js"></script>
<script src="axios.min.js"></script>
<script>
var config = {
"el": "#app",
"data": {
categoryList: []
},
"methods": {
"selectItem": function(id) {
var serverUrl = "http://localhost:1314/item/selectByCategoryId?categoryId=" + id;
axios.get(serverUrl)
.then(function(res) {
window.alert("then:联网成功")
})
.catch(function() {
window.alert("catch:联网失败")
});
}
},
"mounted": function() {
// 调用methods中的selectItem()
this.selectItem(1);
// selectAll()联网
var serverUrl = "http://localhost:1314/category/selectAll";
axios.get(serverUrl)
.then(function(res) {
window.alert("then:联网成功")
//给data中的集合赋值,赋值后,界面会自动更新
this.vue.categoryList = res.data;
})
.catch(function() {
window.alert("catch:联网失败")
});
}
}
var vue = new Vue(config);
</script>
</html>
运行index.html 联网成功
9. 根据数据库图片地址,显示图片
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<div id="app">
<table width="100%">
<tr>
<td v-for="category in categoryList">
{{category.name}}
</td>
</tr>
</table>
<table>
<tr v-for="item in itemList">
<td>
{{item.name}}
<br>
{{item.price}}
<br>
<img v-bind:src="item.image" width="100" height="100" />
</td>
</tr>
</table>
</div>
</body>
<script src="vue.js"></script>
<script src="axios.min.js"></script>
<script>
var config = {
"el": "#app",
"data": {
categoryList: [],
itemList:[]
},
"methods": {
"selectItem": function(id) {
var serverUrl = "http://localhost:1314/item/selectByCategoryId?categoryId=" + id;
axios.get(serverUrl)
.then(function(res) {
window.alert("then:联网成功")
this.vue.itemList = res.data;
})
.catch(function() {
window.alert("catch:联网失败")
});
}
},
"mounted": function() {
// 调用methods中的selectItem()
this.selectItem(1);
// selectAll()联网
var serverUrl = "http://localhost:1314/category/selectAll";
axios.get(serverUrl)
.then(function(res) {
window.alert("then:联网成功")
//给data中的集合赋值,赋值后,界面会自动更新
this.vue.categoryList = res.data;
})
.catch(function() {
window.alert("catch:联网失败")
});
}
}
var vue = new Vue(config);
</script>
</html>
查看数据库item表
下载相应图片,保存到项目目录中,名称与数据库相同
运行网页
10. 商品详情页面的跳转
根据点击的物品,自动匹配id参数,生成跳转网站的参数传递
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<div id="app">
<table width="100%">
<tr>
<td v-for="category in categoryList" v-on:click="selectItem(category.id)">
{{category.name}}
</td>
</tr>
</table>
<table>
<tr v-for="item in itemList">
<td>
<a v-bind:href="'detail.html?id='+item.id">
{{item.name}}
<br>
{{item.price}}
<br>
<img v-bind:src="item.image" width="100" height="100" />
</a>
</td>
</tr>
</table>
</div>
</body>
<script src="vue.js"></script>
<script src="axios.min.js"></script>
<script>
var config = {
"el": "#app",
"data": {
categoryList: [],
itemList: []
},
"methods": {
"selectItem": function(id) {
var serverUrl = "http://localhost:1314/item/selectByCategoryId?categoryId=" + id;
axios.get(serverUrl)
.then(function(res) {
//window.alert("then:联网成功")
this.vue.itemList = res.data;
})
.catch(function() {
window.alert("catch:联网失败")
});
}
},
"mounted": function() {
// 调用methods中的selectItem()
this.selectItem(1);
// selectAll()联网
var serverUrl = "http://localhost:1314/category/selectAll";
axios.get(serverUrl)
.then(function(res) {
//window.alert("then:联网成功")
//给data中的集合赋值,赋值后,界面会自动更新
this.vue.categoryList = res.data;
})
.catch(function() {
window.alert("catch:联网失败")
});
}
}
var vue = new Vue(config);
</script>
</html>
点击第一台手机链接,导向该地址,自动匹配id的值 = 1
11. 商品详情页面
端口测试
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<div id="app">
{{item.name}}<br>
{{item.price}}<br>
<img v-bind:src="item.image" />
<br>
{{item.itemDesc}}
</div>
</body>
<script src="vue.js"></script>
<script src="axios.min.js"></script>
<script>
var config = {
"el": "#app",
"data": {
item: null
},
"mounted": function() {
console.log("mounted运行了")
// 接收商品的编号,在index.html中单击商品 detail.html?id=2
var p = location.search;
console.log(p); // ?id=2
p = p.substr(1);
console.log(p); // id=2
// 字符串分隔
var stringArray = p.split("=");
console.log(stringArray[0]); // id
console.log(stringArray[1]); // 商品编号
var serverUrl = "http://localhost:1314/item/selectByItemId?id=" + stringArray[1];
axios.get(serverUrl)
.then(function(res) {
//window.alert("then:联网成功")
this.vue.item = res.data;
})
.catch(function() {
window.alert("catch:联网失败")
})
}
}
var vue = new Vue(config);
</script>
</html>
跳转成功