<1>jsp界面:result.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ page contentType="text/html;charset=GBK"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="org.apache.commons.lang.ObjectUtils.Null"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<%
ArrayList<ArrayList<String>> MyList = (ArrayList<ArrayList<String>>) request.getAttribute("bookList");
%>
<%
String searchType = (String) request.getAttribute("searchType");
if (null == searchType) {
searchType = "all";
}
%>
<%
int totalPage = 0;
%>
<%
String str_i = (String) request.getAttribute("i");
%>
<%
int i = Integer.parseInt(str_i);
%>
<head>
<title>result</title>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<style type="text/css">
<!--
.STYLE1 {color: #FFFFFF}
body {
background-color: white;
}
#Layer1 {
position:absolute;
400px;
height:146px;
z-index:1;
left: 424px;
top: 242px;
}
-->
</style>
</head>
<script type="text/javascript">
var xmlHttpRequest;
function createXmlHttpRequest()
{
if(window.ActiveXObject)
{
try
{
xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
}
return xmlHttpRequest;
}
else if(window.XMLHttpRequest)
{
return new XMLHttpRequest();
}
}
function auto()
{
var query = document.getElementById("query");
var auto = document.getElementById("auto");
var tags = document.getElementById("tags");
if(event.keyCode == 40)
{
if(query.value != "" && auto.style.visibility != "hidden")
{
tags.focus();
tags.selectedIndex = 0;
query.value = tags.options[0].text;
return;
}
}
xmlHttpRequest = createXmlHttpRequest();
xmlHttpRequest.onreadystatechange = backFct;
var url = "AjaxindexAction.action?tag=" + query.value;
xmlHttpRequest.open("get",url,true);
xmlHttpRequest.send(null);
}
function backFct()
{
if(xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200)
{
var rs = xmlHttpRequest.responseText;
if(rs != "")
{
var tagsRs = rs.split(",");
var auto = document.getElementById("auto");
var tags = document.getElementById("tags");
var query = document.getElementById("query");
tags.length = 0;
tags.size = tagsRs.length;
for(var i=0;i<tagsRs.length;i++)
{
var option = document.createElement("option");
option.setAttribute("text", tagsRs[i]);
tags.options[i] = option;
}
auto.style.width = query.style.width;
tags.style.width = query.style.width;
auto.style.left = query.offsetLeft - 1;
auto.style.top = query.offsetTop + query.offsetHeight + 1;
auto.style.visibility = "visible";
}
else
{
document.getElementById("auto").style.visibility = "hidden";
}
}
}
function text()
{
var query = document.getElementById("query");
var auto = document.getElementById("auto");
var tags = document.getElementById("tags");
if(event.keyCode == 40 || event.keyCode == 38)
{
if(query.value != "" && auto.style.visibility != "hidden")
{
query.value = tags.options[tags.selectedIndex].text;
}
}
else if(event.keyCode == 13)
{
auto.style.visibility = "hidden";
query.focus();
}
}
</script>
<script type="text/javascript" src="external.js"></script>
<body>
<div>
<form action="search.action" method="get" >
<table border="0" width="400px">
<tr size="80">
<td align="center">
<a
href="search.action?searchType=all&i=0&keywords=${requestScope.keywords}">
<%
if (searchType.equals("all"))
out.println("<strong>全部</strong>");
else
out.println("全部");
%> </a>
</td>
<td align="center">
<a
href="search.action?searchType=doc&i=0&keywords=${requestScope.keywords}">
<%
if (searchType.equals("doc"))
out.println("<strong>文档</strong>");
else
out.println("文档");
%> </a>
</td>
<td align="center">
<a
href="search.action?searchType=mov&i=0&keywords=${requestScope.keywords}">
<%
if (searchType.equals("mov"))
out.println("<strong>视频</strong>");
else
out.println("视频");
%> </a>
</td>
<td align="center">
<a
href="search.action?searchType=music&i=0&keywords=${requestScope.keywords}">
<%
if (searchType.equals("music"))
out.println("<strong>音乐</strong>");
else
out.println("音乐");
%> </a>
</td>
<td align="center">
<a
href="search.action?searchType=other&i=0&keywords=${requestScope.keywords}">
<%
if (searchType.equals("other"))
out.println("<strong>其他</strong>");
else
out.println("其他");
%> </a>
</td>
<td>
<br>
<br>
</td>
</tr>
<tr>
<td colspan="5">
<input type="text" name="keywords" id="query" onKeyUp="auto();" style=" 400px; height: 30px; font-size: 18px;"
value="<%=request.getAttribute("keywords") %>" />
<div id="auto" style="border-style: solid; border- 1px; visibility: hidden; position: absolute;margin-left:12px;margin-top:50px">
<select id="tags" onkeyup="text();" size="0" style=" margin:-2px;">
</select>
</div>
</td>
<td>
<input type="hidden" name="searchType" value="<%=searchType%>" />
<input type="hidden" name="i" value="<%=i%>" />
<input type="submit" style="height:32px; " value="搜索一下" />
</td>
</tr>
</table>
<table border="0" width="800px">
<br>
<HR
style="FILTER: alpha(opacity = 100, finishopacity = 0, style = 2)"
width="100%" color="blue" SIZE=10>
<h1>
搜索结果:
</h1>
<%
//对搜索结果进行分类计数
int totalNum = 0;
if (searchType.equals("all")) {
for (int m = 0; m < MyList.get(0).size(); m++) {
totalNum++;
}
} else if (searchType.equals("doc")) {
for (int m = 0; m < MyList.get(0).size(); m++) {
if (MyList.get(2).get(m).equals("doc")
|| MyList.get(2).get(m).equals("ppt")
|| MyList.get(2).get(m).equals("xls")
|| MyList.get(2).get(m).equals("txt")
|| MyList.get(2).get(m).equals("pdf")) {
totalNum++;
}
}
} else if (searchType.equals("mov")) {
for (int m = 0; m < MyList.get(0).size(); m++) {
if (MyList.get(2).get(m).equals("avi")
|| MyList.get(2).get(m).equals("mpg")
|| MyList.get(2).get(m).equals("3gp")
|| MyList.get(2).get(m).equals("mp4")
|| MyList.get(2).get(m).equals("asf")
|| MyList.get(2).get(m).equals("asx")
|| MyList.get(2).get(m).equals("flv")
|| MyList.get(2).get(m).equals("rmvb")
|| MyList.get(2).get(m).equals("rm")
|| MyList.get(2).get(m).equals("wmv9")
|| MyList.get(2).get(m).equals("mkv")
|| MyList.get(2).get(m).equals("swf")) {
totalNum++;
}
}
} else if (searchType.equals("music")) {
for (int m = 0; m < MyList.get(0).size(); m++) {
if (MyList.get(2).get(m).equals("aac")
|| MyList.get(2).get(m).equals("mp3")) {
totalNum++;
}
}
} else if (searchType.equals("other")) {
for (int m = 0; m < MyList.get(0).size(); m++) {
if (
MyList.get(2).get(m).equals("rar")
|| MyList.get(2).get(m).equals("jpeg")
|| MyList.get(2).get(m).equals("rar")
|| MyList.get(2).get(m).equals("gif")
|| MyList.get(2).get(m).equals("jpg")
|| MyList.get(2).get(m).equals("bmp")
|| MyList.get(2).get(m).equals("png")) {
totalNum++;
}
}
}
%>
<%
//对搜索结果进行分类输出
if (searchType.equals("all")) {
for (; i < MyList.get(0).size(); i++) {
%>
<a rel="external"
href="getTitle.action?describe=<%=MyList.get(3).get(i)%>&author=<%=MyList.get(6).get(i)%>
&kind=<%=MyList.get(2).get(i)%>&title=<%=MyList.get(0).get(i)%>&url=<%=MyList.get(5).get(i)%>" >
<font
size="4"> <%
out.println(MyList.get(0).get(i));
%> </font> </a>
<br>
<%
out.print("<font color='blue'>描述:</font>"
+ MyList.get(3).get(i));
%>
<%
out.print("类型为:" + MyList.get(2).get(i));
%>
<%
try {
out.print("<font color='blue'>全文:</font>"
+ MyList.get(8).get(i).substring(0, 100));
} catch (IndexOutOfBoundsException e) {
out.print("无内容");
}
out.print("....");
%>
<font size="2" color="green"> <%
out.print("作者为:" + MyList.get(6).get(i));
%> <%
out.print("出版社:" + MyList.get(7).get(i));
%> <%
out.print("url:" + MyList.get(5).get(i));
%> <%
out.print("时间:" + MyList.get(4).get(i));
%><br><br> </font>
<% if ((i + 1) % 10 == 0) {
break;
}
}
} if (searchType.equals("doc")) {
for (; i < MyList.get(0).size(); i++) {
if (MyList.get(2).get(i).equals("doc")
|| MyList.get(2).get(i).equals("ppt")
|| MyList.get(2).get(i).equals("xls")
|| MyList.get(2).get(i).equals("txt")
|| MyList.get(2).get(i).equals("pdf")) {
%>
<a rel="external"
href="getTitle.action?describe=<%=MyList.get(3).get(i)%>&author=<%=MyList.get(6).get(i)%>&
kind=<%=MyList.get(2).get(i)%>&title=<%=MyList.get(0).get(i)%>&url=<%=MyList.get(5).get(i)%>"><font
size="4"> <%
out.println(MyList.get(0).get(i));
%> </font> </a>
<br>
<%
out.print("<font color='blue'>描述:</font>"
+ MyList.get(3).get(i));
%>
<%
out.print("类型为:" + MyList.get(2).get(i));
%>
<%
try {
out.print("<font color='blue'>全文:</font>"
+ MyList.get(8).get(i).substring(0, 100));
} catch (IndexOutOfBoundsException e) {
out.print("无内容");
}
out.print("....");
%>
<font size="2" color="green"> <%
out.print("作者为:" + MyList.get(6).get(i));
%> <%
out.print("出版社:" + MyList.get(7).get(i));
%> <%
out.print("url:" + MyList.get(5).get(i));
%> <%
out.print("时间:" + MyList.get(4).get(i));
%><br><br> </font>
<%if ((i + 1) % 10 == 0) {
break;
}
}
}
} if (searchType.equals("mov")) {
for (; i < MyList.get(0).size(); i++) {
if (MyList.get(2).get(i).equals("avi")
|| MyList.get(2).get(i).equals("mpg")
|| MyList.get(2).get(i).equals("3gp")
|| MyList.get(2).get(i).equals("mp4")
|| MyList.get(2).get(i).equals("asf")
|| MyList.get(2).get(i).equals("asx")
|| MyList.get(2).get(i).equals("flv")
|| MyList.get(2).get(i).equals("rmvb")
|| MyList.get(2).get(i).equals("rm")
|| MyList.get(2).get(i).equals("wmv9")
|| MyList.get(2).get(i).equals("mkv")
|| MyList.get(2).get(i).equals("swf")) {
%>
<a rel="external"
href="getTitle.action?describe=<%=MyList.get(3).get(i)%>&author=<%=MyList.get(6).get(i)%>&kind=<%=MyList.get(2).get(i)%>
&title=<%=MyList.get(0).get(i)%>&url=<%=MyList.get(5).get(i)%>" ><font
size="4"> <%
out.println(MyList.get(0).get(i));
%> </font> </a>
<br>
<%
out.print("<font color='blue'>描述:</font>"
+ MyList.get(3).get(i));
%>
<%
out.print("类型为:" + MyList.get(2).get(i));
%>
<%
try {
out.print("<font color='blue'>全文:</font>"
+ MyList.get(8).get(i).substring(0, 100));
} catch (IndexOutOfBoundsException e) {
out.print("无内容");
}
out.print("....");
%>
<font size="2" color="green"> <%
out.print("作者为:" + MyList.get(6).get(i));
%> <%
out.print("出版社:" + MyList.get(7).get(i));
%> <%
out.print("url:" + MyList.get(5).get(i));
%> <%
out.print("时间:" + MyList.get(4).get(i));
%><br> <br></font>
<%if ((i + 1) % 10 == 0) {
break;
}
}
}
} if (searchType.equals("music")) {
for (; i < MyList.get(0).size(); i++) {
if (MyList.get(2).get(i).equals("aac")
|| MyList.get(2).get(i).equals("mp3")) {
if ((i + 1) % 10 == 0) {
break;
}
%>
<a rel="external"
href="getTitle.action?describe=<%=MyList.get(3).get(i)%>&author=<%=MyList.get(6).get(i)%>
&kind=<%=MyList.get(2).get(i)%>&title=<%=MyList.get(0).get(i)%>&url=<%=MyList.get(5).get(i)%>" ><font
size="4"> <%
out.println(MyList.get(0).get(i));
%> </font> </a>
<br>
<%
out.print("<font color='blue'>描述:</font>"
+ MyList.get(3).get(i));
%>
<%
out.print("类型为:" + MyList.get(2).get(i));
%>
<%
try {
out.print("<font color='blue'>全文:</font>"
+ MyList.get(8).get(i).substring(0, 100));
} catch (IndexOutOfBoundsException e) {
out.print("无内容");
}
out.print("....");
%>
<font size="2" color="green"> <%
out.print("作者为:" + MyList.get(6).get(i));
%> <%
out.print("出版社:" + MyList.get(7).get(i));
%> <%
out.print("url:" + MyList.get(5).get(i));
%> <%
out.print("时间:" + MyList.get(4).get(i));
%><br><br> </font>
<%
}
}
} if (searchType.equals("other")) {
for (; i < MyList.get(0).size(); i++) {
if (MyList.get(2).get(i).equals("rar")
|| MyList.get(2).get(i).equals("jpeg")
|| MyList.get(2).get(i).equals("rar")
|| MyList.get(2).get(i).equals("gif")
|| MyList.get(2).get(i).equals("jpg")
|| MyList.get(2).get(i).equals("bmp")
|| MyList.get(2).get(i).equals("png")) {
%>
<a rel="external"
href="getTitle.action?describe=<%=MyList.get(3).get(i)%>&author=<%=MyList.get(6).get(i)%>&
kind=<%=MyList.get(2).get(i)%>&title=<%=MyList.get(0).get(i)%>&url=<%=MyList.get(5).get(i)%>"><font
size="4"> <%
out.println(MyList.get(0).get(i));
%> </font> </a>
<br>
<%
out.print("<font color='blue'>描述:</font>"
+ MyList.get(3).get(i));
%>
<%
out.print("类型为:" + MyList.get(2).get(i));
%>
<%
try {
out.print("<font color='blue'>全文:</font>"
+ MyList.get(8).get(i).substring(0, 100));
} catch (IndexOutOfBoundsException e) {
out.print("无内容");
}
out.print("....");
%>
<font size="2" color="green"> <%
out.print("作者为:" + MyList.get(6).get(i));
%> <%
out.print("出版社:" + MyList.get(7).get(i));
%> <%
out.print("url:" + MyList.get(5).get(i));
%> <%
out.print("时间:" + MyList.get(4).get(i));
%><br><br> </font>
<%if ((i + 1) % 10 == 0) {
break;
}
}
}
}
%>
<br>
</table>
<%if(totalNum%10!=0){
totalPage = totalNum / 10 + 1;
}
else{
totalPage = totalNum / 10 ;
}
%>
<%
String string_i;
%>
</form>
</div>
<br>
<form action="search.action" method="post">
<table border="0" width="400px">
<tr size="80">
<%if(totalPage!=1){
for (int j = 0; j < totalPage; j++) {
string_i = String.valueOf(j + 1);
%>
<a
href='search.action?searchType=${requestScope.searchType}&keywords=${requestScope.keywords}&i=<%=j * 10%>'>
<%
out.println("第" + string_i + "页");
%> </a>
<%
}
}
%>
</table>
</form>
<div id="split_line">
<span id="tpl_infor">搜索 <b><s:property value="keywords"
escape="false" /> </b> 获得约<%=totalNum%>条结果. (用时 <%=MyList.get(9).get(0)%>毫秒)</span>
</div>
<div id="auto" position: absolute; margin-left:429px; margin-top:230px; left: 489px; top: -52px;">
相关搜索:
<% List<String> titlelist = new ArrayList<String>();
titlelist = (List<String>)request.getAttribute("titlelist");
%>
<%
int b = 1;
for(int a = 0; a < titlelist.size(); a++){
if(b%5==0){
out.print("<br>");
}
b++;
%>
<a href="search.action?searchType=${requestScope.searchType}&keywords=<%=titlelist.get(a)%>&i=0">
<%out.println(titlelist.get(a));%>
</a>
<%
}
%>
</div>
</body>
</html>
<2>编写字符串相似度比较类:
StringCompare.java
package com.dreamers.search;
public class StringCompare {
private int compare(String str, String target) {
int d[][]; // 矩阵
int n = str.length();
int m = target.length();
int i; // 遍历str的
int j; // 遍历target的
char ch1; // str的
char ch2; // target的
int temp; // 记录相同字符,在某个矩阵位置值的增量,不是0就是1
if (n == 0) {
return m;
}
if (m == 0) {
return n;
}
d = new int[n + 1][m + 1];
for (i = 0; i <= n; i++) { // 初始化第一列
d[i][0] = i;
}
for (j = 0; j <= m; j++) { // 初始化第一行
d[0][j] = j;
}
for (i = 1; i <= n; i++) { // 遍历str
ch1 = str.charAt(i - 1);
// 去匹配target
for (j = 1; j <= m; j++) {
ch2 = target.charAt(j - 1);
if (ch1 == ch2) {
temp = 0;
} else {
temp = 1;
}
// 左边+1,上边+1, 左上角+temp取最小
d[i][j] = min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1]
+ temp);
}
}
return d[n][m];
}
private int min(int one, int two, int three) {
return (one = one < two ? one : two) < three ? one : three;
}
/**
* 获取两字符串的相似度
*
* @param str
* @param target
* @return
*/
public float getSimilarityRatio(String str, String target) {
return 1 - (float) compare(str, target)
/ Math.max(str.length(), target.length());
}
/* public static void main(String[] args) {
StringCompare lt = new StringCompare();
String str = "ABCDEF";
String target = "FECDBA";
System.out.println("similarityRatio="
+ lt.getSimilarityRatio(str, target));
}*/
}
<3>在action里相关代码:
public List<String> titlelist = new ArrayList<String>() ;
public List<String> db = new ArrayList<String>(); //
public List<String> init() throws DocumentException, FileNotFoundException, IOException, ClassNotFoundException
{
XmlReader reader = new XmlReader();
db=reader.getTitle(); //读取xml中的资源
StringCompare it = new StringCompare();
for(int i = 0 ; i < db.size(); i++){
if(it.getSimilarityRatio(getKeywords(), db.get(i))>0.3){
titlelist.add(db.get(i));
}
}
return titlelist;
}
public List<String> getTitlelist() throws FileNotFoundException, DocumentException, IOException, ClassNotFoundException {
titlelist = init();
System.out.println("titlelist :"+titlelist.get(0));
return titlelist;
}
public void setTitlelist(List<String> titlelist) {
this.titlelist = titlelist;
}
大概就是这样吧,可能有些问题,呵呵,又事留言吧。