Simple Named Parameter Method 命名參數方法
這個功能僅在Ext JS 4.x和Sencha Touch 2.x可用。
Server
命名參數方法在服務器上註明用@ExtDirectMethod和類型SIMPLE_NAMED:
@Service
public class NamedService {
@ExtDirectMethod(ExtDirectMethodType.SIMPLE_NAMED)
public String showDetails(String firstName, String lastName, int age)
{
...
}
}
調用命名參數的方法只有當代碼被編譯在調試狀態。在Java中沒有標準的方法來確定一個參數的名稱。Spring可以在調試信息讀取參數的名字。如果你的代碼不能編譯在調試狀態,添加@RequestParam註解在參數的前面。
@Service
public class NamedService {
@ExtDirectMethod(ExtDirectMethodType.SIMPLE_NAMED)
public String showDetails(@RequestParam(value = "firstName") String firstName,
@RequestParam(value = "lastName") String lastName,
@RequestParam(value = "age") int age)
{
...
}
}
類庫獲得參數名字從@RequestParam註釋。註釋有優先權,類庫會一直從@RequestParam獲得參數如果註釋存在,甚至即使有調試信息可用。
和Simple Methods一樣,命名參數方法也支持這些服務器對象:
HttpServletResponse
HttpServletRequest
HttpSession
Locale
Principal
Parameters annotated with @RequestHeader (since 1.1.0)
服務器參數可以按任何順序添加
@ExtDirectMethod(ExtDirectMethodType.SIMPLE_NAMED)
public String showDetails(HttpServletResponse response,
String firstName, String lastName,
HttpServletRequest request, int age)
{
...
}
Client
調用服務器方法跟簡單方法類似。只是添加方法參數不再是按順序按逗號分隔添加,方法期望一個JavaScript對象用參數名作爲對象屬性名。名稱必須一致,順序無關。和平常方法一直支持一個回調函數在參數值後,一個作用域作爲最後一個參數。
var values = {lastName: 'Doe', firstName: 'John', age: 27};
namedService.showDetails(values, callbackFunction);
//with scope
namedService.showDetails(values, callbackFunction, this);
Examples
- http://demo.rasc.ch/eds/extjs42/named-arguments.html
- http://demo.rasc.ch/eds/extjs41/named-arguments.html