<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<title>Document</title>
</head>
<body>
<div id='app'>
<!-- <h2 :style="{key(屬性值):value(屬性值)}">{{message}}</h2> -->
<h2 :style="{fontSize:finalSize,color:finalColor}">{{message}}</h2>
</div>
<script src='../js/vue.js'></script>
<script>
const app = new Vue({
el:'#app', //用于掛載要管理的元素
data:{ //定義數據
message: '你好啊,李銀河!',
name: 'codewhy',
finalSize: '50px',
finalColor: 'red'
}
})
</script>
</body>
</html>
函數方法改進:
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<title>Document</title>
</head>
<body>
<div id='app'>
<!-- <h2 :style="{key(屬性值):value(屬性值)}">{{message}}</h2> -->
<h2 :style="getStyles()">{{message}}</h2>
</div>
<script src='../js/vue.js'></script>
<script>
const app = new Vue({
el:'#app', //用于掛載要管理的元素
data:{ //定義數據
message: '你好啊,李銀河!',
name: 'codewhy',
finalSize: '50px',
finalColor: 'red'
},
methods:{
getStyles:function(){
return {fontSize:this.finalSize,color:this.finalColor};
}
}
})
</script>
</body>
</html>
了解即可
在這里插入圖片描述
1.1VueJS介紹
Vue.js是一個構建數據驅動的 web 界面的漸進式框架。Vue.js 的目標是通過盡可能簡單的 API 實現響應的數據綁定和組合的視圖組件。它不僅易于上手,還便于與第三方庫或既有項目整合。
官網:https://cn.vuejs.org/
1.2MVVM模式
MVVM是Model-View-ViewModel的簡寫。它本質上就是MVC 的改進版。MVVM 就是將其中的View 的狀態和行為抽象化,讓我們將視圖 UI 和業務邏輯分開
MVVM模式和MVC模式一樣,主要目的是分離視圖(View)和模型(Model)
Vue.js 是一個提供了 MVVM 風格的雙向數據綁定的 Javascript 庫,專注于View 層。它的核心是 MVVM 中的 VM, 也就是 ViewModel。 ViewModel負責連接 View 和 Model,保證視圖和數據的一致性,這種輕量級的架構讓前端開發更加高效、便捷
1.3VueJS 快速入門
1.3VueJS 快速入門
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>快速入門</title> <script src="js/vuejs-2.5.16.js"></script> </head> <body> <div id="app"> {{message}} </div> <script> new Vue({ el:'#app', //表示當前vue對象接管了div區域data:{ message:'hello world' //注意不要寫分號結尾 } }); </script> </body> </html>
1.4插值表達式
數據綁定最常見的形式就是使用“Mustache”語法 (雙大括號) 的文本插值,Mustache 標簽將會被替代為對應數據對象上屬性的值。無論何時,綁定的數據對象上屬性發生了改變,插值處的內容都會更新。
Vue.js 都提供了完全的 JavaScript 表達式支持。
{{ number + 1 }} {{ ok ? 'YES' : 'NO' }}
這些表達式會在所屬 Vue 實例的數據作用域下作為 JavaScript 被解析。有個限制就是,每個綁定都只能包含單個表達式,所以下面的例子都不會生效。
<!-- 這是語句,不是表達式 --> {{ var a = 1 }} <!-- 流控制也不會生效,請使用三元表達式 --> {{ if (ok) { return message } }}
2.1v-on
可以用指令監聽 DOM 事件,并在觸發時運行一些 JavaScript 代碼
2.1.1v-on:click
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>事件處理 v-on示例1</title>
<script src="js/vuejs-2.5.16.js"></script>
</head>
<body>
<div id="app">
{{message}}
<button v-on:click="fun1('good')">點擊改變</button>
</div>
<script>
new Vue({
el:'#app', //表示當前vue對象接管了div區域data:{
message:'hello world' //注意不要寫分號結尾
},
methods:{
fun1:function(msg){ this.message=msg;
}
}
});
</script>
</body>
</html>
2.1.2v-on:keydown
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>事件處理 v-on示例2</title> <script src="js/vuejs-2.5.16.js"></script> </head> <body> <div id="app"> <input type="text"v-on:keydown="fun2('good',$event)"> </div> <script> new Vue({ el:'#app', //表示當前vue對象接管了div區域methods:{ fun2:function(msg,event){ if(! ((event.keyCode>=48&&event.keyCode<=57)||event.keyCode==8||event.keyCode==46)){ event.preventDefault(); } } } }); </script> </body> </html>
2.1.3v-on:mouseover
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>事件處理 v-on示例3</title> <script src="js/vuejs-2.5.16.js"></script> </head> <body> <div id="app"> <div v-on:mouseover="fun1" id="div"> <textarea v-on:mouseover="fun2($event)">這是一個文件域</textarea> </div> </div> <script> new Vue({ el:'#app', //表示當前vue對象接管了div區域methods:{ fun1:function(){ alert("div"); }, fun2:function(event){ alert("textarea"); event.stopPropagation();//阻止冒泡 } } }); </script> </body> </html>
2.1.4事件修飾符
Vue.js 為 v-on 提供了事件修飾符來處理 DOM 事件細節,如:event.preventDefault() 或
event.stopPropagation()。
Vue.js通過由點(.)表示的指令后綴來調用修飾符。
.stop
.prevent
.capture
.self
.once
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>v-on 事件修飾符</title> <script src="js/vuejs-2.5.16.js"></script> </head> <body> <div id="app"> <form @submit.prevent action="http://www.itcast.cn" method="get"> <input type="submit" value="提交"/> </form> <div @click="fun1"> <a @click.stop >itcast</a> </div> </div> <script> new Vue({ el:'#app', //表示當前vue對象接管了div區域methods:{ fun1:function(){ alert("hello itcast"); } } }); </script> </body> </html>
2.1.5按鍵修飾符
Vue 允許為 v-on 在監聽鍵盤事件時添加按鍵修飾符全部的按鍵別名:
.enter
.tab
.delete(捕獲 “刪除” 和 “退格” 鍵)
.esc
.space
.up
.down
.left
.right
.ctrl
.alt
.shift
.meta
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>v-on 按鈕修飾符</title> <script src="js/vuejs-2.5.16.js"></script> </head> <body> <div id="app"> <input type="text" v-on:keyup.enter="fun1"> </div> <script> new Vue({ el:'#app', //表示當前vue對象接管了div區域methods:{ fun1:function(){ alert("你按了回車"); } } }); </script> </body> </html> <p><!-- Alt + C --> <input @keyup.alt.67="clear"> <!-- Ctrl + Click --> <div @click.ctrl="doSomething">Do something</div>
v-on簡寫方式
<!-- 完整語法 --> <a v-on:click="doSomething">...</a> <!-- 縮 寫 --> <a @click="doSomething">...</a>
2.2v-text與v-html
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>v-html與v-text</title> <script src="js/vuejs-2.5.16.js"></script> </head> <body> <div id="app"> <div v-text="message"></div> <div v-html="message"></div> </div> <script> new Vue({ el:'#app', //表示當前vue對象接管了div區域data:{ message:"<h1>傳智黑馬</h1>" } }); </script> </body> </html>
2.3v-bind
插值語法不能作用在 HTML 特性上,遇到這種情況應該使用 v-bind指令
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>v-bind</title> <script src="js/vuejs-2.5.16.js"></script> </head> <body> <div id="app"> <font size="5" v-bind:color="ys1">傳智播客</font> <font size="5" :color="ys2">黑馬程序員</font> <hr> <a v-bind={href:"http://www.itcast.cn/index/"+id}>itcast</a> </div> <script> new Vue({ el:'#app', //表示當前vue對象接管了div區域data:{ ys1:"red", ys2:"green", id:1 } }); </script> </body> </html>
v-bind簡寫方式
<!-- 完整語法 --> <a v-bind:href="url">...</a> <!-- 縮 寫 --> <a :href="url">...</a>
2.4v-model
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>v-model</title> <script src="js/vuejs-2.5.16.js"></script> </head> <body> <div id="app"> 姓名:<input type="text" id="username" v-model="user.username"><br> 密碼:<input type="password" id="password" v-model="user.password"><br> <input type="button" @click="fun" value="獲取"> </div> <script> new Vue({ el:'#app', //表示當前vue對象接管了div區域data:{ user:{username:"",password:""} }, methods:{ fun:function(){ alert(this.user.username+" "+this.user.password); this.user.username="tom"; this.user.password="11111111"; } } }); </script> </body> </html>
2.5v-for
操作array
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>v-model</title> <script src="js/vuejs-2.5.16.js"></script> </head> <body> <div id="app"> <ul> <li v-for="(item,index) in list">{{item+" "+index}}</li> </ul> </div> <script> new Vue({ el:'#app', //表示當前vue對象接管了div區域 data:{ list:[1,2,3,4,5,6] } }); </script> </body> </html>
操作對象
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>v-for示例1</title> <script src="js/vuejs-2.5.16.js"></script> </head> <body> <div id="app"> <ul> <li v-for="(value,key) in product">{{key}}--{{value}}</li> </ul> </div> <script> new Vue({ el:'#app', //表示當前vue對象接管了div區域data:{ product:{id:1,pname:"電視機",price:6000} } }); </script> </body> </html>
操作對象數組
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>v-for示例1</title> <script src="js/vuejs-2.5.16.js"></script> </head> <body> <div id="app"> <table border="1"> <tr> <td>序號</td> <td>名稱</td> <td>價格</td> </tr> <tr v-for="p in products"> <td> {{p.id}} </td> <td> {{p.pname}} </td> <td> {{p.price}} </td> </tr> </table> </div> <script> new Vue({ el:'#app', //表示當前vue對象接管了div區域data:{ products:[{id:1,pname:"電視機",price:6000},{id:2,pname:"電冰箱",price:8000}, {id:3,pname:"電風扇",price:600}] } }); </script> </body> </html>
v-if是根據表達式的值來決定是否渲染元素
v-show是根據表達式的值來切換元素的display css屬性
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>v-if與v-show</title> <script src="js/vuejs-2.5.16.js"></script> </head> <body> <div id="app"> <span v-if="flag">傳智播客</span> <span v-show="flag">itcast</span> <button @click="toggle">切換</button> </div> <script> new Vue({ el:'#app', //表示當前vue對象接管了div區域data:{ flag:false }, methods:{ toggle:function(){ this.flag=!this.flag; } } }); </script> </body> </html>
每個 Vue 實例在被創建之前都要經過一系列的初始化過程.
vue 在 生 命 周 期 中 有 這 些 狀 態 , beforeCreate,created,beforeMount,mounted,beforeUpdate,updated,beforeDestroy,destroyed 。 Vue 在實例化的過程中,會調用這些生命周期的鉤子,給我們提供了執行自定義邏輯的機會。那么,在這些vue鉤子 中,vue實例到底執行了那些操作,我們先看下面執行的例子
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>生命周期</title> <script src="js/vuejs-2.5.16.js"></script> </head> <body> <div id="app"> {{message}} </div> <script> var vm = new Vue({ el: "#app", data: { message: 'hello world' }, beforeCreate: function() { console.log(this); showData('創建vue實例前', this); }, created: function() { showData('創建vue實例后', this); }, beforeMount: function() { showData('掛載到dom前', this); }, mounted: function() { showData('掛載到dom后', this); }, beforeUpdate: function() { showData('數據變化更新前', this); }, updated: function() { showData('數據變化更新后', this); }, beforeDestroy: function() { vm.test = "3333"; showData('vue實例銷毀前', this); }, destroyed: function() { showData('vue實例銷毀后', this); } }); function realDom() { console.log('真實dom結構:' + document.getElementById('app').innerHTML); } function showData(process, obj) { console.log(process); console.log('data 數據:' + obj.message) console.log(' 掛 載 的 對 象 :') console.log(obj.$el) realDom(); console.log(' ') console.log(' ') } vm.message="good "; vm.$destroy(); </script> </body> </html>
vue對象初始化過程中,會執行到beforeCreate,created,beforeMount,mounted 這幾個鉤子的內容
beforeCreate :數據還沒有監聽,沒有綁定到vue對象實例,同時也沒有掛載對象
created :數據已經綁定到了對象實例,但是還沒有掛載對象
beforeMount: 模板已經編譯好了,根據數據和模板已經生成了對應的元素對象,將數據對象關聯到了對象的el屬性,el屬性是一個HTMLElement對象,也就是這個階段,vue實例通過原生的createElement等方法來創建這個html片段,準備注入到我們vue實例指明的el屬性所對應的掛載點
mounted:將el的內容掛載到了el,相當于我們在jquery執行了(el).html(el),生成頁面上真正的dom,上面我們 就會發現dom的元素和我們el的元素是一致的。在此之后,我們能夠用方法來獲取到el元素下的dom對象,并進 行各種操作
當我們的data發生改變時,會調用beforeUpdate和updated方
beforeUpdate :數據更新到dom之前,我們可以看到$el對象已經修改,但是我們頁面上dom的數據還沒有發生改變
updated: dom結構會通過虛擬dom的原則,找到需要更新頁面dom結構的最小路徑,將改變更新到dom上面,完成更新
beforeDestroy,destroed :實例的銷毀,vue實例還是存在的,只是解綁了事件的監聽還有watcher對象數據與view的綁定,即數據驅動
4.1vue-resource
vue-resource是Vue.js的插件提供了使用XMLHttpRequest或JSONP進行Web請求和處理響應的服務。 當vue更新到2.0之后,作者就宣告不再對vue-resource更新,而是推薦的axios,在這里大家了解一下vue-resource就可以。
vue-resource的github: https://github.com/pagekit/vue-resource
4.2axios
Axios 是一個基于 promise 的 HTTP 庫,可以用在瀏覽器和 node.js 中
axios的github:https://github.com/axios/axios
4.2.1引入axios
首先就是引入axios,如果你使用es6,只需要安裝axios模塊之后
import axios from 'axios'; //安裝方法 npm install axios //或 bower install axios
當然也可以用script引入
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
4.2.2get請求
//通過給定的ID來發送請求axios.get('/user?ID=12345') .then(function(response){ console.log(response); }) .catch(function(err){ console.log(err); }); //以上請求也可以通過這種方式來發送axios.get('/user',{ params:{ ID:12345 } }) .then(function(response){ console.log(response); }) .catch(function(err){ console.log(err); });
4.2.3post請求
axios.post('/user',{ firstName:'Fred', lastName:'Flintstone' }) .then(function(res){ console.log(res); }) .catch(function(err){ console.log(err); });
為方便起見,為所有支持的請求方法提供了別名
axios.request(con?g)
axios.get(url[, con?g]) axios.delete(url[, con?g]) axios.head(url[, con?g]) axios.post(url[, data[, con?g]]) axios.put(url[, data[, con?g]]) axios.patch(url[, data[, con?g]])
5.1案例需求
完成用戶的查詢與修改操作
5.2數據庫設計與表結構
CREATE DATABASE vuejsdemo; USE vuejsdemo; CREATE TABLE USER( id INT PRIMARY KEY AUTO_INCREMENT, age INT, username VARCHAR(20), PASSWORD VARCHAR(50), email VARCHAR(50), sex VARCHAR(20) )
User類
public class User { private Integer id; private String username; private String password; private String sex; private int age; private String email; 省略getter/setter }
5.3服務器端
5.3.1配置文件
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema- instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.itheima.vuejsDemo</groupId>
<artifactId>vuejsDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>vuejsDemo Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.version>5.0.2.RELEASE</spring.version>
<slf4j.version>1.6.6</slf4j.version>
<log4j.version>1.2.12</log4j.version>
<mybatis.version>3.4.5</mybatis.version>
</properties>
<dependencies><!-- spring -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.8</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency><!-- log start -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency><!-- log end -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.5</version>
</dependency>
</dependencies>
<build>
<finalName>vuejsDemo</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default- bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
</plugins>
</build>
</project>
web.mxl
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1" metadata-complete="true"> <!-- 手動指定 spring 配置文件位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 配置 spring 提供的監聽器,用于啟動服務時加載容器 。 該間監聽器只能加載 WEB-INF 目錄中名稱為 applicationContext.xml 的配置文件 --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- 配置 spring mvc 的核心控制器 --> <servlet> <servlet-name>springmvcDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 配置初始化參數,用于讀取 springmvc 的配置文件 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <!-- 配置 servlet 的對象的創建時間點:應用加載時創建。取值只能是非 0 正整數,表示啟動順序 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvcDispatcherServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- 配置 springMVC 編碼過濾器 --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <!-- 設置過濾器中的屬性值 --> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <!-- 啟動過濾器 --> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <!-- 過濾所有請求 --> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
springmvc.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置創建 spring 容器要掃描的包 --> <context:component-scan base-package="com.itheima"> <!-- 制定掃包規則 ,只掃描使用@Controller 注解的 JAVA 類 --> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <mvc:annotation-driven></mvc:annotation-driven> </beans>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置 spring 創建容器時要掃描的包 --> <context:component-scan base-package="com.itheima"> <!--制定掃包規則,不掃描@Controller 注解的 JAVA 類,其他的還是要掃描 --> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- 加載配置文件 --> <context:property-placeholder location="classpath:db.properties"/> <!-- 配 置 MyBatis 的 Session 工 廠 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 數據庫連接池 --> <property name="dataSource" ref="dataSource"/> <!-- 加載 mybatis 的全局配置文件 --> <property name="configLocation" value="classpath:SqlMapConfig.xml"/> </bean> <!-- 配置數據源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!-- 配置 Mapper 掃描器 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.itheima.dao"/> </bean> <tx:annotation-driven/> <!-- (事務管理)transaction manager, use JtaTransactionManager for global tx --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> </beans>
db.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/vuejsDemo jdbc.username=root jdbc.password=root
5.3.2Controller
@RequestMapping("/user") @Controller @ResponseBody public class UserController { @Autowired private IUserService userService; @RequestMapping(value="/findAll.do") public List<User> findAll() { return userService.findAll(); } @RequestMapping(value="/findById.do") public User findById(Integer id) { return userService.findById(id); } @RequestMapping(value="/update.do") public User update(@RequestBody User user) { return userService.update(user); } }
5.3.3Dao
public interface IUserDao { @Select("select * from user") public List<User> findAll(); @Select("select * from user where id=#{id}") User findById(Integer id); @Update("update user set username=#{username},password=#{password},sex=#{sex},age=# {age},email=#{email} where id=#{id}") void update(User user); }
5.4客戶端
5.4.1user.html頁面
<!DOCTYPE html> <html> <head> <!-- 頁面meta --> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>數據 - AdminLTE2定制版</title> <meta name="description" content="AdminLTE2定制版"> <meta name="keywords" content="AdminLTE2定制版"> <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport"> <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script> <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> <link rel="stylesheet" href="/vuejsDemo/plugins/bootstrap/css/bootstrap.min.css"> <link rel="stylesheet" href="/vuejsDemo/plugins/font-awesome/css/font-awesome.min.css"> <link rel="stylesheet" href="/vuejsDemo/plugins/ionicons/css/ionicons.min.css"> <link rel="stylesheet" href="/vuejsDemo/plugins/iCheck/square/blue.css"> <link rel="stylesheet" href="/vuejsDemo/plugins/morris/morris.css"> <link rel="stylesheet" href="/vuejsDemo/plugins/jvectormap/jquery-jvectormap-1.2.2.css"> <link rel="stylesheet" href="/vuejsDemo/plugins/datepicker/datepicker3.css"> <link rel="stylesheet" href="/vuejsDemo/plugins/daterangepicker/daterangepicker.css"> <link rel="stylesheet" href="/vuejsDemo/plugins/bootstrap-wysihtml5/bootstrap3- wysihtml5.min.css"> <link rel="stylesheet" href="/vuejsDemo/plugins/datatables/dataTables.bootstrap.css"> <link rel="stylesheet" href="/vuejsDemo/plugins/treeTable/jquery.treetable.css"> <link rel="stylesheet" href="/vuejsDemo/plugins/treeTable/jquery.treetable.theme.default.css"> <link rel="stylesheet" href="/vuejsDemo/plugins/select2/select2.css"> <link rel="stylesheet" href="/vuejsDemo/plugins/colorpicker/bootstrap-colorpicker.min.css"> <link rel="stylesheet" href="/vuejsDemo/plugins/bootstrap-markdown/css/bootstrap- markdown.min.css"> <link rel="stylesheet" href="/vuejsDemo/plugins/adminLTE/css/AdminLTE.css"> <link rel="stylesheet" href="/vuejsDemo/plugins/adminLTE/css/skins/_all-skins.min.css"> <link rel="stylesheet" href="/vuejsDemo/css/style.css"> <link rel="stylesheet" href="/vuejsDemo/plugins/ionslider/ion.rangeSlider.css"> <link rel="stylesheet" href="/vuejsDemo/plugins/ionslider/ion.rangeSlider.skinNice.css"> <link rel="stylesheet" href="/vuejsDemo/plugins/bootstrap-slider/slider.css"> <link rel="stylesheet" href="/vuejsDemo/plugins/bootstrap-datetimepicker/bootstrap- datetimepicker.css"> </head> <body class="hold-transition skin-purple sidebar-mini"> <div class="wrapper" id="app"> <!-- 頁面頭部 --> <header class="main-header"> <!-- Logo --> <a href="all-admin-index.html" class="logo"> <!-- mini logo for sidebar mini 50x50 pixels --> <span class="logo-mini"><b>數據</b></span> <!-- logo for regular state and mobile devices --> <span class="logo-lg"><b>數據</b>后臺管理</span> </a> <!-- Header Navbar: style can be found in header.less --> <nav class="navbar navbar-static-top"> <!-- Sidebar toggle button--> <a href="#" class="sidebar-toggle" data-toggle="offcanvas" role="button"> <span class="sr-only">Toggle navigation</span> </a> <div class="navbar-custom-menu"> <ul class="nav navbar-nav"> <!-- Messages: style can be found in dropdown.less--> <li class="dropdown messages-menu"> <a href="#" class="dropdown-toggle" data-toggle="dropdown"> <i class="fa fa-envelope-o"></i> <span class="label label-success">4</span> </a> <ul class="dropdown-menu"> <li class="header">你有4個郵件</li> <li> <!-- inner menu: contains the actual data --> <ul class="menu"> <li> class="img-circle" alt="User Image"> <!-- start message --> <a href="#"> <div class="pull-left"> <img src="/vuejsDemo/img/user2-160x160.jpg" </div> <h4> </small> </a> </li> 系統消息 <small><i class="fa fa-clock-o"></i> 5 分鐘前 </h4> <p>歡迎登錄系統?</p> <!-- end message --> <li> class="img-circle" alt="User Image"> <a href="#"> <div class="pull-left"> <img src="/vuejsDemo/img/user3-128x128.jpg" </div> <h4> </small> </a> </li> <li> 團隊消息 <small><i class="fa fa-clock-o"></i> 2 小時前 </h4> <p>你有新的任務了</p> <a href="#"> <div class="pull-left"> <img src="/vuejsDemo/img/user4-128x128.jpg" class="img-circle" alt="User Image"> </div> <h4> Today</small> </a> </li> <li> Developers <small><i class="fa fa-clock-o"></i> </h4> <p>Why not buy a new awesome theme?</p> class="img-circle" alt="User Image"> <a href="#"> <div class="pull-left"> <img src="/vuejsDemo/img/user3-128x128.jpg" </div> <h4> Yesterday</small> </a> </li> <li> Sales Department <small><i class="fa fa-clock-o"></i> </h4> <p>Why not buy a new awesome theme?</p> class="img-circle" alt="User Image"> <a href="#"> <div class="pull-left"> <img src="/vuejsDemo/img/user4-128x128.jpg" </div> <h4> days</small> </a> </li> </ul> </li> Reviewers <small><i class="fa fa-clock-o"></i> 2 </h4> <p>Why not buy a new awesome theme?</p> <li class="footer"><a href="#">See All Messages</a></li> </ul> </li> <!-- Notifications: style can be found in dropdown.less --> <li class="dropdown notifications-menu"> <a href="#" class="dropdown-toggle" data-toggle="dropdown"> <i class="fa fa-bell-o"></i> <span class="label label-warning">10</span> </a> <ul class="dropdown-menu"> <li class="header">你有10個新消息</li> <li> <!-- inner menu: contains the actual data --> .............................. .............................. .............................. .............................. .............................. .............................. <script src="/vuejsDemo/plugins/jQuery/jquery-2.2.3.min.js"></script> <script src="/vuejsDemo/plugins/jQueryUI/jquery-ui.min.js"></script> <script> $.widget.bridge('uibutton', $.ui.button); </script> <script src="/vuejsDemo/plugins/bootstrap/js/bootstrap.min.js"></script> <script src="/vuejsDemo/plugins/raphael/raphael-min.js"></script> <script src="/vuejsDemo/plugins/morris/morris.min.js"></script> <script src="/vuejsDemo/plugins/sparkline/jquery.sparkline.min.js"></script> <script src="/vuejsDemo/plugins/jvectormap/jquery-jvectormap-1.2.2.min.js"></script> <script src="/vuejsDemo/plugins/jvectormap/jquery-jvectormap-world-mill-en.js"></script> <script src="/vuejsDemo/plugins/knob/jquery.knob.js"></script> <script src="/vuejsDemo/plugins/daterangepicker/moment.min.js"></script> <script src="/vuejsDemo/plugins/daterangepicker/daterangepicker.js"></script> <script src="/vuejsDemo/plugins/daterangepicker/daterangepicker.zh-CN.js"></script> <script src="/vuejsDemo/plugins/datepicker/bootstrap-datepicker.js"></script> <script src="/vuejsDemo/plugins/datepicker/locales/bootstrap-datepicker.zh-CN.js"></script> <script src="/vuejsDemo/plugins/bootstrap-wysihtml5/bootstrap3-wysihtml5.all.min.js"></script> <script src="/vuejsDemo/plugins/slimScroll/jquery.slimscroll.min.js"></script> <script src="/vuejsDemo/plugins/fastclick/fastclick.js"></script> <script src="/vuejsDemo/plugins/iCheck/icheck.min.js"></script> <script src="/vuejsDemo/plugins/adminLTE/js/app.min.js"></script> <script src="/vuejsDemo/plugins/treeTable/jquery.treetable.js"></script> <script src="/vuejsDemo/plugins/select2/select2.full.min.js"></script> <script src="/vuejsDemo/plugins/colorpicker/bootstrap-colorpicker.min.js"></script> <script src="/vuejsDemo/plugins/bootstrap-wysihtml5/bootstrap-wysihtml5.zh-CN.js"></script> <script src="/vuejsDemo/plugins/bootstrap-markdown/js/bootstrap-markdown.js"></script> <script src="/vuejsDemo/plugins/bootstrap-markdown/locale/bootstrap-markdown.zh.js"></script> <script src="/vuejsDemo/plugins/bootstrap-markdown/js/markdown.js"></script> <script src="/vuejsDemo/plugins/bootstrap-markdown/js/to-markdown.js"></script> <script src="/vuejsDemo/plugins/ckeditor/ckeditor.js"></script> <script src="/vuejsDemo/plugins/input-mask/jquery.inputmask.js"></script> <script src="/vuejsDemo/plugins/input-mask/jquery.inputmask.date.extensions.js"></script> <script src="/vuejsDemo/plugins/input-mask/jquery.inputmask.extensions.js"></script> <script src="/vuejsDemo/plugins/datatables/jquery.dataTables.min.js"></script> <script src="/vuejsDemo/plugins/datatables/dataTables.bootstrap.min.js"></script> <script src="/vuejsDemo/plugins/chartjs/Chart.min.js"></script> <script src="/vuejsDemo/plugins/flot/jquery.flot.min.js"></script> <script src="/vuejsDemo/plugins/flot/jquery.flot.resize.min.js"></script> <script src="/vuejsDemo/plugins/flot/jquery.flot.pie.min.js"></script> <script src="/vuejsDemo/plugins/flot/jquery.flot.categories.min.js"></script> <script src="/vuejsDemo/plugins/ionslider/ion.rangeSlider.min.js"></script> <script src="/vuejsDemo/plugins/bootstrap-slider/bootstrap-slider.js"></script> <script src="/vuejsDemo/plugins/bootstrap-datetimepicker/bootstrap-datetimepicker.js"></script> <script src="/vuejsDemo/plugins/bootstrap-datetimepicker/locales/bootstrap-datetimepicker.zh- CN.js"></script> <script src="/vuejsDemo/js/vuejs-2.5.16.js"></script> <script src="/vuejsDemo/js/axios-0.18.0.js"></script> <script src="/vuejsDemo/js/user.js"></script> <script> $(document).ready(function () { // 選擇框 $(".select2").select2(); // WYSIHTML5編輯器 $(".textarea").wysihtml5({ locale: 'zh-CN' }); }); // 設置激活菜單 function setSidebarActive(tagUri) { var liObj = $("#" + tagUri); if (liObj.length > 0) { liObj.parent().parent().addClass("active"); liObj.addClass("active"); } $(document).ready(function () { // 激活導航位置 setSidebarActive("admin-datalist"); // 列表按鈕 $("#dataList td input[type='checkbox']").iCheck({ checkboxClass: 'icheckbox_square-blue', increaseArea: '20%' }); // 全選操作 $("#selall").click(function () { var clicks = $(this).is(':checked'); if (!clicks) { $("#dataList td input[type='checkbox']").iCheck("uncheck"); } else { $("#dataList td input[type='checkbox']").iCheck("check"); }); }); } $(this).data("clicks", !clicks); </script> </body> </html>
5.4.2user.js頁面
生成動態的HTML頁面,頁面中使用嵌入 Vue.js 語法可動態生成
1. {{xxxx}}雙大括號文本綁定
2. v-xxxx以v-開頭用于標簽屬性綁定,稱為指令
雙大括號語法{{}}
格式:{{表達式}}
作用:
使用在標簽體中,用于獲取數據
可以使用 JavaScript 表達式
一次性插值v-once
通過使用v-once指令,你也能執行一次性地插值,當數據改變時,插值處的內容不會更新
輸出HTML指令v-html
格式:v-html='xxxx'
作用:
如果是HTML格式數據,雙大括號會將數據解釋為普通文本,為了輸出真正的 HTML,你需要使用v-html指令。
Vue 為了防止 XSS 攻擊,在此指令上做了安全處理,當發現輸出內容有 script 標簽時,則不渲染
XSS 攻擊主要利用 JS 腳本注入到網頁中,讀取 Cookie 值(Cookie一般存儲了登錄身份信息),讀取到了發送到黑客服務器,從而黑客可以使用你的帳戶做非法操作。XSS 攻擊還可以在你進入到支付時,跳轉到釣魚網站。
元素綁定指令v-bind
完整格式:v-bind:元素的屬性名='xxxx'
縮寫格式::元素的屬性名='xxxx'
作用:將數據動態綁定到指定的元素上
事件綁定指令v-on
完整格式:v-on:事件名稱="事件處理函數名"
縮寫格式:@事件名稱="事件處理函數名"注意:@后面沒有冒號
作用:用于監聽 DOM 事件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
<h3>1、{{}}雙大括號輸出文本內容</h3>
<!-- 文本內容 -->
<p>{{msg}}</p>
<!-- JS表達式 -->
<p>{{score + 1}}</p>
<h3>2、一次性插值</h3>
<p v-once>{{score + 1}}</p>
<h3>3、指令輸出真正的 HTML 內容 v-html</h3>
<p>雙大括號:{{contentHtml}}</p>
<!--
v-html:
1、如果輸出的內容是HTML數據,雙大括號將數據以普通文本方式進行輸出,為了輸出真正HTML數據,就需要使用v-html指定
2、為了防止XSS攻擊
-->
<p>v-html:<span v-html="contentHtml"></span></p>
<h3>4、v-bind屬性綁定指令</h3>
<img v-bind:src="imgUrl">
<img :src="imgUrl">
<a :href="tzUrl">跳轉</a>
<h3>5、事件綁定指令 v-on</h3> <input type="text" value="1" v-model="num"> <button @click='add'>點擊+1</button> </div> <script src="./node_modules/vue/dist/vue.js"></script> <script> var vm = new Vue({ el: '#app', data: { msg: "菜園子", score: 100, contentHtml: '<span style="color:red">此內容為紅色字體</span>', imgUrl: 'https://cn.vuejs.org/images/logo.png', tzUrl: 'https://www.baidu.com/', num : 10 }, methods: { add: function(){ console.log('add被調用') this.num ++ } }, }) </script></body></html>
git源碼地址:https://github.com/caiyuanzi-song/vue-demo.git
*請認真填寫需求信息,我們會在24小時內與您取得聯系。