在现实生活中,我们需要一些系统提供输入拼音首字母,返回与其对应中文的功能,这样可以提高人机交互性以及提高系统的友好性。
    结合之前所做的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((short0, (short127);
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()方法覆盖了,就没有大问题了的。大家可以试试。





本文依据《创作共用约定》之“署名-禁止派生-非商业用途”方式发布,即你可以免费拷贝、分发、呈现和表演当前作品,但是必须基于以下条款:

  • 署名:你必须明确标明作者的名字。

  • 非商业用途:你不可将当前作品用于商业目的。

  • 禁止派生:你不可更改、转变或者基于此作品重新构造为新作品。

对于任何二次使用或分发,你必须让其他人明确当前作品的授权条款。

在得到作者的明确允许下,这里的某些条款可以放弃。