??? 在現實生活中,我們需要一些系統提供輸入拼音首字母,返回與其對應中文的功能,這樣可以提高人機交互性以及提高系統的友好性。
??? 結合之前所做的portlet技術,還有AJAX,讓我們在web應用這塊來說說這個不是太復雜的應用吧:
??? 對于我們的漢字與拼音對應詞庫生成需要如下資源:
??? 1、首先要一個該系統所屬的中文詞庫
??? 2、一份漢字與拼音的對照表
??? 接下來我們會用這個中文詞庫去匹配漢字與拼音對照表之中的數據,找出與各個詞語對應的拼音來,然后在這個文件中文詞語后面生成其對應的漢語拼音聲母首字母序列。
??? 首先我們需要用漢字拼音對照表生成體統中文詞庫對應的,拼音列表。之后我們需要將這個拼音系統詞庫列表存儲到內存之中。考慮到效率等綜合因素,我們選取了TreeMap這個類,它以其優秀的內部結構使得containsKey(), get(), put() 和 remove()等操作能夠保持其時間復雜度在對數級上,即logN。為了能夠保持拼音對應漢字的能夠隨著字母的增減而對應顯示,我們選用了TreeMap中的SubMap()方法,其返回值是一個SortedMap對象。這下面的代碼使我從之前的OOo應用中取出的,大家改改就可以用了。

?????????????

?1?//?XActionListener
?2?
?3???????????????public?void?textChanged(TextEvent?rEvent)?{
?4?
?5??????????????????????Object?searchTextBox?=?xControlContainer
?6?
?7????????????????????????????????????.getControl(searchText);
?8?
?9??
10?
11??????????????????????XTextComponent?yText?=?(XTextComponent)?UnoRuntime.queryInterface(
12?
13????????????????????????????????????XTextComponent.class,?searchTextBox);
14?
15??????????????????????searchString?=?yText.getText();
16?
17??????????????????????searchString?=?searchString.toLowerCase();
18?
19??????????????????????logger.debug("searchString?is?"?+?searchString);
20?
21??????????????????????Object?resultComboBoxModel?=?xControlContainer
22?
23????????????????????????????????????.getControl(resultComboBox);
24?
25??
26?
27??????????????????????XComboBox?xComboBox?=?(XComboBox)?UnoRuntime.queryInterface(
28?
29????????????????????????????????????XComboBox.class,?resultComboBoxModel);
30?
31??
32?
33??????????????????????Object?label?=?xControlContainer.getControl("Label1");
34?
35??????????????????????XFixedText?xLabel?=?(XFixedText)?UnoRuntime.queryInterface(
36?
37????????????????????????????????????XFixedText.class,?label);
38?
39??????????????????????char?shiftChar?=?searchString.charAt(searchString.length()?-?1);
40?
41??????????????????????shiftChar++;
42?
43??????????????????????String?tempString?=?searchString.substring(0,
44?
45????????????????????????????????????searchString.length()?-?1);
46?
47??????????????????????tempString?=?tempString?+?shiftChar;
48?
49??????????????????????if?(searchString.length()?==?1)
50?
51?????????????????????????????tempString?=?""?+?shiftChar;
52?
53??????????????????????logger.debug("tempString?is?"?+?tempString);
54?
55??????????????????????SortedMap?showMap?=?(SortedMap)?chineseMedicalTermWithPropertyHash
56?
57????????????????????????????????????.subMap(searchString,?tempString);
58?
59??
60?
61??????????????????????//?xLabel.setText(chineseMedicalTermWithPropertyHash.size()+"
62?
63??????????????????????//?check");
64?
65??
66?
67??????????????????????xComboBox.removeItems((short)?0,?(short)?127);
68?
69??????????????????????short?j?=?0;
70?
71??????????????????????Iterator?it?=?showMap.entrySet().iterator();
72?
73??????????????????????while?(it.hasNext())?{
74?
75?????????????????????????????Map.Entry?me?=?(Map.Entry)?it.next();
76?
77?????????????????????????????Object?ov?=?me.getValue();
78?
79?????????????????????????????xComboBox.addItem(ov.toString(),?j);
80?
81?????????????????????????????j++;
82?
83??????????????????????}
84?
85???????????????}
86?

最重要的在這里:

SortedMap showMap = (SortedMap) chineseMedicalTermWithPropertyHash

?????????????????????????????????? .subMap(searchString, tempString);

是從TreeMap實例中取出在length-1length的所有關鍵字組成的一個SortedMap實例,它的特性是:

A map that further guarantees that it will be in ascending key order, sorted according to the natural ordering of its keys (see the Comparable interface), or by a comparator provided at sorted map creation time. This order is reflected when iterating over the sorted map's collection views (returned by the entrySet, keySet and values methods). Several additional operations are provided to take advantage of the ordering. (This interface is the map analogue of the SortedSet interface.)

?????? 而對于AJAX部分來講更是簡單。我們知道AJAX與一般的web應用區別就是在它使用了Javascript的一個對象XMLHttpResponse/XMLHttpRequest,從而達到了異步的傳輸效果,提高了人機交互性。在本例中,我們沒有使用什么AJAX的框架,而是直接使用了在html標簽中最基本的javascript函數的方式來實現,從而達到了異步響應的功能。其中解決了一個問題,現在說說。那就是如果單單要實現AJAX傳輸數據,不能夠使用session來傳輸,這樣的效果總是會慢一拍,為什么會這樣呢?請大家想想,過段時間答復。:)

??? 至于portlet,只要滿足JSR168的就行了,難度不是太大。只要把doView()方法覆蓋了,就沒有大問題了的。大家可以試試。





本文依據《創作共用約定》之“署名-禁止派生-非商業用途”方式發布,即你可以免費拷貝、分發、呈現和表演當前作品,但是必須基于以下條款:

  • 署名:你必須明確標明作者的名字。

  • 非商業用途:你不可將當前作品用于商業目的。

  • 禁止派生:你不可更改、轉變或者基于此作品重新構造為新作品。

對于任何二次使用或分發,你必須讓其他人明確當前作品的授權條款。

在得到作者的明確允許下,這里的某些條款可以放棄。