首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

在 Apache Wink 中使用 Dojo Grid 和 Dojo Tree 小部件(3)

在 Apache Wink 中使用 Dojo Grid 和 Dojo Tree 小部件(3)

为一个 Dojo 树定制服务提供方程序您可以实现一个 Dojo 树,类似 Dojo Grid 服务提供方程序。在这一节,您将要编写一个自定义服务提供方程序来支持树型表示,以便于它能被 Dojo 小部件所理解。
首先调用服务提供方程序 JSONDojoTreeProvider,如清单 14 代码片段中的注解所示。您必须重写 isWritable 和 writeTo 方法。另外提供了一个新的 MIME 类型,application/json_dojo_tree。
清单  14. Dojo Tree 自定义服务提供方程序
1
2
3
4
5
@Provider
@Produces(value = { “application/json_dojo_tree”})
public class JsonDojoTreeProvider implements MessageBody-Writer<DojoTree> {
...
}




您需要为树定义一个新内容类型,这样 Dojo 客户端就可以区分和显示不同小部件上的数据,如清单 15 所示。
清单  15. 为 Dojo Tree 服务提供方程序重写 isWriteable
1
2
3
4
public boolean isWriteable(Class<?> inputClass, Type type,
Annotation[] annotations, MediaType mediaType) {
return inputClass == == DojoTree.class;
}




当输入类为 DojoTree 时,该方法返回 true。然后 Wink 框架将选择自定义 JsonDojoTreeProvider 来执行 XML 编组。
清单  16. 为 JSON Tree 服务提供方程序重写 writeTo
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
public void writeTo(DojoTree list, Class<?> inputClass, Type type,
Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, Object> map, OutputStream os)
throws IOException, WebApplicationException {
JAXBContext jc;
final String methodName = “writeTo”;
try {

JAXBContext jc = JAXBContext.newInstance("rest.resource");
OutputStream tempOs = new ByteArrayOutputStream();
Marshaller ms = jc.createMarshaller();
HashMap<String, String> namespaceMap = new HashMap<String, String>();
namespaceMap.put("http://www.w3.org/2001/XMLSchema-instance",
                    "xmlns");
namespaceMap.put("http://www.w3.org/2001/XMLSchema-instance",
                    "xmlns.type");

XMLOutputFactory factory = new MappedXMLOutputFactory(namespaceMap);
XMLStreamWriter xsw = factory.createXMLStreamWriter(tempOs);
ms.marshal(list, xsw);
String replacedJsonString = replaceAll(tempOs.toString());

// remove dojoItems tag
int index = replacedJsonString.indexOf(":");
int end = replacedJsonString.lastIndexOf("}");

// For an empty list items tag won't be present, But Dojo
//client requires it

if (replacedJsonString.indexOf("items") > 0) {
if (index > 0) {
   if (list.getItems().size() == 1) {
        String itemsString = replacedJsonString.substring(index + 1, end);
        os.write(replaceWithParenthesisForSingleItem(itemsString).getBytes());
            }
    else {
        os.write(replacedJsonString.substring(index + 1, end).getBytes());
          }
          } else {
            os.write(replacedJsonString.getBytes());
                }

            } else {
            String emptJsonString = "{items:[],startRecordIndex:" + "0"
                   + ", endRecordIndex:" + "0" + ",totalRecords:" + "0"
                        + "}";
            os.write(emptJsonString.getBytes());
            }
            os.close();
            xsw.close();
            tempOs.close();

        } catch (JAXBException e) {
            e.printStackTrace();
        } catch (XMLStreamException e) {
            e.printStackTrace();
        }

    }


private String replaceWithParenthesisForSingleItem(String jsonString) {

int firstColonIndex = jsonString.indexOf(“:”);
int firstFlowerBracketIndex = jsonString.indexOf(“}”);
String jsonObject = jsonString.substring(firstColonIndex + 1,
firstFlowerBracketIndex + 1);




通过重写 writeTo() 方法,代码将 DojoTree 对象转换成一个树格式然后写入一个 HTTP Response 输出流。JsonDojoTreeProvider 的 writeTo() 函数非常类似于 JsonGridProvider。Dojo Tree 格式没有 startRecordIndex、endRecordIndex 或 totalRecords。在 writeTo() 函数中主要的区别是当 DojoTree 对象的一个条目列表为空时,以及  DojoTree 对象只有一个条目列表时。
返回列表