欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

用java等语言仿360首页拼音输入全模糊搜索和自动换肤

程序员文章站 2024-03-12 13:35:50
360首页搜索效果如下 1、完成编写的schoolnet校园网主要目录结构如下 主要实现支持中文、拼音首字母、拼音全字母的智能搜索和换肤 页面效果...

360首页搜索效果如下

用java等语言仿360首页拼音输入全模糊搜索和自动换肤

用java等语言仿360首页拼音输入全模糊搜索和自动换肤用java等语言仿360首页拼音输入全模糊搜索和自动换肤

1、完成编写的schoolnet校园网主要目录结构如下

用java等语言仿360首页拼音输入全模糊搜索和自动换肤

主要实现支持中文、拼音首字母、拼音全字母的智能搜索和换肤

页面效果如下

用java等语言仿360首页拼音输入全模糊搜索和自动换肤

用java等语言仿360首页拼音输入全模糊搜索和自动换肤用java等语言仿360首页拼音输入全模糊搜索和自动换肤用java等语言仿360首页拼音输入全模糊搜索和自动换肤用java等语言仿360首页拼音输入全模糊搜索和自动换肤

用java等语言仿360首页拼音输入全模糊搜索和自动换肤

主要核心代码如下

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"%> 
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> 
<html> 
<head> 
<title>校园网首页面</title> 
<link rel="stylesheet" href="/schoolnet/css/index.css" type="text/css" /> 
<script type="text/javascript" src="/schoolnet/js/ddsmoothmenu.js"></script> 
<script src="/schoolnet/js/jquery.js"></script> 
<script src="/schoolnet/js/jquery-ui.js"></script> 
<script type="text/javascript" src="/schoolnet/js/publicjs.js"></script> 
<script type="text/javascript" src="/schoolnet/myajax/ajax_uni.js"></script> 
<script type="text/javascript" src="/schoolnet/js/style.js"></script> 
<c:if test="${loginuser.skin.id!=null }"> 
 <script type="text/javascript"> 
  $(document).ready( 
 
    function setskin() { 
     var photo = '${loginuser.skin.photo}'; 
     $("body").css( 
       "background", 
       "#f6f6f6 url(/schoolnet/images/pf/" + photo 
         + ") top repeat"); 
    }); 
 </script> 
 
 
</c:if> 
</head> 
<body> 
 <div class="topsearch"> 
  <div class="topsearchtype" id="searchtype"> 
   <a class="current" href="javascript:void(0)" 
    onclick="setsearchtype(this),setsearch()" name="searchdatas" 
    id="books.png">资料</a> <a href="javascript:void(0)" 
    onclick="setsearchtype(this),setsearch()" name="searchsongs" 
    id="t01d566d0c28b32ddad.png">音乐</a> <a href="javascript:void(0)" 
    onclick="setsearchtype(this),setsearch()" name="searchnews" 
    id="newspaper.png">新闻</a> <a href="javascript:void(0)" 
    onclick="setsearchtype(this),setsearch()" name="searchxiaoshuos" 
    id="library.png">小说</a> <a href="javascript:void(0)" 
    onclick="setsearchtype(this),setsearch()" name="searcharticles" 
    id="blog.gif">日志</a> 
  </div> 
  <form id="search-form" 
   action="/schoolnet/other.do?flag=searchdatas&searchpagenow=1" 
   method="post" name="searchdatas" target="blank" 
   onsubmit="return checksearch()"> 
   <img src="/schoolnet/images/front/books.png" id="searchimg" 
    width="32px" height="32px" style="margin-bottom:-10px" /><input 
    type="text" class="topsearchtext" name="search" id="topsearchtext" 
    onkeyup="setsearch()" onfocus="newsearch()" "/> <input 
    id="inputsearchtype" type="submit" class="topsearchbutton" 
    value="搜索资料" /> 
  </form> 
  <ul id="setsearch" class="setsearch"> 
  </ul> 
 </div> 
 <div id="mainmenu" class="ddsmoothmenu"> 
  <ul> 
   <li><a href="/schoolnet/login.do?flag=gohomeui" title="" 
    id="*menu_selected"><span>校园网</span> 
   </a> 
   </li> 
   <li><a href="/schoolnet/login.do?flag=gohomeui" title=""><span>个人中心</span> 
   </a></li> 
   <li><a href="/schoolnet/profile.do?flag=gohomepageui" title=""><span>我的主页</span> 
   </a></li> 
   <li><a href="javascript:void(0)" title=""><span>应用</span> 
   </a> 
   <ul class="menulevel"> 
     <li><a href="/schoolnet/shou.do?flag=shuoshuo&pagenow=1" 
      title="">说说</a> 
     </li> 
     <li><a 
      href="/schoolnet/album.do?flag=myalbumui&pagenow=1&userid=${loginuser.id }" 
      title="">相册</a> 
     </li> 
     <li><a href="/schoolnet/friend.do?flag=myfriendui&pagenow=1" 
      title="">好友</a> 
     </li> 
     <li><a 
      href="/schoolnet/article.do?flag=articleui&pagenow=1&userid=${loginuser.id }" 
      title="">日志</a> 
     </li> 
     <li><a href="/schoolnet/music.do?flag=gomusichome&pagenow=1" 
      title="">音乐</a> 
     </li> 
     <li><a 
      href="/schoolnet/news.do?flag=gonewsui&typeid=1&pagenow=1" 
      title="">新闻</a> 
     </li> 
     <li><a 
      href="/schoolnet/datas.do?flag=godatasui&pagenow=1&action=all" 
      title="">资料</a> 
     </li> 
     <li><a 
      href="/schoolnet/xiaoshuo.do?flag=goxiaoshuoui&pagenow=1&id=all" 
      title="">小说</a> 
     </li> 
     <li><a 
      href="/schoolnet/skin.do?flag=goskinui&pagenow=1&id=all" title="">装扮</a> 
     </li> 
     <li><a href="/schoolnet/share .do?flag=goshareui&pagenow=1" 
      title="">新鲜事</a> 
     </li> 
    </ul> 
   </li> 
   <li><a href="javascript:void(0)" title=""><span>关于</span> 
   </a> 
   <ul class="menulevel"> 
     <li><a href="/schoolnet/other.do?flag=goaboutui#1" title="">关于我们</a> 
     </li> 
     <li><a href="/schoolnet/other.do?flag=goaboutui#2" title="">网站地图</a> 
     </li> 
     <li><a href="/schoolnet/other.do?flag=goaboutui#3" title="">版权说明</a> 
     </li> 
     <li><a href="/schoolnet/other.do?flag=goaboutui#4" title="">友情链接</a> 
     </li> 
    </ul> 
   </li> 
   <li><a href="javascript:void(0)" title=""><span>设置</span> 
   </a> 
   <ul class="menulevel"> 
     <li><a href="/schoolnet/register.do?flag=updui">修改个人信息</a> 
     </li> 
     <li><a href="/schoolnet/login.do?flag=logout">退出</a> 
     </li> 
    </ul> 
   </li> 
  </ul> 
  <c:if test="${loginuser!=null }"> 
   <div id="page-wrap"> 
    <ul class="dropdown"> 
     <li id="menu_selected"><a id="menu_selected" 
      href="/schoolnet/login.do?flag=gohomeui">  <img 
       src="/schoolnet/images/head/${loginuser.photo}" width="120px" 
       height="45px" style=" margin-top:-18px;margin-left:-10px" /> 
     </a> 
      <ul class="sub_menu"> 
       <li><a href="/schoolnet/register.do?flag=updui">修改个人信息</a> 
       </li> 
       <li><a href="/schoolnet/login.do?flag=logout">退出</a> 
       </li> 
      </ul></li> 
    </ul> 
   </div> 
  </c:if> 
 </div> 
</body> 
</html> 

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)) 
<span style="white-space:pre">   </span>{ 
<span style="white-space:pre">    </span>string namehead[]=chinesepinyintool.getheadbystring(search); 
<span style="white-space:pre">    </span>string nameheadstring=""; 
<span style="white-space:pre">    </span>for (int i = 0; i < namehead.length; i++) { 
<span style="white-space:pre">     </span>nameheadstring+=namehead[i]; 
<span style="white-space:pre">    </span>} 
<span style="white-space:pre">    </span>string nameall[]=chinesepinyintool.stringtopinyin(search); 
<span style="white-space:pre">    </span>string nameallstring=""; 
<span style="white-space:pre">    </span>for (int i = 0; i < nameall.length; i++) { 
<span style="white-space:pre">     </span>nameallstring+=nameall[i]; 
<span style="white-space:pre">    </span>} 
<span style="white-space:pre">    </span>list<song> songs=universityservice.executequerybypage("from song where to_pinyin(name) like '%"+ nameallstring +"%' or pinyin(name) like '%"+ nameheadstring +"%'", null, 1, 10); 
<span style="white-space:pre">    </span>for(int i=0;i<songs.size();i++) 
<span style="white-space:pre">    </span>{ 
<span style="white-space:pre">     </span>con +="<li><a target='blank' href='/schoolnet/music.do?flag=gopatentdisplay&pid="+songs.get(i).getpatent().getid()+"'>"+songs.get(i).getname()+"</a></li>"; 
<span style="white-space:pre">    </span>} 
<span style="white-space:pre">    </span>if (songs.size()!=0) { 
<span style="white-space:pre">     </span>con +="<li><a href='javascript:void(0)' onclick='hideli()' style='display:inline;float:right;margin-right:8px;'>关闭</a></li>"; 
<span style="white-space:pre">    </span>} 
<span style="white-space:pre">    </span>else { 
<span style="white-space:pre">     </span>con +="<li><a href='javascript:void(0)' onclick='hideli()' style='display:inline;'>无任意匹配结果</a></li>"; 
<span style="white-space:pre">    </span>} 
<span style="white-space:pre">    </span>out.write(con); 
<span style="white-space:pre">    </span>out.close(); 
<span style="white-space:pre">   </span>} 
else if("setpifu".equals(action)) 
<span style="white-space:pre">  </span>{ 
<span style="white-space:pre">   </span>string skinid=request.getparameter("skinid"); 
<span style="white-space:pre">   </span>skin skin=(skin) universityservice.findbyid(skin.class, integer.valueof(skinid)); 
<span style="white-space:pre">   </span>users user=(users) request.getsession().getattribute("loginuser"); 
<span style="white-space:pre">   </span>user.setskin(skin); 
<span style="white-space:pre">   </span>universityservice.update(user); 
<span style="white-space:pre">  </span>} 

做拼音转换时需导入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.settonetype(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; 
 } 
 
 /** 
  * 将字符串数组转换成字符串 
  * 
  * @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; 
 } 
 
 /** 
  * 取汉字的首字默认是大 
  * 
  * @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;

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。