- style內部需要封號;
- 神坑(charset不是冒號)response.setContentType("text/html;charset=UTF-8");
- js改的是內存代碼而不是源代碼
- 所有操作都是通過ajax引擎執行
- 方法之中套方法外界便訪問不到是私有
- thread.sleep(毫秒)阻塞線程睡一會
- json鍵值對鍵是字符串,值任意,在HTML中取屬性和Java一樣
Paste_Image.png
- json最外不能有“”,否則就變成字符串了
- $==jquery
- jquery中post中文不用管,get需要按原來方式解決 (response.setContentType("text/html;charset=utf-8");)
--------查表jq看
-------get,post一樣
--------$.ajax({})里面參數隨意組合
$(function(){
$.get(
"/webfb22/ajaxServlet",//地址
{"name":"zahngsan","age":25},
function(data){
alert("3");
alert(data.name);
},
"json"
);
// })
function done3(){
$.ajax({
url:"/webfb22/ajaxServlet",
async:true,
data:{"lisiwu":20},
type:"post",
success:function(data){
alert(data.name);
},
error:function(){
},
dataType:"json"
});
};
Paste_Image.png
- 局部檢驗是否重名
- $("#checkuser").html("用戶名已存在");$("#checkuser").css("color","red");修改內部內容和顯示顏色
- $("#username").blur(function(){})失去焦點
$(function(){
$("#username").blur(function(){
var username=$(this).val();
$.post(
"${pageContext.request.contextPath}/checkUserName",
{"username":username},
function(data){
if(data.isExist){
$("#checkuser").html("用戶名已存在");
$("#checkuser").css("color","red");
}else{
$("#checkuser").html("用戶名可用");
$("#checkuser").css("color","green");
}
},
"json"
);
});
});
----------------dao
public Long checkUserName(String username) throws SQLException {
QueryRunner qr = new QueryRunner(MyCurrentConn.getCombpdatasource());
String sql="select count(*) from user where username=? ";
return (Long) qr.query(sql, new ScalarHandler(),username);
}
---------web
-response.getWriter().write("{\"isExist\":" + isExist + "}");注意反義
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
ProductSer pros = new ProductSer();
boolean isExist = false;
try {
isExist = pros.checkUserName(username);
} catch (SQLException e) {
e.printStackTrace();
}
response.getWriter().write("{\"isExist\":" + isExist + "}");
}
- js可以寫任意地方
- 重定向
-----style="position:relative"---position:absolute;z-index:1000顯示下拉結果div
---<div style='padding:5px;cursor:pointer' onclick='clickFn(this)' onmouseover='overFn(this)' onmouseout='outFn(this)'>小手指和鼠標不同色
<form class="navbar-form navbar-right" role="search">
<div class="form-group" style="position:relative">
<input id="search" type="text" class="form-control" placeholder="Search" onkeyup="searchWord(this)">
<div id="showDiv" style="display:none; position:absolute;z-index:1000;background:#fff; width:179px;border:1px solid #ccc;">
</div>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
<!-- 完成異步搜索 -->
<script type="text/javascript">
function overFn(obj){
$(obj).css("background","#DBEAF9");
}
function outFn(obj){
$(obj).css("background","#fff");
}
function clickFn(obj){
$("#search").val($(obj).html());
$("#showDiv").css("display","none");
}
function searchWord(obj){
//1、獲得輸入框的輸入的內容
var word = $(obj).val();
//2、根據輸入框的內容去數據庫中模糊查詢---List<Product>
var content = "";
$.post(
"${pageContext.request.contextPath}/searchWord",
{"word":word},
function(data){
//3、將返回的商品的名稱 現在showDiv中
//[{"pid":"1","pname":"小米 4c 官方版","market_price":8999.0,"shop_price":8999.0,"pimage":"products/1/c_0033.jpg","pdate":"2016-08-14","is_hot":1,"pdesc":"小米 4c 標準版 全網通 白色 移動聯通電信4G手機 雙卡雙待 官方好好","pflag":0,"cid":"1"}]
if(data.length>0){
for(var i=0;i<data.length;i++){
content+="<div style='padding:5px;cursor:pointer' onclick='clickFn(this)' onmouseover='overFn(this)' onmouseout='outFn(this)'>"+data[i]+"</div>";
}
$("#showDiv").html(content);
$("#showDiv").css("display","block");
}
},
"json");}</script>
------------------------------dao---記住模糊搜索是like
public List<Object> getSearchName(String str) throws SQLException {
QueryRunner qr = new QueryRunner(MyCurrentConn.getCombpdatasource());
String sql="select * from product where pname like ? limit 0,8";
List<Object> names = qr.query(sql, new ColumnListHandler("pname"),"%"+str+"%");
// System.out.println(names.size());
return names;
}
--------web
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//獲得關鍵字
String word = request.getParameter("word");
//查詢該關鍵字的所有商品
ProductService service = new ProductService();
List<Object> productList = null;
try {
productList = service.findProductByWord(word);
} catch (SQLException e) {
e.printStackTrace();
}
//["xiaomi","huawei",""...]
//使用json的轉換工具將對象或集合轉成json格式的字符串
/*JSONArray fromObject = JSONArray.fromObject(productList);
String string = fromObject.toString();
System.out.println(string);*/
Gson gson = new Gson();
String json = gson.toJson(productList);
System.out.println(json);
response.setContentType("text/html;charset=UTF-8");
response.getWriter().write(json);
}
1.Ajax
1.什么是同步,什么是異步
同步現象:客戶端發送請求到服務器端,當服務器返回響應之前,客戶端都處于等待卡死狀態
異步現象:客戶端發送請求到服務器端,無論服務器是否返回響應,客戶端都可以隨意做其他事情,不會被卡死
2.Ajax的運行原理(有一個Ajax來幫助請求服務,只需要調用他就行)
頁面發起請求,會將請求發送給瀏覽器內核中的Ajax引擎,Ajax引擎會提交請求到服務器端,在這段時間里,客戶端可以任意進行任意操作,直到服務器端將數據返回給Ajax引擎后,會觸發你設置的事件,從而執行自定義的js邏輯代碼完成某種頁面功能。
二、js原生的Ajax技術(了解)
js原生的Ajax其實以瀏覽器內內置的Ajax引擎對象學習
1)創建Ajax引擎對象
2)為Ajax引擎對象綁定監聽(監聽服務器已將數據響應給引擎)
3)綁定提交地址
4)發送請求
5)接受響應數據
------所用異步訪問都是ajax引擎
function done(){
var req=new XMLHttpRequest();
req.onreadystatechange=function(){
alert("2222222");
var resp=req.response();
alert(resp);
};
req.open("GET","webfb22/ajaxServlet",true);
req.send();
}
注意:如果是post提交
在發送請求之前設置一個頭
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
2.Json數據格式
- json是一種與語言無關的數據交換的格式,作用:
使用ajax進行前后臺數據交換
移動端與服務端的數據交換 - Json的格式與解析
json有兩種格式:
1)對象格式:{"key1":obj,"key2":obj,"key3":obj...}
2)數組/集合格式:[obj,obj,obj...]
json是js的原生內容,也就意味著js可以直接取出json對象中的數據 - Json的轉換插件直接將java對象或集合轉換成json字符串jsonlib;Gson:google;fastjson:阿里巴巴
3.Jquery的Ajax技術
jquery對js原生的ajax進行了封裝
其中:
url:代表請求的服務器端地址
data:代表請求服務器端的數據(可以是key=value形式也可以是json格式)
callback:表示服務器端成功響應所觸發的函數(只有正常成功返回才執行)
type:表示服務器端返回的數據類型(jquery會根據指定的類型自動類型轉換)
常用的返回類型:text、json、html等
3)$.ajax( { option1:value1,option2:value2... } );
常用的option有如下:
async:是否異步,默認是true代表異步
data:發送到服務器的參數,建議使用json格式
dataType:服務器端返回的數據類型,常用text和json
success:成功響應執行的函數,對應的類型是function類型
type:請求方式,POST/GET
url:請求服務器端地址
$.get(url, [data], [callback], [type])
$.post(url, [data], [callback], [type])
$.get(
"/webfb22/ajaxServlet",//地址
{"name":"zahngsan","age":25},
function(data){
alert("3");
alert(data.name); },
"json"
);
-------------------------
function done3(){
$.ajax({
url:"/webfb22/ajaxServlet",
async:true,
data:{"lisiwu":20},
type:"post",
success:function(data){
alert(data.name);
},
error:function(){
},
dataType:"json"
});
};