W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
DataProvider前端向后端發(fā)出ajax請求的時候,不可避免的會有一些查詢條件的設置。
我們分前端和后端分別加以說明。
//dsBranch和dsDept都是DataSet對象
var branch = dsBranch.getData();
dsDept.setParameter("branchId", branch.get("id"));
dsDept.flushAsync();//觸發(fā)后端的DataProvider方法查詢數(shù)據(jù)
我們通過DataSet的parameter屬性設置查詢參數(shù),并調(diào)用DataSet的flushAsync方法觸發(fā)后端的DataProvider方法進行數(shù)據(jù)查詢。
@DataProvider
public Collection<Dept> getDeptsByBranchId(String branchId) throws Exception {
return depttDao.getDeptsByBranchId(branchId);
}
在DataProvider方法中我們通過一個String類型的branchId獲取前端傳入的查詢條件branchId。
在前面的例子中,查詢條件只有一個,比較簡單,如果查詢條件不止一個參數(shù),那么我們可以用一個JSON定義:
//dsBranch和dsDept都是DataSet對象
var branch = dsBranch.getData();
dsDept.setParameter({
"branchId": branch.get("id"),
"branchName": branch.get("name")
});
dsDept.flushAsync();//觸發(fā)后端的DataProvider方法查詢數(shù)據(jù)
多個參數(shù) 對應的Java端的處理代碼:
@DataProvider
public Collection<Dept> getDeptsByBranchId(String branchId, String branchName) {
return depttDao.getDeptsByBranchId(branchId, branchName);
}
在編寫Java后端代碼處理的時候,我們只要保證對應的參數(shù)名一致就可以。 Map參數(shù) 如果參數(shù)特別多情況下,我們也可以在Java端直接使用Map接收數(shù)據(jù):
@DataProvider
public Collection<Dept> getDeptsByBranchId(Map<String, Object> parameters) throws Exception {
String branchId = (String)parameters.get("branchId");
String branchName = (String)parameters.get("branchName");
return productDao.getDeptsByBranchId(branchId, branchName);
}
對象參數(shù)
如果我們覺得上述的多個參數(shù)在服務端用Map接收參數(shù)的編程方式不好的話,也可以改用對象參數(shù)接收。
為了實現(xiàn)對象參數(shù)接收,我們首先需要在dorado的model文件中定義一個全局的DataType:
注意設置matchType屬性對應為你需要的Java對象。
之后在客戶端上傳參數(shù)的時候,你只要添加一個$dataType屬性指定為Branch就可以:
//dsBranch和dsDept都是DataSet對象
var branch = dsBranch.getData();
dsDept.setParameter({
$dataType : "Branch",//設置json的$dataType屬性
"id": branch.get("id"),
"name": branch.get("name")
});
dsDept.flushAsync();//觸發(fā)后端的DataProvider方法查詢數(shù)據(jù)
客戶端設置了$dataType屬性后上傳到服務器端dorado引擎查找匹配的DataProvider方法的時候,就會自動在全局的model配置文件中查找匹配的DataType,并且將相關(guān)數(shù)據(jù)自動轉(zhuǎn)換為對應DataType配置好的matchType屬性對應Java類的實例中,本例就是Branch.java對象。 這樣我們的DataProvider端的Java代碼就可以改寫為:
@DataProvider
public Collection<Dept> getDeptsByBranchId(Branch branch) throws Exception {
String branchId = branch.getId();
String branchName = branch.getName();
return productDao.getDeptsByBranchId(branchId, branchName);
}
通過上述方法我們就可以多參數(shù)查詢對象化的處理。
查詢參數(shù)除了String類型自然還有其它的數(shù)據(jù)類型,DataProvider的查詢也是支持的,例如:
//dsCategory, dsProduct都是DataSet對象
var category = dsCategory.getData();
dsProduct.setParameter("categoryId": category.get("id"));
dsProduct.flushAsync();//觸發(fā)后端的DataProvider方法查詢數(shù)據(jù)
對應的Java端的處理代碼:
@DataProvider
public Collection<Product> getProductsByCategoryId(Long categoryId) {
return productDao.getProductsByCategoryId(categoryId);
}
```# 查詢參數(shù)# 查詢參數(shù)
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: