在 Apache Wink 中使用 Dojo Grid 和 Dojo Tree 小部件(4)
 
- UID
- 1066743
|

在 Apache Wink 中使用 Dojo Grid 和 Dojo Tree 小部件(4)
注册自定义服务提供方程序必须使用 Wink 框架注册自定义服务提供方程序,需要从 Wink 框架中继承 javax.ws.rs.core.application。清单 17 是一个示例。
清单 17. 注册自定义服务提供方程序1
2
3
4
5
6
7
| public Set<Object> getSingletons()
{
Set<Object> singletons = new HashSet<Object>();
singletons.add(new JsonGridProvider());
singletons.add(new JsonDojoTreeProvider());
return singletons;
}
|
建模一个资源 本小节将介绍如何建模一个样例资源,并显示如何使用该样例资源调用自定义服务提供方程序(JsonGridProvider 和 JsonDojoTreeProvider)来生成 JSON 网格输出以及 JSON 树输出。
创建样例资源来显示 Grid 和 Tree 输出 假设您有一个样例类 Employee。它是一个有 JAXB 注解的简单 Java 类,如清单 18 所示。
清单 18. 样例 Employee 资源1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
| // Employee.java
/**
@XmlAccessorType Controls whether fields or Javabean properties are serialized
by default
XmlAccessType.FIELD: Every non static, non transient attribute in a JAXB-bound
class will be automatically bound to XML, unless annotated by XmlTransient.
Getter/setter pairs are bound to XML only when they are explicitly annotated
by some of the JAXB annotations
*
*/
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Employee {
@XmlElement
private String id;
@XmlElement private String name;
@XmlElement private Boolean manager;
@XmlElement private String sex;
@XmlElement private String age;
/**
• @return the id
*/
public String getId() {
return id;
}
/**
• @param id the id to set
*/
public void setId(String id) {
this.id = id;
}
/**
• @return the name
*/
public String getName() {
return name;
}
/**
• @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
• @return the manager
*/
public Boolean getManager() {
return manager;
}
/**
• @param manager the manager to set
*/
public void setManager(Boolean manager) {
this.manager = manager;
}
/**
• @return the sex
*/
public String getSex() {
return sex;
}
/**
• @param sex the sex to set
*/
public void setSex(String sex) {
this.sex = sex;
}
/**
• @return the age
*/
public String getAge() {
return age;
}
/**
• @param age the age to set
*/
public void setAge(String age) {
this.age = age;
}
|
您还有一个保存员工列表的样例类 EmployeeView。此类是一个含有 JAXB 注解的简单 Java 类,如清单 19 所示。
清单 19. 样例 Employee View 资源1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| @XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class EmployeeView extends Employee {
@XmlElementWrapper(name = "children")
@XmlElement(name = "child")
Set<EmployeeView> employeeSet ;
public Set<EmployeeView> getChildrenRepresentation(){
return employeeSet;
}
public void setChildrenRepresentation(Set<EmployeeView> employeeSet){
this.employeeSet=employeeSet;
}
|
|
|
|
|
|
|