汉字转拼音Java代码,应用于DWR

做项目的时候,经常会用到DWR增强用户体验,比如输入姓名拼音的首字母就可以自动搜出姓名。

         首先,这里涉及到将输入的汉字转为拼音的方法,用java代码写成PingyHanzi

 

 

 1 /**
 2  * Title:获得汉字的拼音首字母
 3  * Description: GB 2312-80 把收录的汉字分成两级。第一级汉字是常用汉字,计 3755 个,
 4  * 置于 16~55 区,按汉语拼音字母/笔形顺序排列;第二级汉字是次常用汉字,
 5  * 计 3008 个,置于 56~87 区,按部首/笔画顺序排列,所以本程序只能查到
 6  * 对一级汉字的声母。同时对符合声母(zh,ch,sh)只能取首字母(z,c,s) 
 7  * Copyright: Copyright (c) 2009
 8  * Company: 
 9  * @author not attributable
10  * @version 1.0
11  */
12 public class PingyHanzi { 
13 
14 // 国标码和区位码转换常量
15   private static final int GB_SP_DIFF = 160;
16 
17 //存放国标一级汉字不同读音的起始区位码
18   private static final int[] secPosvalueList = {
19       160116371833207822742302243325942787,
20       310632123472363537223730385840274086,
21       439045584684492552495600};
22 
23 //存放国标一级汉字不同读音的起始区位码对应读音
24   private static final char[] firstLetter = {
25       'a''b''c''d''e''f''g''h''j',
26       'k''l''m''n''o''p''q''r''s',
27       't''w''x''y''z'};
28 
29 //获取一个字符串的拼音码
30   public static String getFirstLetter(String oriStr) {
31     String str = oriStr.toLowerCase();
32     StringBuffer buffer = new StringBuffer();
33     char ch;
34     char[] temp;
35     for (int i = 0; i < str.length(); i++) { //依次处理str中每个字符
36       ch = str.charAt(i);
37       temp = new char[] {
38           ch};
39       byte[] uniCode = new String(temp).getBytes();
40       if (uniCode[0< 128 && uniCode[0> 0) { // 非汉字
41         buffer.append(temp);
42       }
43       else {
44         buffer.append(convert(uniCode));
45       }
46     }
47     return buffer.toString();
48   }
49 
50   /** 获取一个汉字的拼音首字母。
51    * GB码两个字节分别减去160,转换成10进制码组合就可以得到区位码
52    * 例如汉字"你"的GB码是0xC4/0xE3,分别减去0xA0(160)就是0x24/0x43
53    * 0x24转成10进制就是36,0x43是67,那么它的区位码就是3667,在对照表中读音为‘n'
54    */
55 
56   private static char convert(byte[] bytes) {
57 
58     char result = '-';
59     int secPosvalue = 0;
60     int i;
61     for (i = 0; i < bytes.length; i++) {
62       bytes[i] -= GB_SP_DIFF;
63     }
64     secPosvalue = bytes[0* 100 + bytes[1];
65     for (i = 0; i < 23; i++) {
66       if (secPosvalue >= secPosvalueList[i] &&
67           secPosvalue < secPosvalueList[i + 1]) {
68         result = firstLetter[i];
69         break;
70       }
71     }
72     return result;
73   }
74   public static void main(String [] args){
75       String a = PingyHanzi.getFirstLetter("我是中国人every");
76       System.out.println(a);
77   }
78 }
79 

 

 

接下来,在新增姓名的同时,将姓名拼音的首字母也写进数据库,使用时用DWR调用。

首先,业务逻辑层用写一个方法通过输入的字母查找出相应的姓名:

 

 1     public String findNameAjaxBySpell(String spell,String inputId,String divId){
 2         StringBuffer sb = new StringBuffer();
 3         String hql = "select empName from Emp where spell like '"+spell+"%'";
 4         List empNames = this.getSession().createQuery(hql).setMaxResults(10).list();
 5         int i = 1;
 6         sb.append("<ul>");
 7         for(Iterator it = empNames.iterator();it.hasNext();){
 8             String empName = (String)it.next();
 9             sb.append("<li class=lis><a href='#' onClick="+"\""+"gets_value('"+empName+"','"+inputId+"','"+divId+"')"+"\""+">["+(i++)+"]&nbsp;&nbsp;");
10             sb.append(empName);
11             sb.append("</a></li>");
12         }
13         if(empNames.size()!=0){
14             sb.append("<span onClick="+"\""+divId+".style.display='none';"+"\""+"class='close'><a href='#'>关闭</a></span>");
15         }
16         sb.append("<ul>");
17         return new String(sb);
18     }

 

页面显示为:

 

 

1                                 <td class="x_bill_td_00" width="15%" align="center">部门负责人:</td>
2                                 <td>
3                                     <div class="xmkc" style="z-index:1" align="left">
4                                       <div><input type="text" name="dept.rsPerson" id="rsPerson"  onclick="disp_cc('rsdiv');" 
                                  onkeyup="findNameBySpell(this.value,'rsPerson','rsdiv');"/></div>
5                                         <div id="rsdiv" class="classlist">
6                                         <!--可循环-->
7                                     </div>
8                                     </div>
9                                 </td>

 

 

JSDWR调用为

 

 1 function disp_cc(taag) {
 2     if (gets_id(taag).style.display == "none") {
 3         gets_id(taag).style.display = "";
 4     } else {
 5         gets_id(taag).style.display = "none";
 6     }
 7 }
 8 
 9 function findNameBySpell(spell,input,div){
10             empMgr.findNameAjaxBySpell(spell,input,div,
11                 function(data){
12                     document.getElementById(div).innerHTML=data;
13                     document.getElementById(div).style.display="block";
14                 }
15             );
16 }
17 

 

posted @ 2010-03-26 09:19  niexian  阅读(932)  评论(0)    收藏  举报