我在写一个jsp页面,想实现页面中输入 拼音字母:
    如:zs 就会查找zs开头的名字 
    如:张三,张松等 怎么可以实现、?
高人 帮帮忙·~~~~

解决方案 »

  1.   

    jsp页面关键有个刷新的麻烦东西,LZ考虑用下ajax
      

  2.   

    然后找个ajax的控件,比如jquery的 autocomplete
      

  3.   

    方向性,看楼主想怎么实现,
    zs------>张三,张松(性能极其低)
    张三,张松------>zs(数据插入时生成)
    package sample;public class GB2Alpha ...{    //字母Z使用了两个标签,这里有27个值
        //i, u, v都不做声母, 跟随前面的字母
        private char[] chartable =
                ...{
                    '啊', '芭', '擦', '搭', '蛾', '发', '噶', '哈', '哈',
                    '击', '喀', '垃', '妈', '拿', '哦', '啪', '期', '然',
                    '撒', '塌', '塌', '塌', '挖', '昔', '压', '匝', '座'
                };    private char[] alphatable =
                ...{
                    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
                    'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 
                    'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
                };
        private int[] table = new int[27];
        //初始化
        ...{
            for (int i = 0; i < 27; ++i) ...{
                table[i] = gbValue(chartable[i]);
            }
        }    public GB2Alpha() ...{
        }    //主函数,输入字符,得到他的声母,
        //英文字母返回对应的大写字母
        //其他非简体汉字返回 '0'    public char Char2Alpha(char ch) ...{        if (ch >= 'a' && ch <= 'z')
                return (char) (ch - 'a' + 'A');
            if (ch >= 'A' && ch <= 'Z')
                return ch;
            int gb = gbValue(ch);
            if (gb < table[0])
                return '0';
            int i;
            for (i = 0; i < 26; ++i) ...{
                if (match(i, gb)) break;
            }
            if (i >= 26)
                return '0';
            else
                return alphatable[i];
        }    //根据一个包含汉字的字符串返回一个汉字拼音首字母的字符串
        public String String2Alpha(String SourceStr)...{
            
            String Result = "";
            int StrLength = SourceStr.length();
            int i;
            try ...{
                for (i = 0; i < StrLength; i++) ...{
                    Result += Char2Alpha(SourceStr.charAt(i));
                }
            } catch (Exception e) ...{
                Result = "";
            }
            return Result;
        }    private boolean match(int i, int gb) ...{
            
            if (gb < table[i])
                return false;
            int j = i + 1;        //字母Z使用了两个标签
            while (j < 26 && (table[j] == table[i])) ++j;
            if (j == 26)
                return gb <= table[j];
            else
                return gb < table[j];
        }    //取出汉字的编码
        private int gbValue(char ch) ...{
            
            String str = new String();
            str += ch;
            try ...{
                byte[] bytes = str.getBytes("GB2312");
                if (bytes.length < 2)
                    return 0;
                return (bytes[0] << 8 & 0xff00) + (bytes[1] &
                        0xff);
            } catch (Exception e) ...{
                return 0;
            }
        }    public static void main(String[] args) ...{
            
            GB2Alpha obj1 = new GB2Alpha();
            System.out.println(obj1.String2Alpha("陈胜"));
            System.out.println(obj1.String2Alpha("吴广"));
            return;
        }
    }
     
      

  4.   

    我还没有学 Ajax 不会实现
        其实我是考虑到了一种方法:就是在数据库中插入拼音一个字段,而在前台的时候,通过将姓名汉字转码,成拼音首字母(代码下面)。插入到数据库,不要用户自己插入拼音那一列的内容。这种方法不知道 好不好,大家都可以再说说自己看法,最好的解决方式public class GetFirstLetter { //  国标码和区位码转换常量
    private static final int GB_SP_DIFF = 160;// 存放国标一级汉字不同读音的起始区位码
    private static final int[] secPosvalueList = {
          1601, 1637, 1833, 2078, 2274, 2302, 2433, 2594, 2787,
          3106, 3212, 3472, 3635, 3722, 3730, 3858, 4027, 4086,
          4390, 4558, 4684, 4925, 5249, 5600
          };// 存放国标一级汉字不同读音的起始区位码对应读音
    private static final char[] firstLetter = {
          'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j',
          'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
          't', 'w', 'x', 'y', 'z'
          };// 获取一个字符串的拼音码
    public static String getFirstLetter(String oriStr) {
        String str = oriStr.toLowerCase();
        StringBuffer buffer = new StringBuffer();
        char ch;
        char[] temp;
        for (int i = 0; i < str.length(); i++) { //依次处理str中每个字符
          ch = str.charAt(i);
          temp = new char[] {
              ch};
          byte[] uniCode = new String(temp).getBytes();
          if (uniCode[0] < 128 && uniCode[0] > 0) { // 非汉字
            buffer.append(temp);
          }
          else {
            buffer.append(convert(uniCode));
          }
        }
        return buffer.toString();
    }
    /** 获取一个汉字的拼音首字母。
       * GB码两个字节分别减去160,转换成10进制码组合就可以得到区位码
       * 例如汉字"你"的GB码是0xC4/0xE3,分别减去0xA0(160)就是0x24/0x43
       * 0x24转成10进制就是36,0x43是67,那么它的区位码就是3667,在对照表中读音为‘n'
       */
    private static char convert(byte[] bytes) {
        char result = '-';
        int secPosvalue = 0;
        int i;
        for (i = 0; i < bytes.length; i++) {
          bytes[i] -= GB_SP_DIFF;
        }
        secPosvalue = bytes[0] * 100 + bytes[1];
        for (i = 0; i < 23; i++) {
          if (secPosvalue >= secPosvalueList[i] &&
              secPosvalue < secPosvalueList[i + 1]) {
            result = firstLetter[i];
            break;
          }
        }
        return result;
    }
    public static void main(String[] args){
        String str = getFirstLetter("你好");
        System.out.println(str);
    }
    }
      

  5.   

    如果楼主可以使用AJAX还是尝试使用AJAX  autocomplete实际上代码也很少,也不太复杂