BaseAction.java 基础类
/**
* Class Name: BaseAction.java
* @author JLC
* @version 1.0
*/
public class BaseAction extends ActionSupport {
private static final long serialVersionUID = -2983580695806850367L;
/**
*
*/
protected ServletContext getServletContext(){
return ServletActionContext.getServletContext();
}
/**
*
*/
protected HttpServletRequest getRequest() {
return ServletActionContext.getRequest();
}
/**
*
*/
protected HttpServletResponse getResponse() {
return ServletActionContext.getResponse();
}
/**
*
*/
protected HttpSession getSession() {
return getRequest().getSession();
}
/**
* 取得访问域名
* @return
*/
public String getURL(){
return getRequest().getServerName();
}
/**
* 存放Cookie
* @param name
* @param value
*/
public void addCookie(String name,String value){
Cookie cookie = new Cookie(name, value);
cookie.setMaxAge(60*60*24);
cookie.setPath("/");
getResponse().addCookie(cookie);
}
/**
* 删除Cookie
* @param name
* @param value
*/
public void removeCookie(String name){
Cookie cookie = new Cookie(name, null);
cookie.setMaxAge(0);
cookie.setPath("/");
getResponse().addCookie(cookie);
}
/**
* Cookie取得
* @return
* @throws Exception
*/
public String getCookie(String name){
try{
Cookie[] cookies = getRequest().getCookies();
for(Cookie cookie : cookies){
cookie.setPath("/");
if(cookie.getName().equals(name)){
return cookie.getValue();
}
}
}catch(Exception e){ }
return "";
}
/**
* 获取IP地址
*/
public String getIpAddress(){
HttpServletRequest request= getRequest();
String ip = request.getHeader("x-forwarded-for");
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
}
SearchAction.java 搜索类
/**
* Class Name: SearchAction.java 搜索类
* @author JLC
* @version 1.0
*/
public class SearchAction extends BaseAction {
/**
*
*/
private static final long serialVersionUID = 3141706082945311957L;
/**
* 搜索关键字
*/
private String keyWord;
/**
* 结果集
*/
private List<ContentObject> searcheResult;
private ContentObject contentObj;
/**
* Function:
* @author
* @return
* @throws Exception
*/
@Action(
value = "index",
results = { @Result(name = SUCCESS, location = "/result.jsp") }
)
public String searcheContent() throws Exception {
searcheResult = new ArrayList<ContentObject>();
try{
Version v = Version.LUCENE_42;
//取得查询对象
IndexReader[] readers = SearchObject.getInstance().getSearcherReads();
MultiReader mReaders = new MultiReader(readers);
IndexSearcher indexSearch = new IndexSearcher(mReaders);
Analyzer analyzer =new StandardAnalyzer(v);
//创建boolean查询
BooleanQuery query = new BooleanQuery();
String[] field = { "title", "content"};
BooleanClause.Occur[] flags = new BooleanClause.Occur[2];
flags[0] = BooleanClause.Occur.SHOULD;
flags[1] = BooleanClause.Occur.SHOULD;
Query query1 = MultiFieldQueryParser.parse(v, QueryParser.escape(keyWord), field, flags, analyzer);
query.add(query1, Occur.MUST);
TopScoreDocCollector topCollector = TopScoreDocCollector.create(10000, true);
indexSearch.search(query, topCollector);
//取得查询结果
TopDocs topDocs = topCollector.topDocs();
int resultCount=topDocs.totalHits;
for(int i=0;i<resultCount;i++){
Document doc = indexSearch.doc(topDocs.scoreDocs[i].doc);
//转换Document对象为内容对象
ContentObject content = PackContentObject.convertDocToContent(doc);
//加入到结果列表 返回给前台页面获取
searcheResult.add(content);
}
}catch(Exception e){
e.printStackTrace();
}
return SUCCESS;
}
public String getKeyWord() {
return keyWord;
}
public void setKeyWord(String keyWord) {
this.keyWord = keyWord;
}
public List<ContentObject> getSearcheResult() {
return searcheResult;
}
public ContentObject getContentObj() {
return contentObj;
}
public void setContentObj(ContentObject contentObj) {
this.contentObj = contentObj;
}
}
创建index.jsp内容查询页面
请注意页面如果要使用Struts2的标签必须加入到顶部
<%@ taglib uri="/struts-tags" prefix="s" %>
下面为页面片段内容
<body>
<!-- 添加索引 -->
<s:form action="index">
<input type="text" name="keyWord" value=""><input type="submit" name="" value="搜索">
</s:form>
<a href="addcontent.jsp" >添加内容</a>
</body>
创建result.jsp结果显示页面
下面为页面片段内容
<body>
<s:iterator id="obj" value="searcheResult" >
标题:<s:property value="#obj.title" /></p>
内容:<s:property value="#obj.content" /></p>
<hr>
</s:iterator>
</body>
实际运行效果1.查询条件“搜狐”
2.结果列表
分享到:
相关推荐
- 第六节 变量的作用域与存储期 - 第七节 函数的重载 - 第八节 程序举例 - 本章小结 - 课后习题 ◇ 第六章 数组与字符串 - 课前索引 - 第一节 一维数组 - 第二节 二维数组 - 第三节 字符数组 - 第四节 ...
04 多表查询之复合查询与子查询 05 mysql之索引 第48章 01 python操作数据库pymysql 02 数据库之事务 03 mysql事务之savepoint 第49章 01 http协议之请求协议 02 http协议之响应协议 03 web框架的概念 04 做一个...
2008-04-09 人力资源管理软件发布1.5版本,做了以下改进 员工资料支持拼音查询(感谢☆缘☆) 员工资料输入身份证后自动填写籍贯、出生日期和性别(感谢☆缘☆) 员工资料支持描述信息(如工作经历等),可自定义...
View2包括读者号、读者姓名、单位、电话、书号、书名、借阅日期、应归还日期、实际还书日期和过期天数等属性,其条件是所有借书已过期的读者和借书情况(假定借阅期为60天); ② 根据上述定义的视图,在查询分析器...
数据库优化 177 8.1 索引的使用 178 8.1.1索引对单个表查询的影响 178 8.1.2索引对多个表查询的影响 180 8.1.3多列索引对查询的影响 181 8.1.4索引的作用 182 8.1.5 索引的弊端 182 8.1.6 选择...
数据库优化 177 8.1 索引的使用 178 8.1.1索引对单个表查询的影响 178 8.1.2索引对多个表查询的影响 180 8.1.3多列索引对查询的影响 181 8.1.4索引的作用 182 8.1.5 索引的弊端 182 8.1.6 选择...
内容简介 书籍 计算机书籍 c#作为微软的旗舰编程语言,深受程序员喜爱,是他们编写高效应用程序的首选语言。visual c# 2010新增了大量可圈可点的丰富特性,本书围绕着基础知识和这些新特性全面介绍了如何利用...
4.3.2 变量的存储分配(生存期) 90 4.3.3 *基于栈的函数调用的实现 93 4.3.4 C++程序的多模块结构 95 4.3.5 标识符的作用域 97 4.3.6 名空间(namespace) 103 4.4 递归函数 105 4.4.1 递归函数的定义 105 4.4.2 ...
11.1.4 第四步:定义方法将输入转化为期望事件 11.2 编译器如何实现事件 11.3 设计侦听事件的类型 11.4 显式实现事件 第12章 泛型 12.1 Framework类库中的泛型 12.2 Wintellect的Power Collections库 12.3 ...
2.1.1 数据化开发全面地和彻底地将材料标准(包括材料规范和试验标准等)的内容(条文、表格和图形)分解为试验特征的标题、要求值(表达式)、依据和属性,以及计量、采样(统计)和数据测量规则等,并包装到各个...
第二部分 C#程序设计基础.28 第四章 数 据 类 型 .28 4.1 值 类 型 .28 4.2 引 用 类 型 .33 4.3 装箱和拆箱 .39 4.4 小 结 .42 第五章 变量和常量 .44 5.1 变 量 .44 5.2 常 量 .46 5.3 小 结 .47 ...