为什么枚举要实现接口?
268
2023-07-03
用java等语言仿360首页拼音输入全模糊搜索和自动换肤
360首页搜索效果如下
1、完成编写的schoolnet校园网主要目录结构如下
主要实现支持中文、拼音首字母、拼音全字母的智能搜索和换肤
页面效果如下
主要核心代码如下
1、head.jsp
<%@page import="java.io.File"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
onclick="setsearchtype(this),setsearch()" name="searchdatas" id="books.png">资料 onclick="setsearchtype(this),setsearch()" name="searchsongs" id="t01d566d0c28b32ddad.png">音乐 onclick="setsearchtype(this),setsearch()" name="searchnews" id="newspaper.png">新闻 onclick="setsearchtype(this),setsearch()" name="searchxiaoshuos" id="library.png">小说 onclick="setsearchtype(this),setsearch()" name="searcharticles" id="blog.gif">日志
onclick="setsearchtype(this),setsearch()" name="searchdatas"
id="books.png">资料
onclick="setsearchtype(this),setsearch()" name="searchsongs"
id="t01d566d0c28b32ddad.png">音乐
onclick="setsearchtype(this),setsearch()" name="searchnews"
id="newspaper.png">新闻
onclick="setsearchtype(this),setsearch()" name="searchxiaoshuos"
id="library.png">小说
onclick="setsearchtype(this),setsearch()" name="searcharticles"
id="blog.gif">日志
action="/schoolnet/other.do?flag=searchdatas&searchpageNow=1"
method="post" name="searchdatas" target="blank"
onsubmit="return checksearch()">
width="32px" height="32px" style="margin-bottom:-10px" />
type="text" class="topsearchtext" name="search" id="topsearchtext"
onkeyup="setsearch()" onfocus="newsearch()" "/>
id="inputsearchtype" type="submit" class="topsearchbutton"
value="搜索资料" />
id="*menu_selected">校园网
title="">说说
href="/schoolnet/album.do?flag=myAlbumUI&pageNow=1&userid=${loginuser.id }"
title="">相册
title="">好友
href="/schoolnet/article.do?flag=articleUI&pageNow=1&userid=${loginuser.id }"
title="">日志
title="">音乐
href="/schoolnet/news.do?flag=gonewsUI&typeid=1&pageNow=1"
title="">新闻
href="/schoolnet/datas.do?flag=godatasUI&pageNow=1&action=all"
title="">资料
href="/schoolnet/xiaoshuo.do?flag=goxiaoshuoUI&pageNow=1&id=all"
title="">小说
href="/schoolnet/skin.do?flag=goskinUI&pageNow=1&id=all" title="">装扮
title="">新鲜事
href="/schoolnet/login.do?flag=goHomeUI">
src="/schoolnet/images/head/${loginuser.photo}" width="120px"
height="45px" style=" margin-top:-18px;margin-left:-10px" />
2、主要js函数
//用于设置切换搜索类型,触发input控件焦点时显示对应的热门(前五个)内容(资料、音乐、新闻、小说、日志)
function setsearchtype(o){
$("#searchtype a").removeClass("current");
$(o).addClass("current");
var inputsearchtype=$(o).text();
$("#inputsearchtype").val("搜索"+inputsearchtype);
document.getElementById("search-form").action="/schoolnet/other.do?flag="+o.name+"&searchpageNow=1";
document.getElementById("search-form").name=o.name;
document.getElementById("searchimg").src="/schoolnet/images/front/"+o.id;
$("#topsearchtext").show();
$("#searchfriendss").hide();
if(o.name=="searchfriends")
{
$("#topsearchtext").hide();
$("#searchfriendss").show();
}
}
//用于设置显示智能提示的内容(模糊搜索时匹配的前十个)
function setsearch()
{
var v=document.getElementById("topsearchtext").value;
var type=document.getElementById("search-form").name;
if(v.length==0)
{
$("#setsearch").html("");
}
if(v.length>0)
{
$.ajax({
cache: true,
type: "POST",
url : "/schoolnet/ChangeInfo",
data: {"action":"setsearch","search":v,"searchtype":type},
async: false,
dataType: "html",
success: function(data) {
$("#setsearch").html("");
$("#setsearch").append(data);
}
});
};
}
主要java文件
if("searchsongs".equals(searchtype))
{
String nameHEAD[]=ChinesePinYinTool.getHeadByString(search);
String nameHeadString="";
for (int i = 0; i < nameHEAD.length; i++) {
nameHeadString+=nameHEAD[i];
}
String nameALL[]=ChinesePinYinTool.stringToPinyin(search);
String nameAllString="";
for (int i = 0; i < nameALL.length; i++) {
nameAllString+=nameALL[i];
}
List
for(int i=0;i { con +="
{
con +="
}
if (songs.size()!=0) {
con +="
}
else {
con +="
}
out.write(con);
out.close();
}
else if("setpifu".equals(action))
{
String skinid=request.getParameter("skinid");
Skin skin=(Skin) universityService.findById(Skin.class, Integer.valueOf(skinid));
Users user=(Users) request.getSession().getAttribute("loginuser");
user.setSkin(skin);
universityService.update(user);
}
做拼音转换时需导入pinyin4j.jar包
ChinesePinYinTool拼音转换工具类
package schoolnet.utils;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
/**
*
* 汉语拼音工具
*
*
*
*/
public class ChinesePinYinTool {
public static void main(String[] args) {
String string[]=stringToPinyin("这个");
for (int i = 0; i < string.length; i++) {
System.out.println(string[i]);
}
}
/**
*
* 将字符串转换成拼音数
*
*
* @param src
* @return
*/
public static String[] stringToPinyin(String src) {
return stringToPinyin(src, false, null);
}
/**
* 将字符串转换成拼音数
*
*
* @param src
* @return
*/
public static String[] stringToPinyin(String src, String separator) {
return stringToPinyin(src, true, separator);
}
/**
* 将字符串转换成拼音数
*
*
* @param src
* @param isPolyphone
* 是否查出多音字的拼音
*
* @param separator
* 多音字拼音之间的分隔
*
* @return
*/
public static String[] stringToPinyin(String src, boolean isPolyphone,
String separator) {
// 判断字符串是否为
if ("".equals(src) || null == src) {
return null;
}
char[] srcChar = src.toCharArray();
int srcCount = srcChar.length;
String[] srcStr = new String[srcCount];
for (int i = 0; i < srcCount; i++) {
srcStr[i] = charToPinyin(srcChar[i], isPolyphone, separator);
}
return srcStr;
}
/**
* 将单个字符转换成拼音
*
* @param src
* @return
*/
public static String charToPinyin(char src, boolean isPolyphone,
String separator) {
// 创建汉语拼音处理
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
// 输出设置,大小写,音标方
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
defaultFormat.MUecPysetToneType(HanyuPinyinToneType.WITHOUT_TONE);
StringBuffer tempPinying = new StringBuffer();
// 如果是中
if (src > 128) {
try {
// 转换得出结果
String[] strs = PinyinHelper.toHanyuPinyinStringArray(src,
defaultFormat);
// 是否查出多音字,默认是查出多音字的第字符
if (isPolyphone && null != separator) {
for (int i = 0; i < strs.length; i++) {
tempPinying.append(strs[i]);
if (strs.length != (i + 1)) {
// 多音字之间用特殊符号间隔起来
tempPinying.append(separator);
}
}
} else {
tempPinying.append(strs[0]);
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
} else {
tempPinying.append(src);
}
return tempPinying.toString();
}
public static String hanziToPinyin(String hanzi) {
return hanziToPinyin(hanzi, "");
}
/**
* 将汉字转换成拼音
*
* @param hanzi
* @param separator
* @return
*/
@SuppressWarnings("deprecation")
public static String hanziToPinyin(String hanzi, String separator) {
// 创建汉语拼音处理
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
// 输出设置,大小写,音标方
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
String pinyingStr = "";
try {
pinyingStr = PinyinHelper.toHanyuPinyinString(hanzi, defaultFormat,
separator);
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
return pinyingStr;
}
/**
* 将字http://符串数组转换成字符串
*
* @param str
* @param separator
* 各个字符串之间的分隔
*
* @return
*/
public static String stringArrayToString(String[] str, String separator) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < str.length; i++) {
sb.append(str[i]);
if (str.length != (i + 1)) {
sb.append(separator);
}
}
return sb.toString();
}
/**
* 的将各个字符数组之间连接起来
*
*
* @param str
* @return
*/
public static String stringArrayToString(String[] str) {
return stringArrayToString(str, "");
}
/**
* 将字符数组转换成字符
*
*
* @param str
* @param separator
* 各个字符串之间的分隔
*
* @return
*/
public static String charArrayToString(char[] ch, String separator) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < ch.length; i++) {
sb.append(ch[i]);
if (ch.length != (i + 1)) {
sb.append(separator);
}
}
return sb.toString();
}
/**
* 将字符数组转换成字符
*
*
* @param str
* @return
*/
public static String charArrayToString(char[] ch) {
return charArrayToString(ch, " ");
}
/**
* 取汉字的首字
*
*
* @param src
* @param isCapital
* 是否是大
*
* @return
*/
public static char[] getHeadByChar(char src, boolean isCapital) {
// 如果不是汉字直接返回
if (src <= 128) {
return new char[] { src };
}
// 获取的拼
String[] pinyingStr = PinyinHelper.toHanyuPinyinStringArray(src);
// 创建返回对象
int polyphoneSize = pinyingStr.length;
char[] headChars = new char[polyphoneSize];
int i = 0;
// 截取首字
for (String s : pinyingStr) {
char headChar = s.charAt(0);
// 首字母是否大写,默认是小
if (isCapital) {
headChars[i] = Character.toUpperCase(headChar);
} else {
headChars[i] = headChar;
}
i++;
}
return headChars;http://
}
/**
* 取汉字的首字默认是大
*
* @param src
* @return
*/
public static char[] getHeadByChar(char src) {
return getHeadByChar(src, true);
}
/**
* 查找字符串首字母
*
* @param src
* @return
*/
public static String[] getHeadByString(String src) {
return getHeadByString(src, true);
}
/**
* 查找字符串首字母
*
* @param src
* @param isCapital
* 是否大写
* @return
*/
public static String[] getHeadByString(String src, boolean isCapital) {
return getHeadByString(src, isCapital, null);
}
/**
* 查找字符串首字母
*
* @param src
* @param isCapital
* 是否大写
* @param separator
* 分隔
*
* @return
*/
public static String[] getHeadByString(String src, boolean isCapital,
String separator) {
char[] chars = src.toCharArray();
String[] headString = new String[chars.length];
int i = 0;
for (char ch : chars) {
char[] chs = getHeadByChar(ch, isCapital);
StringBuffer sb = new StringBuffer();
if (null != separator) {
int j = 1;
for (char ch1 : chs) {
sb.append(ch1);
if (j != chs.length) {
sb.append(separator);
}
j++;
}
} else {
sb.append(chs[0]);
}
headString[i] = sb.toString();
i++;
}
return headString;
}
}
数据库建立相关函数
fristPinyin : 此函数是将一个中文字符串的第一个汉字转成拼音首字母 (例如:"好的"->h)
pinyin :此函数是将一个中文字符串对应拼音首字母的每个相连 (例如:"好的"->hd)
to_pinyin :此函数是将一个中文字符串对应拼音全字母的每个相连 (例如:"好的"->haode)
CREATE FUNCTION `fristPinyin`(P_NAME VARCHAR(255)) RETURNS varchar(255) CHARSET utf8
BEGIN
DECLARE V_RETURN VARCHAR(255);
SET V_RETURN = ELT(INTERVAL(CONV(HEX(left(CONVERT(P_NAME USING gbk),1)),16,10),
0xB0A1,0xB0C5,0xB2C1,0xB4EE,0xB6EA,0xB7A2,0xB8C1,0xB9FE,0xBBF7,
0xBFA6,0xC0AC,0xC2E8,0xC4C3,0xC5B6,0xC5BE,0xC6DA,0xC8BB,
0xC8F6,0xCBFA,0xCDDA,0xCEF4,0xD1B9,0xD4D1),
'A','B','C','D','E','F','G','H','J','K','L','M','N','O','P','Q','R','S','T','W','X','Y','Z');
RETURN V_RETURN;
END
CREATE FUNCTION `pinyin`(P_NAME VARCHAR(255)) RETURNS varchar(255) CHARSET utf8
BEGIN
DECLARE V_COMPARE VARCHAR(255);
DECLARE V_RETURN VARCHAR(255);
DECLARE I INT;
SET I = 1;
SET V_RETURN = '';
while I < LENGTH(P_NAME) do
SET V_COMPARE = SUBSTR(P_NAME, I, 1);
IF (V_COMPARE != '') THEN
#SET V_RETURN = CONCAT(V_RETURN, ',', V_COMPARE);
SET V_RETURN = CONCAT(V_RETURN, fristPinyin(V_COMPARE));
#SET V_RETURN = fristPinyin(V_COMPARE);
END IF;
SET I = I + 1;
end while;
IF (ISNULL(V_RETURN) or V_RETURN = '') THEN
SET V_RETURN = P_NAME;
END IF;
RETURN V_RETURN;
END
CREATE FUNCTION to_pinyin(NAME VARCHAR(255) CHARSET gbk)
RETURNS VARCHAR(255) CHARSET gbk
BEGIN
DECLARE mycode INT;
DECLARE tmp_lcode VARCHAR(2) CHARSET gbk;
DECLARE lcode INT;
DECLARE tmp_rcode VARCHAR(2) CHARSET gbk;
DECLARE rcode INT;
DECLARE mypy VARCHAR(255) CHARSET gbk DEFAULT '';
DECLARE lp INT;
SET mycode = 0;
SET lp = 1;
SET NAME = HEX(NAME);
WHILE lp < LENGTH(NAME) DO
SET tmp_lcode = SUBSTRING(NAME, lp, 2);
SET lcode = CAST(ASCII(UNHEX(tmp_lcode)) AS UNSIGNED);
SET tmp_rcode = SUBSTRING(NAME, lp + 2, 2);
SET rcode = CAST(ASCII(UNHEX(tmp_rcode)) AS UNSIGNED);
IF lcode > 128 THEN
SET mycode =65536 - lcode * 256 - rcode ;
SELECT CONCAT(mypy,pin_yin_) INTO mypy FROM t_base_pinyin WHERE CODE_ >= ABS(mycode) ORDER BY CODE_ ASC LIMIT 1;
SET lp = lp + 4;
ELSE
SET mypy = CONCAT(mypy,CHAR(CAST(ASCII(UNHEX(SUBSTRING(NAME, lp, 2))) AS UNSIGNED)));
SET lp = lp + 2;
END IF;
END WHILE;
RETURN LOWER(mypy);
END;
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~