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