`

SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎

阅读更多
    前两天看到了一个中国新闻网,这个网站的搜索form的action是
http://search.chinanews.com/search.do

便知道是struts1的产物,现在都用struts2了,所以给自己的任务是实现Struts2 SSH分页浏览新闻、Lucene分页高亮排序搜索新闻这个两个功能。

 

    IDE使用的MyEclipse6.5,数据库使用MySQL 5.0.37 , 另装了Navicat for MySQL , jdk版本是6.0

    工程做完的效果图如下,com.zly.indexManager中两个类,分别创建索引和搜索索引,

    com.zly.test.entity中是使用的实体类,分别是NewsType(新闻类型),NewsItem(新闻具体条目),PageControl(分页实体bean) , SearchResultBean(保存搜索结果的bean).

  

    

 

        浏览和搜索的前提是有据可查,没有数据什么都实现不了 , 我使用了Htmlparser通过抓取页面信息的形式将新闻添加进数据库 , 添加数据库数据使用了hibernate3

        使用了Annotation的方式完成数据库的映射。

         //NewsType(新闻类型)

Java代码 复制代码 收藏代码
  1. package com.zly.test.entity;   
  2.   
  3. import java.io.Serializable;   
  4.   
  5. import javax.persistence.Column;   
  6. import javax.persistence.Entity;   
  7. import javax.persistence.GeneratedValue;   
  8. import javax.persistence.GenerationType;   
  9. import javax.persistence.Id;   
  10. import javax.persistence.Table;   
  11. @Entity  
  12. @Table(name = "t_newsType")   
  13. public class NewsType implements Serializable {   
  14.   
  15.        
  16.     private static final long serialVersionUID = -5092622762025999122L;   
  17.        
  18.     private Integer id;   
  19.        
  20.     private String newsTypeName;   
  21.     @Column  
  22.     public String getNewsTypeName() {   
  23.         return newsTypeName;   
  24.     }   
  25.   
  26.     public void setNewsTypeName(String newsTypeName) {   
  27.         this.newsTypeName = newsTypeName;   
  28.     }   
  29.     @Id  
  30.     @GeneratedValue(strategy = GenerationType.AUTO)   
  31.     public Integer getId() {   
  32.         return id;   
  33.     }   
  34.   
  35.     public void setId(Integer id) {   
  36.         this.id = id;   
  37.     }   
  38.        
  39.        
  40. }  
package com.zly.test.entity;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "t_newsType")
public class NewsType implements Serializable {

	
	private static final long serialVersionUID = -5092622762025999122L;
	
	private Integer id;
	
	private String newsTypeName;
	@Column
	public String getNewsTypeName() {
		return newsTypeName;
	}

	public void setNewsTypeName(String newsTypeName) {
		this.newsTypeName = newsTypeName;
	}
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}
	
	
}

 

   //NewsItem(新闻具体条目)

  

Java代码 复制代码 收藏代码
  1. package com.zly.test.entity;   
  2.   
  3. import java.io.Serializable;   
  4. import java.util.Date;   
  5.   
  6. import javax.persistence.Column;   
  7. import javax.persistence.Entity;   
  8. import javax.persistence.GeneratedValue;   
  9. import javax.persistence.GenerationType;   
  10. import javax.persistence.Id;   
  11. import javax.persistence.JoinColumn;   
  12. import javax.persistence.ManyToOne;   
  13. import javax.persistence.Table;   
  14. import javax.persistence.Temporal;   
  15. import javax.persistence.TemporalType;   
  16. @Entity  
  17. @Table(name = "t_newsItem")   
  18. public class NewsItem implements Serializable {   
  19.   
  20.        
  21.     private static final long serialVersionUID = -7532888546907495633L;   
  22.        
  23.     private Integer id ;    
  24.        
  25.     private String newsTitle ;   
  26.        
  27.     private String newsContent;   
  28.        
  29.     private Date publishTime;   
  30.        
  31.     private String resource;   
  32.        
  33.     private NewsType type;   
  34.        
  35.     private String editor;   
  36.     @Column  
  37.     public String getEditor() {   
  38.         return editor;   
  39.     }   
  40.   
  41.     public void setEditor(String editor) {   
  42.         this.editor = editor;   
  43.     }   
  44.     @ManyToOne  
  45.     @JoinColumn(name = "t_newsType_id")   
  46.     public NewsType getType() {   
  47.         return type;   
  48.     }   
  49.   
  50.     public void setType(NewsType type) {   
  51.         this.type = type;   
  52.     }   
  53.     @Id  
  54.     @GeneratedValue(strategy = GenerationType.AUTO)   
  55.     public Integer getId() {   
  56.         return id;   
  57.     }   
  58.   
  59.     public void setId(Integer id) {   
  60.         this.id = id;   
  61.     }   
  62.     @Column  
  63.     public String getNewsTitle() {   
  64.         return newsTitle;   
  65.     }   
  66.   
  67.     public void setNewsTitle(String newsTitle) {   
  68.         this.newsTitle = newsTitle;   
  69.     }   
  70.        
  71.        
  72.     @Temporal(value = TemporalType.TIMESTAMP)   
  73.     public Date getPublishTime() {   
  74.         return publishTime;   
  75.     }   
  76.   
  77.     public void setPublishTime(Date publishTime) {   
  78.         this.publishTime = publishTime;   
  79.     }   
  80.   
  81.     public String getResource() {   
  82.         return resource;   
  83.     }   
  84.   
  85.     public void setResource(String resource) {   
  86.         this.resource = resource;   
  87.     }   
  88.   
  89.     public String getNewsContent() {   
  90.         return newsContent;   
  91.     }   
  92.   
  93.     public void setNewsContent(String newsContent) {   
  94.         this.newsContent = newsContent;   
  95.     }   
  96.        
  97.        
  98. }  
package com.zly.test.entity;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
@Table(name = "t_newsItem")
public class NewsItem implements Serializable {

	
	private static final long serialVersionUID = -7532888546907495633L;
	
	private Integer id ; 
	
	private String newsTitle ;
	
	private String newsContent;
	
	private Date publishTime;
	
	private String resource;
	
	private NewsType type;
	
	private String editor;
	@Column
	public String getEditor() {
		return editor;
	}

	public void setEditor(String editor) {
		this.editor = editor;
	}
	@ManyToOne
	@JoinColumn(name = "t_newsType_id")
	public NewsType getType() {
		return type;
	}

	public void setType(NewsType type) {
		this.type = type;
	}
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}
	@Column
	public String getNewsTitle() {
		return newsTitle;
	}

	public void setNewsTitle(String newsTitle) {
		this.newsTitle = newsTitle;
	}
	
	
	@Temporal(value = TemporalType.TIMESTAMP)
	public Date getPublishTime() {
		return publishTime;
	}

	public void setPublishTime(Date publishTime) {
		this.publishTime = publishTime;
	}

	public String getResource() {
		return resource;
	}

	public void setResource(String resource) {
		this.resource = resource;
	}

	public String getNewsContent() {
		return newsContent;
	}

	public void setNewsContent(String newsContent) {
		this.newsContent = newsContent;
	}
	
	
}

 

    添加所有新闻类型的类放在了com.zly.test.newsFetch包下 , 具体代码:

 

 

Java代码 复制代码 收藏代码
  1. package com.zly.test.newsfetch;   
  2.   
  3.   
  4. import java.text.DateFormat;   
  5. import java.text.SimpleDateFormat;   
  6. import java.util.Date;   
  7. import java.util.LinkedHashSet;   
  8. import java.util.Set;   
  9. import java.util.regex.Matcher;   
  10. import java.util.regex.Pattern;   
  11.   
  12. import org.hibernate.Session;   
  13. import org.hibernate.SessionFactory;   
  14. import org.hibernate.cfg.AnnotationConfiguration;   
  15. import org.hibernate.cfg.Configuration;   
  16. import org.htmlparser.Parser;   
  17. import org.htmlparser.Tag;   
  18. import org.htmlparser.filters.NodeClassFilter;   
  19. import org.htmlparser.tags.Div;   
  20. import org.htmlparser.tags.LinkTag;   
  21. import org.htmlparser.util.NodeList;   
  22. import org.htmlparser.visitors.NodeVisitor;   
  23.   
  24. import com.zly.test.entity.NewsItem;   
  25. import com.zly.test.entity.NewsType;   
  26.   
  27. public class GetNews {   
  28.     public static void main(String[] args) throws Exception {   
  29.         //插入数据新闻类型   
  30.         //insertAllTypes();   
  31.            
  32.         //插入所有新闻数据   
  33.            
  34.         //国内新闻   
  35.         //insertNewsItems("http://www.chinanews.com.cn/scroll-news/gn/2009/05" ,"/news.shtml" ,1);   
  36.         //国际新闻   
  37.         //insertNewsItems("http://www.chinanews.com.cn/scroll-news/gj/2009/05" ,"/news.shtml" ,2);   
  38.         //社会新闻   
  39.         //insertNewsItems("http://www.chinanews.com.cn/scroll-news/sh/2009/05" ,"/news.shtml" ,3);   
  40.         //港澳新闻   
  41.         //insertNewsItems("http://www.chinanews.com.cn/scroll-news/ga/2009/05" ,"/news.shtml" ,4);         
  42.         //台湾新闻   
  43.         //insertNewsItems("http://www.chinanews.com.cn/scroll-news/tw/2009/05" ,"/news.shtml" ,5);         
  44.         //华人新闻   
  45.         //insertNewsItems("http://www.chinanews.com.cn/scroll-news/hr/2009/05" ,"/news.shtml" ,6);         
  46.         //经济新闻   
  47.         //insertNewsItems("http://www.chinanews.com.cn/scroll-news/cj/2009/05" ,"/news.shtml" ,7);         
  48.         //文化新闻   
  49.         //insertNewsItems("http://www.chinanews.com.cn/scroll-news/wh/2009/05" ,"/news.shtml" ,8);         
  50.         //娱乐新闻   
  51.         //insertNewsItems("http://www.chinanews.com.cn/scroll-news/yl/2009/05" ,"/news.shtml" ,9);         
  52.         //体育新闻   
  53.         //insertNewsItems("http://www.chinanews.com.cn/scroll-news/ty/2009/05" ,"/news.shtml" ,10);        
  54.         //教育新闻   
  55.         //insertNewsItems("http://www.chinanews.com.cn/scroll-news/edu/2009/05" ,"/news.shtml" ,11);           
  56.         //健康新闻   
  57.         //insertNewsItems("http://www.chinanews.com.cn/scroll-news/jk/2009/05" ,"/news.shtml" ,12);        
  58.         //生活新闻   
  59.         //insertNewsItems("http://www.chinanews.com.cn/scroll-news/life/2009/05" ,"/news.shtml" ,13);          
  60.         //IT新闻   
  61.         //insertNewsItems("http://www.chinanews.com.cn/scroll-news/it/2009/05" ,"/news.shtml" ,14);        
  62.     }   
  63.     public static void insertAllTypes() {   
  64.         Configuration cfg = new AnnotationConfiguration().configure();   
  65.         SessionFactory factory = cfg.buildSessionFactory();   
  66.         Session session = factory.openSession();   
  67.         String str = new String("国内 国际 社会 港澳 台湾 华人 经济 文化 娱乐 体育 教育 健康 生活 IT");   
  68.         String[] typesStr = str.split(" ");   
  69.         NewsType[] types = new NewsType[typesStr.length];   
  70.         session.beginTransaction();   
  71.         for (int i = 0; i < typesStr.length; i++) {   
  72.             types[i] = new NewsType();   
  73.             types[i].setNewsTypeName(typesStr[i]);   
  74.             session.save(types[i]);   
  75.         }   
  76.         session.getTransaction().commit();   
  77.         session.close();   
  78.     }   
  79.        
  80.     //处理5.1 - 5.5 所有的具体类型的新闻   
  81.        
  82.     public static void insertNewsItems(String before , String after , int type) throws Exception {   
  83.         Configuration cfg = new AnnotationConfiguration().configure();   
  84.         SessionFactory factory = cfg.buildSessionFactory();   
  85.         Session session = factory.openSession();   
  86.         //得到当前新闻所属类别   
  87.         NewsType newsType = (NewsType) session.get(NewsType.class, type);   
  88.         final Set<NewsItem> set = new LinkedHashSet<NewsItem>();   
  89.         //获取5月1日-5月5日的新闻   
  90.         for (int i = 1; i <= 5; i++) {   
  91.             String src = before;   
  92.             if(i < 10) {   
  93.                 src = src + "0" + i;   
  94.             }else {   
  95.                 src = src + i ;    
  96.             }   
  97.             src = src + after;   
  98.             //使用htmlParser获取每一条新闻的超链接   
  99.             Parser parser = new Parser(src);   
  100.             parser.setEncoding("gb2312");   
  101.             NodeList divList = parser.parse(new NodeClassFilter(Div.class));   
  102.             for (int j = 0; j < divList.size(); j++) {   
  103.                 Div div = (Div) divList.elementAt(j);   
  104.                 String divClass = div.getAttribute("id");   
  105.                 //取得id为news_list的div   
  106.                 if(divClass != null && divClass.equals("news_list")) {   
  107.                     div.accept(new NodeVisitor() {   
  108.                         //遍历news_list里面的所有超链接   
  109.                         public void visitTag(Tag tag) {   
  110.                             if(tag instanceof LinkTag) {   
  111.                                 String href = ((LinkTag)tag).getLink();   
  112.                                 if(!href.startsWith("http")) {   
  113.                                     href = "http://www.chinanews.com.cn" + href;   
  114.                                 }   
  115.                                 System.out.println(href);   
  116.                                 //找到超链接,将这个超链接所在的页面进行处理,抓取新闻数据,并将其保存在Set中。   
  117.                                 try{   
  118.                                     insertOneNews(href , set);   
  119.                                 }catch(Exception e) {   
  120.                                     e.printStackTrace();   
  121.                                 }   
  122.                                    
  123.                             }   
  124.                         }   
  125.                     });   
  126.                 }   
  127.             }   
  128.         }   
  129.         //获取新闻完成后,将所有NewsItem添加到数据库   
  130.         session.beginTransaction();   
  131.         for (NewsItem newsItem : set) {   
  132.             newsItem.setType(newsType);   
  133.             session.save(newsItem);   
  134.         }   
  135.         session.getTransaction().commit();   
  136.         session.close();   
  137.     }   
  138.     //处理每一个具体的新闻超链接,得到NewsItem类   
  139.     public static void insertOneNews(String src , Set<NewsItem> set) throws Exception {   
  140.         Parser parser = new Parser(src);   
  141.         parser.setEncoding("gb2312");   
  142.         NodeList divList = parser.extractAllNodesThatMatch(new NodeClassFilter(Div.class));   
  143.         NewsItem newsItem = new NewsItem();   
  144.         String title = "";   
  145.         Date parse = null;   
  146.         String content = "";   
  147.         String editor = "";   
  148.         //遍历网页的div。将制定div里面的信息设置到NewsItem实体类中   
  149.         for (int i = 0; i < divList.size(); i++) {   
  150.             Div div = (Div) divList.elementAt(i);   
  151.             String divString = div.getAttribute("class");   
  152.             if(divString != null) {   
  153.                 //设置标题   
  154.                 if(divString.equals("left_bt")) {   
  155.                     title = div.toPlainTextString();   
  156.                 }   
  157.                 //设置发布时间   
  158.                 if(divString.equals("left_time")) {   
  159.                     String publishStr = "";   
  160.                     Pattern pattern = Pattern.compile("[\\d]{4}年[\\d]{2}月[\\d]{2}日 [\\d]{2}:[\\d]{2}");   
  161.                     Matcher matcher = pattern.matcher(div.toPlainTextString()) ;   
  162.                     if(matcher.find()) {   
  163.                         publishStr = matcher.group();   
  164.                     }   
  165.                     DateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");   
  166.                     parse = format.parse(publishStr);   
  167.                 }   
  168.                 //设置正文内容   
  169.                 if(divString.equals("left_zw")) {   
  170.                     content = div.toHtml();   
  171.                 }   
  172.                 //设置记者名称   
  173.                 if(divString.equals("left_name")) {   
  174.                     editor = div.toPlainTextString().trim();   
  175.                     editor = editor.substring(editor.indexOf(":") + 1, editor.lastIndexOf("】"));   
  176.                 }   
  177.             }   
  178.                
  179.         }   
  180.         newsItem.setEditor(editor);   
  181.         newsItem.setNewsContent(content);   
  182.         newsItem.setNewsTitle(title);   
  183.         newsItem.setPublishTime(parse);   
  184.         //设置新闻来源   
  185.         newsItem.setResource("最快新闻网");   
  186.         //将新闻添加到set中   
  187.         set.add(newsItem);   
  188.     }   
  189. }  
package com.zly.test.newsfetch;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.htmlparser.Parser;
import org.htmlparser.Tag;
import org.htmlparser.filters.NodeClassFilter;
import org.htmlparser.tags.Div;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.visitors.NodeVisitor;

import com.zly.test.entity.NewsItem;
import com.zly.test.entity.NewsType;

public class GetNews {
	public static void main(String[] args) throws Exception {
		//插入数据新闻类型
		//insertAllTypes();
		
		//插入所有新闻数据
		
		//国内新闻
		//insertNewsItems("http://www.chinanews.com.cn/scroll-news/gn/2009/05" ,"/news.shtml" ,1);
		//国际新闻
		//insertNewsItems("http://www.chinanews.com.cn/scroll-news/gj/2009/05" ,"/news.shtml" ,2);
		//社会新闻
		//insertNewsItems("http://www.chinanews.com.cn/scroll-news/sh/2009/05" ,"/news.shtml" ,3);
		//港澳新闻
		//insertNewsItems("http://www.chinanews.com.cn/scroll-news/ga/2009/05" ,"/news.shtml" ,4);		
		//台湾新闻
		//insertNewsItems("http://www.chinanews.com.cn/scroll-news/tw/2009/05" ,"/news.shtml" ,5);		
		//华人新闻
		//insertNewsItems("http://www.chinanews.com.cn/scroll-news/hr/2009/05" ,"/news.shtml" ,6);		
		//经济新闻
		//insertNewsItems("http://www.chinanews.com.cn/scroll-news/cj/2009/05" ,"/news.shtml" ,7);		
		//文化新闻
		//insertNewsItems("http://www.chinanews.com.cn/scroll-news/wh/2009/05" ,"/news.shtml" ,8);		
		//娱乐新闻
		//insertNewsItems("http://www.chinanews.com.cn/scroll-news/yl/2009/05" ,"/news.shtml" ,9);		
		//体育新闻
		//insertNewsItems("http://www.chinanews.com.cn/scroll-news/ty/2009/05" ,"/news.shtml" ,10);		
		//教育新闻
		//insertNewsItems("http://www.chinanews.com.cn/scroll-news/edu/2009/05" ,"/news.shtml" ,11);		
		//健康新闻
		//insertNewsItems("http://www.chinanews.com.cn/scroll-news/jk/2009/05" ,"/news.shtml" ,12);		
		//生活新闻
		//insertNewsItems("http://www.chinanews.com.cn/scroll-news/life/2009/05" ,"/news.shtml" ,13);		
		//IT新闻
		//insertNewsItems("http://www.chinanews.com.cn/scroll-news/it/2009/05" ,"/news.shtml" ,14);		
	}
	public static void insertAllTypes() {
		Configuration cfg = new AnnotationConfiguration().configure();
		SessionFactory factory = cfg.buildSessionFactory();
		Session session = factory.openSession();
		String str = new String("国内 国际 社会 港澳 台湾 华人 经济 文化 娱乐 体育 教育 健康 生活 IT");
		String[] typesStr = str.split(" ");
		NewsType[] types = new NewsType[typesStr.length];
		session.beginTransaction();
		for (int i = 0; i < typesStr.length; i++) {
			types[i] = new NewsType();
			types[i].setNewsTypeName(typesStr[i]);
			session.save(types[i]);
		}
		session.getTransaction().commit();
		session.close();
	}
	
	//处理5.1 - 5.5 所有的具体类型的新闻
	
	public static void insertNewsItems(String before , String after , int type) throws Exception {
		Configuration cfg = new AnnotationConfiguration().configure();
		SessionFactory factory = cfg.buildSessionFactory();
		Session session = factory.openSession();
		//得到当前新闻所属类别
		NewsType newsType = (NewsType) session.get(NewsType.class, type);
		final Set<NewsItem> set = new LinkedHashSet<NewsItem>();
		//获取5月1日-5月5日的新闻
		for (int i = 1; i <= 5; i++) {
			String src = before;
			if(i < 10) {
				src = src + "0" + i;
			}else {
				src = src + i ; 
			}
			src = src + after;
			//使用htmlParser获取每一条新闻的超链接
			Parser parser = new Parser(src);
			parser.setEncoding("gb2312");
			NodeList divList = parser.parse(new NodeClassFilter(Div.class));
			for (int j = 0; j < divList.size(); j++) {
				Div div = (Div) divList.elementAt(j);
				String divClass = div.getAttribute("id");
				//取得id为news_list的div
				if(divClass != null && divClass.equals("news_list")) {
					div.accept(new NodeVisitor() {
						//遍历news_list里面的所有超链接
						public void visitTag(Tag tag) {
							if(tag instanceof LinkTag) {
								String href = ((LinkTag)tag).getLink();
								if(!href.startsWith("http")) {
									href = "http://www.chinanews.com.cn" + href;
								}
								System.out.println(href);
								//找到超链接,将这个超链接所在的页面进行处理,抓取新闻数据,并将其保存在Set中。
								try{
									insertOneNews(href , set);
								}catch(Exception e) {
									e.printStackTrace();
								}
								
							}
						}
					});
				}
			}
		}
		//获取新闻完成后,将所有NewsItem添加到数据库
		session.beginTransaction();
		for (NewsItem newsItem : set) {
			newsItem.setType(newsType);
			session.save(newsItem);
		}
		session.getTransaction().commit();
		session.close();
	}
	//处理每一个具体的新闻超链接,得到NewsItem类
	public static void insertOneNews(String src , Set<NewsItem> set) throws Exception {
		Parser parser = new Parser(src);
		parser.setEncoding("gb2312");
		NodeList divList = parser.extractAllNodesThatMatch(new NodeClassFilter(Div.class));
		NewsItem newsItem = new NewsItem();
		String title = "";
		Date parse = null;
		String content = "";
		String editor = "";
		//遍历网页的div。将制定div里面的信息设置到NewsItem实体类中
		for (int i = 0; i < divList.size(); i++) {
			Div div = (Div) divList.elementAt(i);
			String divString = div.getAttribute("class");
			if(divString != null) {
				//设置标题
				if(divString.equals("left_bt")) {
					title = div.toPlainTextString();
				}
				//设置发布时间
				if(divString.equals("left_time")) {
					String publishStr = "";
					Pattern pattern = Pattern.compile("[\\d]{4}年[\\d]{2}月[\\d]{2}日 [\\d]{2}:[\\d]{2}");
					Matcher matcher = pattern.matcher(div.toPlainTextString()) ;
					if(matcher.find()) {
						publishStr = matcher.group();
					}
					DateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
					parse = format.parse(publishStr);
				}
				//设置正文内容
				if(divString.equals("left_zw")) {
					content = div.toHtml();
				}
				//设置记者名称
				if(divString.equals("left_name")) {
					editor = div.toPlainTextString().trim();
					editor = editor.substring(editor.indexOf(":") + 1, editor.lastIndexOf("】"));
				}
			}
			
		}
		newsItem.setEditor(editor);
		newsItem.setNewsContent(content);
		newsItem.setNewsTitle(title);
		newsItem.setPublishTime(parse);
		//设置新闻来源
		newsItem.setResource("最快新闻网");
		//将新闻添加到set中
		set.add(newsItem);
	}
}

 

 通过上面的代码完成了所有的数据添加工作。

 

下面根据ssh的流程分别定制dao , manager , action

 

com.zly.test.dao包中是所有操作dao的抽象类和接口

 

我们直接看这些接口的实现

 

//NewsItemDaoHibernate  新闻实体类dao

 

 

 

Java代码 复制代码 收藏代码
  1. @SuppressWarnings("unchecked")   
  2.     //根据分页得到具体页的内容   
  3.     public List<com.zly.test.entity.NewsItem> getNewsItemByFirstResultAndMaxResult(   
  4.             final int firstResult, final int maxResult , final int type) {   
  5.         return (List<NewsItem>) this.getHibernateTemplate().execute(new HibernateCallback() {   
  6.             public Object doInHibernate(Session session)   
  7.                     throws HibernateException, SQLException {   
  8.                 return session.createQuery(" from NewsItem where type.id = '" + type + "'").setFirstResult(firstResult).setMaxResults(maxResult).list();   
  9.             }   
  10.         });   
  11.     }   
  12.     //得到数据总条数 , 以便计算总页数   
  13.     public Long getItemCount(final int id) {   
  14.         return (Long) this.getHibernateTemplate().execute(new HibernateCallback(){   
  15.             public Object doInHibernate(Session session)   
  16.                     throws HibernateException, SQLException {   
  17.                 return session.createQuery("select count(*) from NewsItem where type.id = '" + id + "'").uniqueResult();   
  18.             }   
  19.         });   
  20.     }   
  21.     //显示新闻数据页面用到, 查询具体新闻属于哪个新闻类别   
  22.     public int getNewsType(final int id) {   
  23.         return (Integer) this.getHibernateTemplate().execute(new HibernateCallback() {   
  24.             public Object doInHibernate(Session session)   
  25.                     throws HibernateException, SQLException {   
  26.                 NewsItem newsItem = get(id);   
  27.                 NewsType type = newsItem.getType();   
  28.                 return type.getId();   
  29.             }   
  30.         });   
  31.     }  
@SuppressWarnings("unchecked")
	//根据分页得到具体页的内容
	public List<com.zly.test.entity.NewsItem> getNewsItemByFirstResultAndMaxResult(
			final int firstResult, final int maxResult , final int type) {
		return (List<NewsItem>) this.getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				return session.createQuery(" from NewsItem where type.id = '" + type + "'").setFirstResult(firstResult).setMaxResults(maxResult).list();
			}
		});
	}
	//得到数据总条数 , 以便计算总页数
	public Long getItemCount(final int id) {
		return (Long) this.getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				return session.createQuery("select count(*) from NewsItem where type.id = '" + id + "'").uniqueResult();
			}
		});
	}
	//显示新闻数据页面用到, 查询具体新闻属于哪个新闻类别
	public int getNewsType(final int id) {
		return (Integer) this.getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				NewsItem newsItem = get(id);
				NewsType type = newsItem.getType();
				return type.getId();
			}
		});
	}

 

   只用到了一个   NewsManagerImpl 

 

 

Java代码 复制代码 收藏代码
  1. package com.zly.test.service.impl;   
  2.   
  3. import java.util.List;   
  4.   
  5. import com.zly.test.dao.NewsItemDao;   
  6. import com.zly.test.entity.NewsItem;   
  7. import com.zly.test.service.NewsManager;   
  8.   
  9. public class NewsManagerImpl implements NewsManager {   
  10.     //新闻条目dao   
  11.     private NewsItemDao newsItemDao;   
  12.        
  13.     public NewsItemDao getNewsItemDao() {   
  14.         return newsItemDao;   
  15.     }   
  16.   
  17.     public void setNewsItemDao(NewsItemDao newsItemDao) {   
  18.         this.newsItemDao = newsItemDao;   
  19.     }   
  20.     //根据分页得到内容   
  21.     public List<NewsItem> getNewsItemByFirstResultAndMaxResult(int firstResult,   
  22.             int maxResult , int type) {   
  23.         return newsItemDao.getNewsItemByFirstResultAndMaxResult(firstResult, maxResult ,  type);   
  24.     }   
  25.     //得到总记录数,以便计算总页数   
  26.     public Long getItemCounts(int id) {   
  27.         return newsItemDao.getItemCount(id);   
  28.     }   
  29.     //通过id得到具体新闻   
  30.     public NewsItem getNewsById(int id) {   
  31.         return newsItemDao.get(id);   
  32.     }   
  33.     //通过id得到所属新闻类别的id   
  34.     public int getNewsType(int id) {   
  35.         return newsItemDao.getNewsType(id);    
  36.     }   
  37.   
  38. }  
package com.zly.test.service.impl;

import java.util.List;

import com.zly.test.dao.NewsItemDao;
import com.zly.test.entity.NewsItem;
import com.zly.test.service.NewsManager;

public class NewsManagerImpl implements NewsManager {
	//新闻条目dao
	private NewsItemDao newsItemDao;
	
	public NewsItemDao getNewsItemDao() {
		return newsItemDao;
	}

	public void setNewsItemDao(NewsItemDao newsItemDao) {
		this.newsItemDao = newsItemDao;
	}
	//根据分页得到内容
	public List<NewsItem> getNewsItemByFirstResultAndMaxResult(int firstResult,
			int maxResult , int type) {
		return newsItemDao.getNewsItemByFirstResultAndMaxResult(firstResult, maxResult ,  type);
	}
	//得到总记录数,以便计算总页数
	public Long getItemCounts(int id) {
		return newsItemDao.getItemCount(id);
	}
	//通过id得到具体新闻
	public NewsItem getNewsById(int id) {
		return newsItemDao.get(id);
	}
	//通过id得到所属新闻类别的id
	public int getNewsType(int id) {
		return newsItemDao.getNewsType(id); 
	}

}

 

 

在applicationContext-action.xml中配置action类别

Java代码 复制代码 收藏代码
  1. <bean class="com.zly.test.action.NewsAction" id="newsAction" scope="request">   
  2.         <property name="newsManager" ref="newsManager"></property>   
  3.         <property name="map" ref="map"></property>   
  4.         <property name="map1" ref="map1"></property>   
  5. </bean>  
		<bean class="com.zly.test.action.NewsAction" id="newsAction" scope="request">
	 		<property name="newsManager" ref="newsManager"></property>
	 		<property name="map" ref="map"></property>
	 		<property name="map1" ref="map1"></property>
		</bean>

 

其中定一个两个map , 因为主页的查看分类新闻的url是采用的这种形式<a href="newsAction.action?category=china" target="_blank">国内</a>   名字为map的Map中保存信息如下

 

Java代码 复制代码 收藏代码
  1. <bean id="map" class="java.util.HashMap">   
  2.    <constructor-arg>   
  3.     <map>   
  4.     <entry key="china" value="1###国内新闻" />   
  5.         <entry key="world" value="2###国际新闻" />   
  6.         <entry key="society" value="3###社会新闻" />   
  7.         <entry key="compatriot" value="4###港台新闻" />   
  8.         <entry key="taiwan" value="5###台湾新闻" />   
  9.         <entry key="huaren" value="6###华人新闻" />   
  10.         <entry key="economic" value="7###经济新闻" />   
  11.         <entry key="wenhua" value="8###文化新闻" />   
  12.         <entry key="entertainment" value="9###娱乐新闻" />   
  13.         <entry key="sports" value="10###体育新闻" />   
  14.         <entry key="jiaoyu" value="11###教育新闻" />   
  15.         <entry key="jiankang" value="12###健康新闻" />   
  16.         <entry key="life" value="13###生活新闻" />   
  17.         <entry key="keji" value="14###科技新闻" />   
  18.     </map>   
  19.    </constructor-arg>   
  20. </bean>  
	<bean id="map" class="java.util.HashMap">
	   <constructor-arg>
	    <map>
	   	<entry key="china" value="1###国内新闻" />
	      	<entry key="world" value="2###国际新闻" />
	      	<entry key="society" value="3###社会新闻" />
	      	<entry key="compatriot" value="4###港台新闻" />
	      	<entry key="taiwan" value="5###台湾新闻" />
	      	<entry key="huaren" value="6###华人新闻" />
	      	<entry key="economic" value="7###经济新闻" />
	      	<entry key="wenhua" value="8###文化新闻" />
	      	<entry key="entertainment" value="9###娱乐新闻" />
	      	<entry key="sports" value="10###体育新闻" />
	      	<entry key="jiaoyu" value="11###教育新闻" />
	      	<entry key="jiankang" value="12###健康新闻" />
	      	<entry key="life" value="13###生活新闻" />
	      	<entry key="keji" value="14###科技新闻" />
	    </map>
	   </constructor-arg>
	</bean>

 

  key是?category后面的值 , value是两部分 , 被###分割开 , 前面的数值是所属新闻类别的id值, 后面的文字是其类别的文字。将其保存在map中,避免不停地查询数据库。

 

  分页类PageControl的代码如下:

 

Java代码 复制代码 收藏代码
  1. package com.zly.test.entity;   
  2.   
  3. import java.util.List;   
  4.   
  5.   
  6.   
  7.   
  8. public class PageControl {   
  9.        
  10.     private int curPage ; //当前是第几页    
  11.        
  12.     private int maxPage ; //一共有多少页    
  13.        
  14.     private Long maxRowCount ; //一共有多少行    
  15.        
  16.     private int rowsPerPage = 8 ; //每页有多少行    
  17.   
  18.     private List<?> data;//每页的User   
  19.        
  20.     public int getCurPage() {   
  21.         return curPage;   
  22.     }   
  23.   
  24.     public void setCurPage(int curPage) {   
  25.         this.curPage = curPage;   
  26.     }   
  27.   
  28.     public int getMaxPage() {   
  29.         return maxPage;   
  30.     }   
  31.   
  32.     public void setMaxPage(int maxPage) {   
  33.         this.maxPage = maxPage;   
  34.     }   
  35.   
  36.     public List<?> getUserList() {   
  37.         return data;   
  38.     }   
  39.   
  40.     public void setUserList(List<?> data) {   
  41.         this.data = data;   
  42.     }   
  43.   
  44.     public Long getMaxRowCount() {   
  45.         return maxRowCount;   
  46.     }   
  47.   
  48.     public void setMaxRowCount(Long amaxRowCountxRowCount) {   
  49.         this.maxRowCount = amaxRowCountxRowCount;   
  50.     }   
  51.   
  52.     public int getRowsPerPage() {   
  53.         return rowsPerPage;   
  54.     }   
  55.   
  56.     public void setRowsPerPage(int rowsPerPage) {   
  57.         this.rowsPerPage = rowsPerPage;   
  58.     }   
  59.        
  60.     public void countMaxPage() {   //根据总行数计算总页数     
  61.         if (this.maxRowCount % this.rowsPerPage ==0){   
  62.            this.maxPage = (int) (this.maxRowCount/this.rowsPerPage);   
  63.         }else{   
  64.            this.maxPage = (int) (this.maxRowCount/this.rowsPerPage + 1);           
  65.         }   
  66.     }   
  67.        
  68. }  
package com.zly.test.entity;

import java.util.List;




public class PageControl {
	
	private int curPage ; //当前是第几页 
	
	private int maxPage ; //一共有多少页 
	
	private Long maxRowCount ; //一共有多少行 
	
	private int rowsPerPage = 8 ; //每页有多少行 

	private List<?> data;//每页的User
	
	public int getCurPage() {
		return curPage;
	}

	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}

	public int getMaxPage() {
		return maxPage;
	}

	public void setMaxPage(int maxPage) {
		this.maxPage = maxPage;
	}

	public List<?> getUserList() {
		return data;
	}

	public void setUserList(List<?> data) {
		this.data = data;
	}

	public Long getMaxRowCount() {
		return maxRowCount;
	}

	public void setMaxRowCount(Long amaxRowCountxRowCount) {
		this.maxRowCount = amaxRowCountxRowCount;
	}

	public int getRowsPerPage() {
		return rowsPerPage;
	}

	public void setRowsPerPage(int rowsPerPage) {
		this.rowsPerPage = rowsPerPage;
	}
	
	public void countMaxPage() {   //根据总行数计算总页数  
	    if (this.maxRowCount % this.rowsPerPage ==0){
	       this.maxPage = (int) (this.maxRowCount/this.rowsPerPage);
	    }else{
	       this.maxPage = (int) (this.maxRowCount/this.rowsPerPage + 1);        
	    }
	}
	
}

 

  被许多页所包含的page.jsp代码如下:

 

 

Java代码 复制代码 收藏代码
  1. <%@ page language="java"  contentType="text/html;charset=utf-8"  
  2.     pageEncoding="utf-8"%>   
  3. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>   
  4. <html>   
  5. <head>   
  6.     <script language="javascript">   
  7.         function jumping() {   
  8.             document.pageForm.submit();   
  9.             return true;   
  10.         }   
  11.         function gotoPage(pageNum) {   
  12.             document.pageForm.jumpPage.value = pageNum ;   
  13.             document.pageForm.submit();   
  14.             return true;   
  15.         }   
  16.     </script>   
  17. </head>   
  18. <body>   
  19.     <c:if test="${pageControl.maxPage != 1}">   
  20.         <form method="post" action="pageAction.action" name="pageForm">   
  21.   
  22.         <table>   
  23.             <tr>   
  24.                 <td>   
  25.                     每页${pageControl.rowsPerPage}行   
  26.                 </td>   
  27.                 <td>   
  28.                     共${pageControl.maxRowCount }行   
  29.                 </td>   
  30.                 <td>   
  31.                     第${pageControl.curPage }页   
  32.                 </td>   
  33.                 <td>   
  34.                     共${pageControl.maxPage }页   
  35.                 </td>   
  36.                 <td>   
  37.                     <c:choose>   
  38.                         <c:when test="${pageControl.curPage == 1}">   
  39.                             首页 上一页   
  40.                         </c:when>   
  41.                            
  42.                         <c:otherwise>   
  43.                             <A HREF="javascript:gotoPage(1)">首页</A>   
  44.                             <A HREF="javascript:gotoPage(${pageControl.curPage - 1} )">上一页</A>     
  45.                         </c:otherwise>   
  46.                     </c:choose>   
  47.                        
  48.                        
  49.                     <c:choose>   
  50.                         <c:when test="${pageControl.curPage == pageControl.maxPage}">   
  51.                             下一页 尾页   
  52.                         </c:when>   
  53.                            
  54.                         <c:otherwise>   
  55.                             <A HREF="javascript:gotoPage(${pageControl.curPage + 1})">下一页</A>   
  56.                             <A HREF="javascript:gotoPage(${pageControl.maxPage })">尾页</A>   
  57.                         </c:otherwise>   
  58.                     </c:choose>   
  59.                     转到第   
  60.                     <SELECT name="jumpPage" onchange="jumping()">   
  61.                                 <c:forEach var="i" begin="1" end="${pageControl.maxPage}" step="1">   
  62.                                     <c:choose>   
  63.                                         <c:when test="${i == pageControl.curPage}">   
  64.                                             <OPTION selected value="${i}">${i }</OPTION>   
  65.                                         </c:when>   
  66.                                         <c:otherwise>   
  67.                                             <OPTION value="${i}">${i}</OPTION>                     
  68.                                         </c:otherwise>   
  69.                                     </c:choose>   
  70.                                 </c:forEach>   
  71.                     </SELECT>   
  72.                     页   
  73.                 </td>   
  74.             </tr>   
  75.         </table>   
  76.   
  77.     </form>   
  78.     </c:if>   
  79.        
  80. </body>   
  81. </html>  
<%@ page language="java"  contentType="text/html;charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
	<script language="javascript">
		function jumping() {
			document.pageForm.submit();
			return true;
		}
		function gotoPage(pageNum) {
			document.pageForm.jumpPage.value = pageNum ;
			document.pageForm.submit();
			return true;
		}
	</script>
</head>
<body>
	<c:if test="${pageControl.maxPage != 1}">
		<form method="post" action="pageAction.action" name="pageForm">

		<table>
			<tr>
				<td>
					每页${pageControl.rowsPerPage}行
				</td>
				<td>
					共${pageControl.maxRowCount }行
				</td>
				<td>
					第${pageControl.curPage }页
				</td>
				<td>
					共${pageControl.maxPage }页
				</td>
				<td>
					<c:choose>
						<c:when test="${pageControl.curPage == 1}">
							首页 上一页
						</c:when>
						
						<c:otherwise>
							<A HREF="javascript:gotoPage(1)">首页</A>
							<A HREF="javascript:gotoPage(${pageControl.curPage - 1} )">上一页</A>	
						</c:otherwise>
					</c:choose>
					
					
					<c:choose>
						<c:when test="${pageControl.curPage == pageControl.maxPage}">
							下一页 尾页
						</c:when>
						
						<c:otherwise>
							<A HREF="javascript:gotoPage(${pageControl.curPage + 1})">下一页</A>
							<A HREF="javascript:gotoPage(${pageControl.maxPage })">尾页</A>
						</c:otherwise>
					</c:choose>
					转到第
					<SELECT name="jumpPage" onchange="jumping()">
								<c:forEach var="i" begin="1" end="${pageControl.maxPage}" step="1">
									<c:choose>
										<c:when test="${i == pageControl.curPage}">
											<OPTION selected value="${i}">${i }</OPTION>
										</c:when>
										<c:otherwise>
											<OPTION value="${i}">${i}</OPTION>					
										</c:otherwise>
									</c:choose>
								</c:forEach>
					</SELECT>
					页
				</td>
			</tr>
		</table>

	</form>
	</c:if>
	
</body>
</html>

 

   下面是struts.xml中关于页面展示新闻的配置

 

  

Java代码 复制代码 收藏代码
  1. <action name="newsAction" class="newsAction" >   
  2.     <result type="redirect">pageAction.action</result>                     
  3. </action>   
  4.   
  5. <action name="pageAction" class="newsAction" method="pageAction">   
  6.     <result>/result.jsp</result>   
  7. </action>   
  8.   
  9. <action name="detailAction" class="newsAction" method="showDetail">   
  10.     <result>/detail.jsp</result>   
  11. </action>  
			<action name="newsAction" class="newsAction" >
				<result type="redirect">pageAction.action</result>					
			</action>
			
			<action name="pageAction" class="newsAction" method="pageAction">
				<result>/result.jsp</result>
			</action>
			
			<action name="detailAction" class="newsAction" method="showDetail">
				<result>/detail.jsp</result>
			</action>

  

   NewsAction代码如下:

  

Java代码 复制代码 收藏代码
  1. package com.zly.test.action;   
  2.   
  3. import java.util.List;   
  4. import java.util.Map;   
  5.   
  6. import com.zly.test.entity.NewsItem;   
  7. import com.zly.test.entity.PageControl;   
  8. import com.zly.test.service.NewsManager;   
  9.   
  10. public class NewsAction extends BaseAction {   
  11.   
  12.     private static final long serialVersionUID = 7780804627621048756L;   
  13.   
  14.     //对应分页jsp中的跳转到第几页   
  15.     private String jumpPage;   
  16.     //对应url?category的值   
  17.     private String category;   
  18.     //对新闻数据进行处理   
  19.     private NewsManager newsManager;   
  20.     //保存<entry key="china" value="1###国内新闻" />这样的信息   
  21.     private Map<String , String> map;   
  22.     //保存<entry key="1" value="china###国内新闻" />这样的信息   
  23.     private Map<String , String> map1;   
  24.        
  25.     public Map<String, String> getMap1() {   
  26.         return map1;   
  27.     }   
  28.   
  29.   
  30.     public void setMap1(Map<String, String> map1) {   
  31.         this.map1 = map1;   
  32.     }   
  33.   
  34.   
  35.     public Map<String, String> getMap() {   
  36.         return map;   
  37.     }   
  38.   
  39.   
  40.     public void setMap(Map<String, String> map) {   
  41.         this.map = map;   
  42.     }   
  43.   
  44.   
  45.     public String getJumpPage() {   
  46.         return jumpPage;   
  47.     }   
  48.   
  49.   
  50.     public void setJumpPage(String jumpPage) {   
  51.         this.jumpPage = jumpPage;   
  52.     }   
  53.   
  54.   
  55.        
  56.     public NewsManager getNewsManager() {   
  57.         return newsManager;   
  58.     }   
  59.   
  60.   
  61.     public void setNewsManager(NewsManager newsManager) {   
  62.         this.newsManager = newsManager;   
  63.     }   
  64.   
  65.     //处理newsAction.action?category=xxx   
  66.     public String execute() {   
  67.         //得到相应的    1###国内新闻   
  68.         String typeIdInfo = map.get(category);   
  69.         //这里得到的是新闻类别id   
  70.         Integer typeId = Integer.parseInt(typeIdInfo.split("###")[0]);   
  71.         //这里得到的是新闻类别字符串   
  72.         String typeString = typeIdInfo.split("###")[1];   
  73.         //将上面三个变量保存在session中,在jsp页面中显示   
  74.         this.getSession().setAttribute("category", category);   
  75.         this.getSession().setAttribute("typeString" , typeString);   
  76.         this.getSession().setAttribute("id", typeId);   
  77.         //跳转到pageAction中,处理分页   
  78.         return SUCCESS;   
  79.     }   
  80.        
  81.     public String pageAction() {   
  82.         //取出新闻类别id   
  83.         Integer id = (Integer) this.getSession().getAttribute("id");   
  84.         //查看是不是第一次来分页,如果是,当前页设置成第一个   
  85.         if(jumpPage == null) {   
  86.             jumpPage = "1";   
  87.         }   
  88.         //从request范围内取出PageControl , 如为空,则第一次分页,创建pageControl对象   
  89.         PageControl pageControl  = (PageControl) this.getRequest().getAttribute("pageControl");   
  90.         if(pageControl == null) {   
  91.             pageControl = new PageControl();   
  92.             //第一次的时候得到相应新闻类别所对应的记录的总数   
  93.             pageControl.setMaxRowCount(newsManager.getItemCounts(id));   
  94.             //计算一共多少页   
  95.             pageControl.countMaxPage();   
  96.         }   
  97.         //设置jumpPage的值为当前页   
  98.         pageControl.setCurPage(Integer.parseInt(jumpPage));   
  99.         //计算出hibernate中firstResult的值   
  100.         int firstResult = (pageControl.getCurPage() - 1) * pageControl.getRowsPerPage() + 1;   
  101.         //设置hibernate中maxResult的值   
  102.         int maxResult = pageControl.getRowsPerPage();   
  103.         //利用hibernate得到当前页的数据,并保存在pageControl分页实体类中   
  104.         List<NewsItem> userList = newsManager.getNewsItemByFirstResultAndMaxResult(firstResult, maxResult , id);   
  105.         pageControl.setData(userList);   
  106.         //将分页实体类保存在request范围内。   
  107.         this.getRequest().setAttribute("pageControl", pageControl);   
  108.         //将页面视图跳转到result.jsp   
  109.         return SUCCESS;   
  110.     }   
  111.        
  112.     public String showDetail() {   
  113.         //得到url中的?id=xxx   
  114.         String newsId = this.getRequest().getParameter("id");   
  115.         int id = Integer.parseInt(newsId);   
  116.         //使用hibernate得到具体id的新闻记录   
  117.         NewsItem item = newsManager.getNewsById(id);   
  118.         //得到id记录所对应的新闻类型的值   map1这种形式<entry key="1" value="china###国内新闻" />   
  119.         int typeId = newsManager.getNewsType(id);   
  120.         //得到china,添加到jsp页面的<a href="newsAction?category=">里面   
  121.         String category = map1.get("" + typeId).split("###")[0];   
  122.         //得到国内新闻,显示在jsp页面的多个位置   
  123.         String typeString = map1.get("" + typeId).split("###")[1];   
  124.         //将以上多个数据添加到request范围内,以便显示。   
  125.         this.getRequest().setAttribute("news", item);   
  126.         this.getRequest().setAttribute("category", category);   
  127.         this.getRequest().setAttribute("typeString", typeString);   
  128.         return SUCCESS;   
  129.     }   
  130.   
  131.   
  132.     public String getCategory() {   
  133.         return category;   
  134.     }   
  135.   
  136.   
  137.     public void setCategory(String category) {   
  138.         this.category = category;   
  139.     }   
  140.        
  141. }  
package com.zly.test.action;

import java.util.List;
import java.util.Map;

import com.zly.test.entity.NewsItem;
import com.zly.test.entity.PageControl;
import com.zly.test.service.NewsManager;

public class NewsAction extends BaseAction {

	private static final long serialVersionUID = 7780804627621048756L;

	//对应分页jsp中的跳转到第几页
	private String jumpPage;
	//对应url?category的值
	private String category;
	//对新闻数据进行处理
	private NewsManager newsManager;
	//保存<entry key="china" value="1###国内新闻" />这样的信息
	private Map<String , String> map;
	//保存<entry key="1" value="china###国内新闻" />这样的信息
	private Map<String , String> map1;
	
	public Map<String, String> getMap1() {
		return map1;
	}


	public void setMap1(Map<String, String> map1) {
		this.map1 = map1;
	}


	public Map<String, String> getMap() {
		return map;
	}


	public void setMap(Map<String, String> map) {
		this.map = map;
	}


	public String getJumpPage() {
		return jumpPage;
	}


	public void setJumpPage(String jumpPage) {
		this.jumpPage = jumpPage;
	}


	
	public NewsManager getNewsManager() {
		return newsManager;
	}


	public void setNewsManager(NewsManager newsManager) {
		this.newsManager = newsManager;
	}

	//处理newsAction.action?category=xxx
	public String execute() {
		//得到相应的    1###国内新闻
		String typeIdInfo = map.get(category);
		//这里得到的是新闻类别id
		Integer typeId = Integer.parseInt(typeIdInfo.split("###")[0]);
		//这里得到的是新闻类别字符串
		String typeString = typeIdInfo.split("###")[1];
		//将上面三个变量保存在session中,在jsp页面中显示
		this.getSession().setAttribute("category", category);
		this.getSession().setAttribute("typeString" , typeString);
		this.getSession().setAttribute("id", typeId);
		//跳转到pageAction中,处理分页
		return SUCCESS;
	}
	
	public String pageAction() {
		//取出新闻类别id
		Integer id = (Integer) this.getSession().getAttribute("id");
		//查看是不是第一次来分页,如果是,当前页设置成第一个
		if(jumpPage == null) {
			jumpPage = "1";
		}
		//从request范围内取出PageControl , 如为空,则第一次分页,创建pageControl对象
		PageControl pageControl  = (PageControl) this.getRequest().getAttribute("pageControl");
		if(pageControl == null) {
			pageControl = new PageControl();
			//第一次的时候得到相应新闻类别所对应的记录的总数
			pageControl.setMaxRowCount(newsManager.getItemCounts(id));
			//计算一共多少页
			pageControl.countMaxPage();
		}
		//设置jumpPage的值为当前页
		pageControl.setCurPage(Integer.parseInt(jumpPage));
		//计算出hibernate中firstResult的值
		int firstResult = (pageControl.getCurPage() - 1) * pageControl.getRowsPerPage() + 1;
		//设置hibernate中maxResult的值
		int maxResult = pageControl.getRowsPerPage();
		//利用hibernate得到当前页的数据,并保存在pageControl分页实体类中
		List<NewsItem> userList = newsManager.getNewsItemByFirstResultAndMaxResult(firstResult, maxResult , id);
		pageControl.setData(userList);
		//将分页实体类保存在request范围内。
		this.getRequest().setAttribute("pageControl", pageControl);
		//将页面视图跳转到result.jsp
		return SUCCESS;
	}
	
	public String showDetail() {
		//得到url中的?id=xxx
		String newsId = this.getRequest().getParameter("id");
		int id = Integer.parseInt(newsId);
		//使用hibernate得到具体id的新闻记录
		NewsItem item = newsManager.getNewsById(id);
		//得到id记录所对应的新闻类型的值   map1这种形式<entry key="1" value="china###国内新闻" />
		int typeId = newsManager.getNewsType(id);
		//得到china,添加到jsp页面的<a href="newsAction?category=">里面
		String category = map1.get("" + typeId).split("###")[0];
		//得到国内新闻,显示在jsp页面的多个位置
		String typeString = map1.get("" + typeId).split("###")[1];
		//将以上多个数据添加到request范围内,以便显示。
		this.getRequest().setAttribute("news", item);
		this.getRequest().setAttribute("category", category);
		this.getRequest().setAttribute("typeString", typeString);
		return SUCCESS;
	}


	public String getCategory() {
		return category;
	}


	public void setCategory(String category) {
		this.category = category;
	}
	
}

 

 

首页页面index.jsp,里面有几个分类超链接和搜索对话框

 

Java代码 复制代码 收藏代码
  1. <%@ page language="java"  pageEncoding="utf-8"%>   
  2.   
  3. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">   
  4. <html>   
  5.      
  6.   <head>   
  7.     <title>最快新闻网</title>   
  8.     <link href="css/main.css" rel="stylesheet" type="text/css" />   
  9.   </head>   
  10.      
  11.   <body>   
  12.          <div id="mainlink">   
  13.                 <span><a href="index.jsp">最快新闻网</a></span><br /><br />   
  14.                 <a href="newsAction.action?category=china" target="_blank">国内</a> | <a href="newsAction.action?category=world" target="_blank">国际</a> | <a href="newsAction.action?category=society" target="_blank">社会</a> | <a href="newsAction.action?category=compatriot" target="_blank">港澳</a> | <a href="newsAction.action?category=taiwan" target="_blank">台湾</a> | <a href="newsAction.action?category=huaren" target="_blank">华人</a> | <a href="newsAction.action?category=economic" target="_blank">经济</a> | <a href="newsAction.action?category=wenhua" target="_blank">文化</a> | <a href="newsAction.action?category=entertainment" target="_blank">娱乐</a> | <a href="newsAction.action?category=sports" target="_blank">体育</a> | <a href="newsAction.action?category=jiaoyu" target="_blank">教育</a> | <a href="newsAction.action?category=jiankang" target="_blank">健康</a> | <a href="newsAction.action?category=life" target="_blank">生活</a> | <a href="newsAction.action?category=keji" target="_blank">IT</a><br />   
  15.                 <form action="searchAction.action" method="post" target="_blank">   
  16.                     本站搜索:   
  17.                     <input type="text" name="searchParam" style="height:20px"/>   
  18.                     <select name="searchWhich">   
  19.                         <option value="title"/>标题   
  20.                         <option value="content"/>内容   
  21.                     </select>   
  22.                     <input type="submit" value="搜索" style="height:23px"/>   
  23.                 </form>               
  24.          </div>   
  25.   </body>   
  26. </html>  
<%@ page language="java"  pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  
  <head>
    <title>最快新闻网</title>
    <link href="css/main.css" rel="stylesheet" type="text/css" />
  </head>
  
  <body>
   		 <div id="mainlink">
				<span><a href="index.jsp">最快新闻网</a></span><br /><br />
				<a href="newsAction.action?category=china" target="_blank">国内</a> | <a href="newsAction.action?category=world" target="_blank">国际</a> | <a href="newsAction.action?category=society" target="_blank">社会</a> | <a href="newsAction.action?category=compatriot" target="_blank">港澳</a> | <a href="newsAction.action?category=taiwan" target="_blank">台湾</a> | <a href="newsAction.action?category=huaren" target="_blank">华人</a> | <a href="newsAction.action?category=economic" target="_blank">经济</a> | <a href="newsAction.action?category=wenhua" target="_blank">文化</a> | <a href="newsAction.action?category=entertainment" target="_blank">娱乐</a> | <a href="newsAction.action?category=sports" target="_blank">体育</a> | <a href="newsAction.action?category=jiaoyu" target="_blank">教育</a> | <a href="newsAction.action?category=jiankang" target="_blank">健康</a> | <a href="newsAction.action?category=life" target="_blank">生活</a> | <a href="newsAction.action?category=keji" target="_blank">IT</a><br />
				<form action="searchAction.action" method="post" target="_blank">
					本站搜索:
					<input type="text" name="searchParam" style="height:20px"/>
					<select name="searchWhich">
						<option value="title"/>标题
						<option value="content"/>内容
					</select>
					<input type="submit" value="搜索" style="height:23px"/>
				</form>   		 
   		 </div>
  </body>
</html>

 

 

 

  其表现形式如下:

新闻分页展示页面result.jsp代码如下:

Html代码 复制代码 收藏代码
  1. <%@ page language="java" contentType="text/html;charset=utf-8"  
  2.     pageEncoding="utf-8"%>  
  3. <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>  
  4. <%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>  
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  6. <html>  
  7. <head>  
  8. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
  9. <title>${typeString} -- 最快新闻网</title>  
  10. </head>  
  11. <body>  
  12.     <jsp:include page="index.jsp"></jsp:include>  
  13.     <div id="content">  
  14.         <font color="red" size="5">${typeString}</font>  
  15.         <br /><br /><br /><br />  
  16.         <div id="newsListTitle" style="float:left;">  
  17.             <c:forEach items="${pageControl.data}" var="news">  
  18.                 <div style="margin-top: 20px;">  
  19.                     <span>  
  20.                         <a href="detailAction.action?id=${news.id }">${news.newsTitle }</a>  
  21.                     </span>  
  22.                     <br />  
  23.                 </div>  
  24.             </c:forEach>         
  25.         </div>  
  26.            
  27.         <div id="newsListTime">  
  28.             <c:forEach items="${pageControl.data}" var="news">  
  29.                 <div style="margin-top: 20px;">  
  30.                     <span>  
  31.                         <fmt:formatDate value="${news.publishTime}" pattern="MM-dd HH:mm"/>  
  32.                     </span>  
  33.                     <br />  
  34.                 </div>  
  35.             </c:forEach>  
  36.         </div>  
  37.         <br /><br /><br /><br />  
  38.         <%@ include file="page.jsp" %>  
  39.     </div>  
  40. </body>  
  41. </html>  
<%@ page language="java" contentType="text/html;charset=utf-8"
    pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>${typeString} -- 最快新闻网</title>
</head>
<body>
	<jsp:include page="index.jsp"></jsp:include>
	<div id="content">
		<font color="red" size="5">${typeString}</font>
		<br /><br /><br /><br />
		<div id="newsListTitle" style="float:left;">
			<c:forEach items="${pageControl.data}" var="news">
				<div style="margin-top: 20px;">
					<span>
						<a href="detailAction.action?id=${news.id }">${news.newsTitle }</a>
					</span>
					<br />
				</div>
			</c:forEach>		
		</div>
		
		<div id="newsListTime">
			<c:forEach items="${pageControl.data}" var="news">
				<div style="margin-top: 20px;">
					<span>
						<fmt:formatDate value="${news.publishTime}" pattern="MM-dd HH:mm"/>
					</span>
					<br />
				</div>
			</c:forEach>
		</div>
		<br /><br /><br /><br />
		<%@ include file="page.jsp" %>
	</div>
</body>
</html>

  显示效果如下:

 

 

 

其中点击具体超链接的效果图如下:

 

 

 

任务1 到此完成,新闻显示工作结束。下面是搜索引擎部分。

 

搜索的工具类放置在com.zly.indexManager包下面

 

说明,本程序使用了庖丁解牛中文分词,用户使用时需要中文字典,我的字典放在了c:\dic下面,使用庖丁还需要配置环境变量PAODING_DIC_HOME , 其值为c:\dic , (就是你的字典文件所在的目录)

 

代码如下:

 

创建索引类IndexCreateUtil

 

Java代码 复制代码 收藏代码
  1. package com.zly.indexManager;   
  2.   
  3. import java.io.File;   
  4. import java.text.DateFormat;   
  5. import java.text.SimpleDateFormat;   
  6. import java.util.List;   
  7.   
  8. import net.paoding.analysis.analyzer.PaodingAnalyzer;   
  9.   
  10. import org.apache.lucene.analysis.Analyzer;   
  11. import org.apache.lucene.document.Document;   
  12. import org.apache.lucene.document.Field;   
  13. import org.apache.lucene.index.IndexWriter;   
  14. import org.hibernate.SessionFactory;   
  15. import org.hibernate.cfg.AnnotationConfiguration;   
  16. import org.hibernate.cfg.Configuration;   
  17. import org.hibernate.Session;   
  18. import org.htmlparser.Parser;   
  19.   
  20. import com.zly.test.entity.NewsItem;   
  21.   
  22. public class IndexCreateUtil {   
  23.        
  24.     @SuppressWarnings("unchecked")   
  25.     public void createIndexForNews() throws Exception {   
  26.         //存放索引的文件夹   
  27.         File indexFile = new File("c:/index/news");   
  28.         //使用了庖丁解牛分词器   
  29.         Analyzer analyzer = new PaodingAnalyzer();   
  30.         //使用索引文件夹,庖丁解牛分词器创建IndexWriter   
  31.         IndexWriter indexWriter = new IndexWriter(indexFile , analyzer , true);   
  32.         //从数据库中读取出所有的新闻记录以便进行索引的创建   
  33.         Configuration cfg = new AnnotationConfiguration().configure();   
  34.         SessionFactory factory = cfg.buildSessionFactory();   
  35.         Session session = factory.openSession();   
  36.         List<NewsItem> list = session.createQuery(" from NewsItem").list();   
  37.         DateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");   
  38.         //对所有的新闻实体进行索引创建   
  39.         for (NewsItem newsItem : list) {   
  40.             //建立一个lucene文档   
  41.             Document doc = new Document();   
  42.             //得到新闻标题   
  43.             String newsTitle = newsItem.getNewsTitle();   
  44.             //得到新闻内容   
  45.             String newsContent = newsItem.getNewsContent();   
  46.             //得到新闻事件   
  47.             String publishDate = format.format(newsItem.getPublishTime());   
  48.             //得到新闻主键id   
  49.             String id = newsItem.getId() + "";   
  50.             //将新闻标题加入文档,因为要搜索和高亮,所以index是tokennized,TermVector是WITH_POSITIONS_OFFSETS   
  51.             doc.add(new Field("title" , newsTitle , Field.Store.YES , Field.Index.TOKENIZED , Field.TermVector.WITH_POSITIONS_OFFSETS));   
  52.             //利用htmlparser得到新闻内容html的纯文本   
  53.             Parser parser = new Parser();   
  54.             parser.setInputHTML(newsContent);   
  55.             String strings = parser.parse(null).elementAt(0).toPlainTextString().trim();   
  56.             //添加新闻内容至文档,与标题相似   
  57.             doc.add(new Field("content" , strings , Field.Store.COMPRESS , Field.Index.TOKENIZED , Field.TermVector.WITH_POSITIONS_OFFSETS));   
  58.             //添加时间至文档,因为要按照此字段降序排列排序,所以tokenzied,不用高亮所以TermVector是no就行了   
  59.             doc.add(new Field("date" , publishDate , Field.Store.YES , Field.Index.TOKENIZED , Field.TermVector.NO));   
  60.             //添加主键至文档,不分词,不高亮。   
  61.             doc.add(new Field("id" , id , Field.Store.YES , Field.Index.NO , Field.TermVector.NO));   
  62.             indexWriter.addDocument(doc);   
  63.         }   
  64.         //创建索引   
  65.         indexWriter.optimize();   
  66.         indexWriter.close();   
  67.         //关闭session   
  68.         session.close();   
  69.     }   
  70.     public static void main(String[] args) throws Exception {   
  71.         IndexCreateUtil util  = new IndexCreateUtil();   
  72.         util.createIndexForNews();   
  73.     }   
  74. }  
package com.zly.indexManager;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;

import net.paoding.analysis.analyzer.PaodingAnalyzer;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.hibernate.Session;
import org.htmlparser.Parser;

import com.zly.test.entity.NewsItem;

public class IndexCreateUtil {
	
	@SuppressWarnings("unchecked")
	public void createIndexForNews() throws Exception {
		//存放索引的文件夹
		File indexFile = new File("c:/index/news");
		//使用了庖丁解牛分词器
		Analyzer analyzer = new PaodingAnalyzer();
		//使用索引文件夹,庖丁解牛分词器创建IndexWriter
		IndexWriter indexWriter = new IndexWriter(indexFile , analyzer , true);
		//从数据库中读取出所有的新闻记录以便进行索引的创建
		Configuration cfg = new AnnotationConfiguration().configure();
		SessionFactory factory = cfg.buildSessionFactory();
		Session session = factory.openSession();
		List<NewsItem> list = session.createQuery(" from NewsItem").list();
		DateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
		//对所有的新闻实体进行索引创建
		for (NewsItem newsItem : list) {
			//建立一个lucene文档
			Document doc = new Document();
			//得到新闻标题
			String newsTitle = newsItem.getNewsTitle();
			//得到新闻内容
			String newsContent = newsItem.getNewsContent();
			//得到新闻事件
			String publishDate = format.format(newsItem.getPublishTime());
			//得到新闻主键id
			String id = newsItem.getId() + "";
			//将新闻标题加入文档,因为要搜索和高亮,所以index是tokennized,TermVector是WITH_POSITIONS_OFFSETS
			doc.add(new Field("title" , newsTitle , Field.Store.YES , Field.Index.TOKENIZED , Field.TermVector.WITH_POSITIONS_OFFSETS));
			//利用htmlparser得到新闻内容html的纯文本
			Parser parser = new Parser();
			parser.setInputHTML(newsContent);
			String strings = parser.parse(null).elementAt(0).toPlainTextString().trim();
			//添加新闻内容至文档,与标题相似
			doc.add(new Field("content" , strings , Field.Store.COMPRESS , Field.Index.TOKENIZED , Field.TermVector.WITH_POSITIONS_OFFSETS));
			//添加时间至文档,因为要按照此字段降序排列排序,所以tokenzied,不用高亮所以TermVector是no就行了
			doc.add(new Field("date" , publishDate , Field.Store.YES , Field.Index.TOKENIZED , Field.TermVector.NO));
			//添加主键至文档,不分词,不高亮。
			doc.add(new Field("id" , id , Field.Store.YES , Field.Index.NO , Field.TermVector.NO));
			indexWriter.addDocument(doc);
		}
		//创建索引
		indexWriter.optimize();
		indexWriter.close();
		//关闭session
		session.close();
	}
	public static void main(String[] args) throws Exception {
		IndexCreateUtil util  = new IndexCreateUtil();
		util.createIndexForNews();
	}
}

 

对索引进行搜索的代码如下:

 

  

Java代码 复制代码 收藏代码
  1. package com.zly.indexManager;   
  2.   
  3. import java.io.File;   
  4. import java.util.ArrayList;   
  5. import java.util.List;   
  6.   
  7. import net.paoding.analysis.analyzer.PaodingAnalyzer;   
  8.   
  9. import org.apache.lucene.analysis.Analyzer;   
  10. import org.apache.lucene.document.Document;   
  11. import org.apache.lucene.index.IndexReader;   
  12. import org.apache.lucene.queryParser.QueryParser;   
  13. import org.apache.lucene.search.Hits;   
  14. import org.apache.lucene.search.IndexSearcher;   
  15. import org.apache.lucene.search.Query;   
  16. import org.apache.lucene.search.Sort;   
  17. import org.apache.lucene.search.highlight.Highlighter;   
  18. import org.apache.lucene.search.highlight.QueryScorer;   
  19. import org.apache.lucene.search.highlight.SimpleFragmenter;   
  20. import org.apache.lucene.search.highlight.SimpleHTMLFormatter;   
  21.   
  22. import com.zly.test.entity.SearchResultBean;   
  23.   
  24. public class IndexSearchUtil {   
  25.        
  26.     public List<SearchResultBean> getSearchResult(String searchWhich , String searchParam , int firstResult , int maxResult) throws Exception{   
  27.         //索引所在文件夹   
  28.         File indexFile = new File("c:/index/news");   
  29.         //读取索引的indexReader   
  30.         IndexReader reader = IndexReader.open(indexFile);   
  31.         //庖丁解牛分词器   
  32.         Analyzer analyzer = new PaodingAnalyzer();   
  33.         //指定对content还是title进行查询   
  34.         QueryParser parser = new QueryParser(searchWhich , analyzer);   
  35.         //创建indexSearcher   
  36.         IndexSearcher searcher  = new IndexSearcher(reader);   
  37.         //对用户的输入进行查询   
  38.         Query query = parser.parse(searchParam);   
  39.         //根据date字段进行排序,得到查询结果   
  40.         Hits hits = searcher.search(query , new Sort("date" , true));   
  41.         //创建list,将结果保存其中,以便在jsp页面中进行显示   
  42.         List<SearchResultBean> list = new ArrayList<SearchResultBean>();   
  43.         //模拟hibernate的serFirstResult和setMaxResult以便返回指定条目的结果   
  44.         for (int i = firstResult - 1; i < firstResult + maxResult - 1; i++) {   
  45.             Document doc = hits.doc(i);   
  46.             //取得该条索引文档   
  47.             SearchResultBean srb = new SearchResultBean();   
  48.             //从中取出标题   
  49.             String title = doc.get("title");   
  50.             //从中取出内容   
  51.             String content = doc.get("content");   
  52.             //从中取出主键id   
  53.             String id = doc.get("id");   
  54.             //从中取出发布时间   
  55.             String date = doc.get("date");   
  56.             //高亮htmlFormatter对象   
  57.             SimpleHTMLFormatter sHtmlF = new SimpleHTMLFormatter("<b><font color='red'>""</font></b>");   
  58.             //高亮对象   
  59.             Highlighter highlighter = new Highlighter(sHtmlF,new QueryScorer(query));   
  60.             //设置高亮附近的字数   
  61.             highlighter.setTextFragmenter(new SimpleFragmenter(100));   
  62.             //如果查询的是标题,进行处理   
  63.             if(searchWhich.equals("title")) {   
  64.                 String bestFragment = highlighter.getBestFragment(analyzer,searchWhich,title);   
  65.                 //获得高亮后的标题内容   
  66.                 srb.setTitle(bestFragment);   
  67.                 //如果内容不足150个字,全部设置   
  68.                 if(content.length() < 150) {   
  69.                     srb.setContent(content);   
  70.                 }else {   
  71.                     //如果内容多于150个字,只取出前面150个字   
  72.                     srb.setContent(content.substring(0 , 150));   
  73.                 }   
  74.             } else {   
  75.                 //如果查询的是内容字段   
  76.                 String bestFragment = highlighter.getBestFragment(analyzer,searchWhich,content);   
  77.                 //取得高亮内容并设置   
  78.                 srb.setContent(bestFragment);   
  79.                 //设置标题,全部设置   
  80.                 srb.setTitle(title);   
  81.             }   
  82.             //设置日期   
  83.             srb.setDate(date);   
  84.             //设置主键   
  85.             srb.setId(id);   
  86.             //添加到list中,以便在jsp页面上显示   
  87.             list.add(srb);   
  88.         }   
  89.         return list;   
  90.     }   
  91.     //取得符合搜索条件的所有记录总数,以便分页 , 与上面方法类似   
  92.     public int getResultCount(String searchWhich , String searchParam) throws Exception {   
  93.         File indexFile = new File("c:/index/news");   
  94.         IndexReader reader = IndexReader.open(indexFile);   
  95.         Analyzer analyzer = new PaodingAnalyzer();   
  96.         QueryParser parser = new QueryParser(searchWhich , analyzer);   
  97.         IndexSearcher searcher  = new IndexSearcher(reader);   
  98.         Query query = parser.parse(searchParam);   
  99.         Hits hits = searcher.search(query);   
  100.         return hits.length();   
  101.     }   
  102. }  
package com.zly.indexManager;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import net.paoding.analysis.analyzer.PaodingAnalyzer;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.highlight.Highlighter;
import org.apache.lucene.search.highlight.QueryScorer;
import org.apache.lucene.search.highlight.SimpleFragmenter;
import org.apache.lucene.search.highlight.SimpleHTMLFormatter;

import com.zly.test.entity.SearchResultBean;

public class IndexSearchUtil {
	
	public List<SearchResultBean> getSearchResult(String searchWhich , String searchParam , int firstResult , int maxResult) throws Exception{
		//索引所在文件夹
		File indexFile = new File("c:/index/news");
		//读取索引的indexReader
		IndexReader reader = IndexReader.open(indexFile);
		//庖丁解牛分词器
		Analyzer analyzer = new PaodingAnalyzer();
		//指定对content还是title进行查询
		QueryParser parser = new QueryParser(searchWhich , analyzer);
		//创建indexSearcher
		IndexSearcher searcher  = new IndexSearcher(reader);
		//对用户的输入进行查询
		Query query = parser.parse(searchParam);
		//根据date字段进行排序,得到查询结果
		Hits hits = searcher.search(query , new Sort("date" , true));
		//创建list,将结果保存其中,以便在jsp页面中进行显示
		List<SearchResultBean> list = new ArrayList<SearchResultBean>();
		//模拟hibernate的serFirstResult和setMaxResult以便返回指定条目的结果
		for (int i = firstResult - 1; i < firstResult + maxResult - 1; i++) {
			Document doc = hits.doc(i);
			//取得该条索引文档
			SearchResultBean srb = new SearchResultBean();
			//从中取出标题
			String title = doc.get("title");
			//从中取出内容
			String content = doc.get("content");
			//从中取出主键id
			String id = doc.get("id");
			//从中取出发布时间
			String date = doc.get("date");
			//高亮htmlFormatter对象
			SimpleHTMLFormatter sHtmlF = new SimpleHTMLFormatter("<b><font color='red'>", "</font></b>");
			//高亮对象
			Highlighter highlighter = new Highlighter(sHtmlF,new QueryScorer(query));
			//设置高亮附近的字数
			highlighter.setTextFragmenter(new SimpleFragmenter(100));
			//如果查询的是标题,进行处理
			if(searchWhich.equals("title")) {
				String bestFragment = highlighter.getBestFragment(analyzer,searchWhich,title);
				//获得高亮后的标题内容
				srb.setTitle(bestFragment);
				//如果内容不足150个字,全部设置
				if(content.length() < 150) {
					srb.setContent(content);
				}else {
					//如果内容多于150个字,只取出前面150个字
					srb.setContent(content.substring(0 , 150));
				}
			} else {
				//如果查询的是内容字段
				String bestFragment = highlighter.getBestFragment(analyzer,searchWhich,content);
				//取得高亮内容并设置
				srb.setContent(bestFragment);
				//设置标题,全部设置
				srb.setTitle(title);
			}
			//设置日期
			srb.setDate(date);
			//设置主键
			srb.setId(id);
			//添加到list中,以便在jsp页面上显示
			list.add(srb);
		}
		return list;
	}
	//取得符合搜索条件的所有记录总数,以便分页 , 与上面方法类似
	public int getResultCount(String searchWhich , String searchParam) throws Exception {
		File indexFile = new File("c:/index/news");
		IndexReader reader = IndexReader.open(indexFile);
		Analyzer analyzer = new PaodingAnalyzer();
		QueryParser parser = new QueryParser(searchWhich , analyzer);
		IndexSearcher searcher  = new IndexSearcher(reader);
		Query query = parser.parse(searchParam);
		Hits hits = searcher.search(query);
		return hits.length();
	}
}

 

 

分页action代码如下:

Java代码 复制代码 收藏代码
  1. package com.zly.test.action;   
  2.   
  3. import java.util.List;   
  4.   
  5. import com.zly.indexManager.IndexSearchUtil;   
  6. import com.zly.test.entity.PageControl;   
  7. import com.zly.test.entity.SearchResultBean;   
  8.   
  9. public class SearchAction extends BaseAction {   
  10.   
  11.        
  12.     private static final long serialVersionUID = -2387037924517370511L;   
  13.     //查询索引实体类   
  14.     private IndexSearchUtil indexSearcher;   
  15.     //对应搜索字段是标题还是内容   
  16.     private String searchWhich;   
  17.     //对应用户输入的搜索内容   
  18.     private String searchParam;   
  19.     //对应分页跳转到的页面   
  20.     private String jumpPage;   
  21.        
  22.     public String getJumpPage() {   
  23.         return jumpPage;   
  24.     }   
  25.   
  26.     public void setJumpPage(String jumpPage) {   
  27.         this.jumpPage = jumpPage;   
  28.     }   
  29.   
  30.     public String getSearchWhich() {   
  31.         return searchWhich;   
  32.     }   
  33.   
  34.     public void setSearchWhich(String searchWhich) {   
  35.         this.searchWhich = searchWhich;   
  36.     }   
  37.   
  38.     public String getSearchParam() {   
  39.         return searchParam;   
  40.     }   
  41.   
  42.     public void setSearchParam(String searchParam) {   
  43.         this.searchParam = searchParam;   
  44.     }   
  45.   
  46.     public String search() throws Exception {   
  47.         //如果为空,说明第一次进入分页   
  48.         if(jumpPage == null) {   
  49.             jumpPage = "1";   
  50.         }   
  51.         //从request范围内取得pageControl对象   
  52.         PageControl pageControl  = (PageControl) this.getRequest().getAttribute("pageControl");   
  53.         //如果为空,则是第一次分页,创建分页对象,并且设置总的记录条数,以便设置最大页数    
  54.         if(pageControl == null) {   
  55.             pageControl = new PageControl();   
  56.             pageControl.setMaxRowCount((long)indexSearcher.getResultCount(searchWhich, searchParam));   
  57.             pageControl.countMaxPage();   
  58.         }   
  59.         //设置当前页   
  60.         pageControl.setCurPage(Integer.parseInt(jumpPage));   
  61.         //计算firstResult   
  62.         int firstResult = (pageControl.getCurPage() - 1) * pageControl.getRowsPerPage() + 1;   
  63.         //计算从当前条数算还有多少条记录   
  64.         long left = pageControl.getMaxRowCount() - firstResult;   
  65.         int maxResult = -1;   
  66.         //如果剩余的记录数不如每页显示数,就设置maxResult为剩余条数   
  67.         if(left < pageControl.getRowsPerPage()) {   
  68.             maxResult = Integer.valueOf(left + "");   
  69.         //如果剩余记录数大于每页显示页数,就设置maxResult为每页条数   
  70.         }else {   
  71.             maxResult = pageControl.getRowsPerPage();    
  72.         }   
  73.         //取得查询结果集   
  74.         List<SearchResultBean> userList = indexSearcher.getSearchResult(searchWhich, searchParam, firstResult, maxResult);   
  75.         //设置为pageControl   
  76.         pageControl.setData(userList);   
  77.         //将pageControl设置到request范围,以便在jsp现实结果   
  78.         this.getRequest().setAttribute("pageControl", pageControl);   
  79.         //将searchWhich和searchParam设置到request范围,以便添加到分页jsp的form里面的hidden表单域,以便下次分页时,能够将值提交过来   
  80.         this.getRequest().setAttribute("searchWhich", searchWhich);   
  81.         this.getRequest().setAttribute("searchParam", searchParam);   
  82.         //跳转到分页视图   
  83.         return SUCCESS;   
  84.            
  85.     }   
  86.   
  87.     public IndexSearchUtil getIndexSearcher() {   
  88.         return indexSearcher;   
  89.     }   
  90.   
  91.     public void setIndexSearcher(IndexSearchUtil indexSearcher) {   
  92.         this.indexSearcher = indexSearcher;   
  93.     }   
  94.        
  95. }  
package com.zly.test.action;

import java.util.List;

import com.zly.indexManager.IndexSearchUtil;
import com.zly.test.entity.PageControl;
import com.zly.test.entity.SearchResultBean;

public class SearchAction extends BaseAction {

	
	private static final long serialVersionUID = -2387037924517370511L;
	//查询索引实体类
	private IndexSearchUtil indexSearcher;
	//对应搜索字段是标题还是内容
	private String searchWhich;
	//对应用户输入的搜索内容
	private String searchParam;
	//对应分页跳转到的页面
	private String jumpPage;
	
	public String getJumpPage() {
		return jumpPage;
	}

	public void setJumpPage(String jumpPage) {
		this.jumpPage = jumpPage;
	}

	public String getSearchWhich() {
		return searchWhich;
	}

	public void setSearchWhich(String searchWhich) {
		this.searchWhich = searchWhich;
	}

	public String getSearchParam() {
		return searchParam;
	}

	public void setSearchParam(String searchParam) {
		this.searchParam = searchParam;
	}

	public String search() throws Exception {
		//如果为空,说明第一次进入分页
		if(jumpPage == null) {
			jumpPage = "1";
		}
		//从request范围内取得pageControl对象
		PageControl pageControl  = (PageControl) this.getRequest().getAttribute("pageControl");
		//如果为空,则是第一次分页,创建分页对象,并且设置总的记录条数,以便设置最大页数 
		if(pageControl == null) {
			pageControl = new PageControl();
			pageControl.setMaxRowCount((long)indexSearcher.getResultCount(searchWhich, searchParam));
			pageControl.countMaxPage();
		}
		//设置当前页
		pageControl.setCurPage(Integer.parseInt(jumpPage));
		//计算firstResult
		int firstResult = (pageControl.getCurPage() - 1) * pageControl.getRowsPerPage() + 1;
		//计算从当前条数算还有多少条记录
		long left = pageControl.getMaxRowCount() - firstResult;
		int maxResult = -1;
		//如果剩余的记录数不如每页显示数,就设置maxResult为剩余条数
		if(left < pageControl.getRowsPerPage()) {
			maxResult = Integer.valueOf(left + "");
		//如果剩余记录数大于每页显示页数,就设置maxResult为每页条数
		}else {
			maxResult = pageControl.getRowsPerPage(); 
		}
		//取得查询结果集
		List<SearchResultBean> userList = indexSearcher.getSearchResult(searchWhich, searchParam, firstResult, maxResult);
		//设置为pageControl
		pageControl.setData(userList);
		//将pageControl设置到request范围,以便在jsp现实结果
		this.getRequest().setAttribute("pageControl", pageControl);
		//将searchWhich和searchParam设置到request范围,以便添加到分页jsp的form里面的hidden表单域,以便下次分页时,能够将值提交过来
		this.getRequest().setAttribute("searchWhich", searchWhich);
		this.getRequest().setAttribute("searchParam", searchParam);
		//跳转到分页视图
		return SUCCESS;
		
	}

	public IndexSearchUtil getIndexSearcher() {
		return indexSearcher;
	}

	public void setIndexSearcher(IndexSearchUtil indexSearcher) {
		this.indexSearcher = indexSearcher;
	}
	
}

 

 

搜索的action在struts.xml中设置如下:

 

Xml代码 复制代码 收藏代码
  1. <action name="searchAction" class="searchAction" method="search">  
  2.     <result>/searchResult.jsp</result>  
  3. </action>  
			<action name="searchAction" class="searchAction" method="search">
				<result>/searchResult.jsp</result>
			</action>

 

//searchResult.jsp代码如下:

 

Html代码 复制代码 收藏代码
  1. <%@ page language="java" contentType="text/html;charset=utf-8"  
  2.     pageEncoding="utf-8"%>  
  3. <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>  
  4. <%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>  
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  6. <html>  
  7. <head>  
  8. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
  9. <title>${searchParam} 的搜查结果 -- 最快新闻网</title>  
  10. </head>  
  11. <body>  
  12.     <jsp:include page="index.jsp"></jsp:include>  
  13.     <div id="content">  
  14.            
  15.         <div id="searchResults" >  
  16.             <c:forEach items="${pageControl.data}" var="result">  
  17.                 <div style="margin-top: 20px;">  
  18.                     <span>  
  19.                         <a href="detailAction.action?id=${result.id }">${result.title}</a><br />  
  20.                         ${result.content }   
  21.                         <font color="green">http://localhost:8080/NewsWithSearch/detailAction.action?id=${result.id } ${result.date }</font>  
  22.                     </span>  
  23.                     <br />  
  24.                 </div>  
  25.             </c:forEach>         
  26.         </div>  
  27.            
  28.         <br /><br /><br /><br />  
  29.            
  30.         <%@ include file="searchPage.jsp" %>  
  31.     </div>  
  32. </body>  
  33. </html>  
<%@ page language="java" contentType="text/html;charset=utf-8"
    pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>${searchParam} 的搜查结果 -- 最快新闻网</title>
</head>
<body>
	<jsp:include page="index.jsp"></jsp:include>
	<div id="content">
		
		<div id="searchResults" >
			<c:forEach items="${pageControl.data}" var="result">
				<div style="margin-top: 20px;">
					<span>
						<a href="detailAction.action?id=${result.id }">${result.title}</a><br />
						${result.content }
						<font color="green">http://localhost:8080/NewsWithSearch/detailAction.action?id=${result.id } ${result.date }</font>
					</span>
					<br />
				</div>
			</c:forEach>		
		</div>
		
		<br /><br /><br /><br />
		
		<%@ include file="searchPage.jsp" %>
	</div>
</body>
</html>

    其运行结果如图所示(按标题搜索):

 

 

 

 

 

按内容搜索的运行结果如下:

 

 

 

至此,本小项目的所有功能完成,虽然没有多少难度,也不是什么高科技, 俺还是在google和javaeye上查了不少资料,总算是做完了,贴出来,与大家分享,也给新手学习提供资料。

 

所有的资源我都添加到了附件中,学过ssh的同学应该能够成功部署项目并运行。

 

其中NewsWithSearch.rar是工程文件夹,包含了所有的代码文件和jar包,加压完直接引到MyEclipse里就行,data.rar是所有的sql语句,插入到MySQL之前应先建立数据库mynews  ,     dic.rar是庖丁解牛用到的字典文件,

解压成一个文件夹,并配置环境变量PAODING_DIC_HOME,其值就是你把它解压成的文件夹(例如c:\dic),最后如果你不想创建索引的话,可以把news.rar解压成一个文件夹,拷贝到c:\index\news下面。

 

 

原文地址:http://shuaigg-babysky.iteye.com/blog/414477

分享到:
评论
1 楼 luoyulin 2016-01-26  
     不错  赞一个  刚好在学习这块

相关推荐

    SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--dic

    SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part3 SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part2 SSH + Lucene + 分页 + 排序 + 高亮 ...

    SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--news.part2

    SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part3 SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part2 SSH + Lucene + 分页 + 排序 + 高亮 ...

    SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part1

    SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part3 SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part2 SSH + Lucene + 分页 + 排序 + 高亮 ...

    SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part2

    SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part3 SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part2 SSH + Lucene + 分页 + 排序 + 高亮 ...

    SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part3

    SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part3 SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part2 SSH + Lucene + 分页 + 排序 + 高亮 ...

    SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--news.part1

    SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part3 SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part2 SSH + Lucene + 分页 + 排序 + 高亮 ...

    SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--data

    SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎

    JAVA上百实例源码以及开源项目源代码

    Calendar万年历 1个目标文件 EJB 模拟银行ATM流程及操作源代码 6个目标文件,EJB来模拟银行ATM机的流程及操作:获取系统属性,初始化JNDI,取得Home对象的引用,创建EJB对象,并将当前的计数器初始化,调用每一个...

    JAVA上百实例源码以及开源项目

    一个简单的CS模式的聊天软件,用socket实现,比较简单。 凯撒加密解密程序 1个目标文件 1、程序结构化,用函数分别实现 2、对文件的加密,解密输出到文件 利用随机函数抽取幸运数字 简单 EJB的真实世界模型(源代码...

    java开源包1

    AutoTips基于搜索引擎Apache Lucene实现。AutoTips提供统一UI。 WAP浏览器 j2wap j2wap 是一个基于Java的WAP浏览器,目前处于BETA测试阶段。它支持WAP 1.2规范,除了WTLS 和WBMP。 Java注册表操作类 jared jared是...

    java开源包11

    AutoTips基于搜索引擎Apache Lucene实现。AutoTips提供统一UI。 WAP浏览器 j2wap j2wap 是一个基于Java的WAP浏览器,目前处于BETA测试阶段。它支持WAP 1.2规范,除了WTLS 和WBMP。 Java注册表操作类 jared jared是...

    java开源包2

    AutoTips基于搜索引擎Apache Lucene实现。AutoTips提供统一UI。 WAP浏览器 j2wap j2wap 是一个基于Java的WAP浏览器,目前处于BETA测试阶段。它支持WAP 1.2规范,除了WTLS 和WBMP。 Java注册表操作类 jared jared是...

    java开源包3

    AutoTips基于搜索引擎Apache Lucene实现。AutoTips提供统一UI。 WAP浏览器 j2wap j2wap 是一个基于Java的WAP浏览器,目前处于BETA测试阶段。它支持WAP 1.2规范,除了WTLS 和WBMP。 Java注册表操作类 jared jared是...

    java开源包6

    AutoTips基于搜索引擎Apache Lucene实现。AutoTips提供统一UI。 WAP浏览器 j2wap j2wap 是一个基于Java的WAP浏览器,目前处于BETA测试阶段。它支持WAP 1.2规范,除了WTLS 和WBMP。 Java注册表操作类 jared jared是...

    java开源包5

    AutoTips基于搜索引擎Apache Lucene实现。AutoTips提供统一UI。 WAP浏览器 j2wap j2wap 是一个基于Java的WAP浏览器,目前处于BETA测试阶段。它支持WAP 1.2规范,除了WTLS 和WBMP。 Java注册表操作类 jared jared是...

    java开源包10

    AutoTips基于搜索引擎Apache Lucene实现。AutoTips提供统一UI。 WAP浏览器 j2wap j2wap 是一个基于Java的WAP浏览器,目前处于BETA测试阶段。它支持WAP 1.2规范,除了WTLS 和WBMP。 Java注册表操作类 jared jared是...

    java开源包4

    AutoTips基于搜索引擎Apache Lucene实现。AutoTips提供统一UI。 WAP浏览器 j2wap j2wap 是一个基于Java的WAP浏览器,目前处于BETA测试阶段。它支持WAP 1.2规范,除了WTLS 和WBMP。 Java注册表操作类 jared jared是...

    java开源包8

    AutoTips基于搜索引擎Apache Lucene实现。AutoTips提供统一UI。 WAP浏览器 j2wap j2wap 是一个基于Java的WAP浏览器,目前处于BETA测试阶段。它支持WAP 1.2规范,除了WTLS 和WBMP。 Java注册表操作类 jared jared是...

    java开源包7

    AutoTips基于搜索引擎Apache Lucene实现。AutoTips提供统一UI。 WAP浏览器 j2wap j2wap 是一个基于Java的WAP浏览器,目前处于BETA测试阶段。它支持WAP 1.2规范,除了WTLS 和WBMP。 Java注册表操作类 jared jared是...

Global site tag (gtag.js) - Google Analytics