我们创建一个查询的界面search.jsp
<%@ page contentType="text/html;charset=GBK"%>
<%@ taglib uri="/struts-bean" prefix="bean" %>
<%@ taglib uri="/struts-html" prefix="html" %>
<%@ taglib uri="/struts-logic" prefix="logic" %>
<%@ page import="java.util.*"%>
<%@ page import="net.skycity.model.LogForm"%>
<%@ page import="net.skycity.blog.LogManager"%>
<style type="text/css">
body {
color: #333333;
font-size: 11px;
font-family: Tahoma, Verdana, sans-serif, "宋体";
margin: 0px;
padding: 0px;
background-position: center top;
}
table {font: 12px Verdana, Tahoma, sans-serif, "宋体";}
</style>
<form action="search.do" focus="query">
<tr>
<td>
<table width='100%' border='0' cellspacing='1' cellpadding='2'>
<tr>
<td align="left" bgcolor='#e2e2e2'>
<font color="#000000">文章分类:</font>
<select name="logTypeId">
<option value="0">全部类别</option>
<option value="1">JAVA</option>
<option value="2">AJAX</option>
<option value="3">STRUTS</option>
</select>
<font color="#000000">文章状态:</font>
<select name="state">
<option value="">全部状态</option>
<option value="0">公开</option>
<option value="1">草稿</option>
<option value="2">已删除</option>
</select>
<font color="#000000">作者:</font>
<select name="author">
<option value="admin">全部作者</option>
<option value="admin">admin</option>
</select>
<font color="#000000">搜索条件:</font>
<input type="text" name="query" value="">
<input name="submit" type="submit" value="查询" align="absmiddle" style="height:21px;width:40px;background-color: #F5F5F5"/>
<input name="submit" type="button" onclick="location.href('createIndex.jsp')" value="重建索引" align="absmiddle" style="height:21px;width:78px;background-color: #F5F5F5"/>
</td>
</tr>
</table>
<table width='100%' border='0' cellspacing='1' cellpadding='2'>
<logic:notEmpty name="logs">
<logic:iterate id="log" name="logs" indexId="idx" type="LogForm">
<tr>
<td align="left">
<a href="show.jsp?logId=<%=log.getLogId()%>"><bean:write name="log" property="logTitle"/></a>
</td>
<td>
<a href="delLog.jsp?logId=<%=log.getLogId()%>">删除</a>
</td>
</tr>
</logic:iterate>
</logic:notEmpty>
</table>
</td>
</tr>
</form>
form的search.do在struts-confgi.xml中如下定义
<action path="/search" type="net.skycity.action.SearchLogAction">
<forward name="show" path="/search.jsp" redirect="true"/>
</action>
SearchLogAction实现:
package net.skycity.action;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.skycity.blog.LogManager;
import net.skycity.util.WellsoonUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class SearchLogAction extends Action {
public ActionForward execute(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception
{
String author=request.getParameter("author");
String logTypeId=request.getParameter("logTypeId");
String state=request.getParameter("state");
String query=WellsoonUtil.transfer(request.getParameter("query")==null?"":request.getParameter("query"));
List logs=null;
if(!StringUtils.isEmpty(query)){
logs=LogManager.searchFor(logTypeId,state,author,query);
}else{
logs=LogManager.searchFor(logTypeId,state,author);
}
request.getSession().setAttribute("logs",logs);
return mapping.findForward("show");
}
}
下载
请自己添加jar文件:struts包,pg74.215.jdbc3.jar(我用的postgresql数据库),lucene-1.4-final.jar,lucene-demos-1.4-final.jar,log4j,dom4j,hibernate2.jar,commons-pool-1.1.jar,commons-dbcp-1.1.jar,cglib-full-2.0.2.jar
Lyyb2001