最近比较闲,学习了struts2,和ibatis,所以确定利用学习到的知识开发一个网站,看到百度知道,问问,比较火,结构也比较简单,就开发了一个问答类型的网站.
网站采用 spring + struts + ibatis 搜索采用compass DB mysql
网站测试地址:
http://panxiuyan34.jspcn.net/ 正式域名:www.123why.com
(怎么样比较好记吧)
昨天抓取了百度的30000条数据,没有违法吧!
希望大家给我提点建议,和改进的地方.
分享一个分页代码(基于标签jstl),希望对大家有用,类似淘宝,javaeye的形式有页块
package com.ask.page;
import java.io.Serializable;
import java.util.List;
/**
* 封装分页信息
*
*/
public class Pagination<T> implements Serializable {
private static final long serialVersionUID = -4263336128482001626L;
private int total; // 总记录数
private int pageIndex; // 当前页, 从1开始计数
private int pageSize; // 页大小
private List<T> items; // 页数据
private int startRow; // 起始行, 从1开始计数
private int endRow; // 结束行, 从1开始计数
private int pageCount;// 总页数
public Pagination() {
}
public Pagination(Pagination<T> page) {
this.pageIndex = page.getPageIndex();
this.pageSize = page.pageSize;
this.startRow = pageSize * (pageIndex - 1) + 1;
this.endRow = this.startRow + pageSize - 1;
}
public Pagination(int pageIndex, int pageSize) {
this.pageIndex = pageIndex <= 0 ? 1 : pageIndex;
this.pageSize = pageSize;
this.startRow = pageSize * (this.pageIndex - 1) + 1;
this.endRow = this.startRow + this.pageSize - 1;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
if (total < pageSize) {
this.pageCount = 1;
} else {
if (total % pageSize == 0) {
this.pageCount = total / pageSize;
} else {
this.pageCount = total / pageSize + 1;
}
}
if (pageIndex > pageCount) {
this.pageIndex = this.pageCount;
startRow = pageSize * (pageIndex - 1) + 1;
endRow = startRow + pageSize - 1;
}
}
public int getPageIndex() {
return pageIndex;
}
public void setPageIndex(int pageIndex) {
this.pageIndex = pageIndex;
}
public List<T> getItems() {
return items;
}
public void setItems(List<T> items) {
this.items = items;
}
public static long getSerialVersionUID() {
return serialVersionUID;
}
/**
* 起始行位置, 从1开始计数
*
* @return
*/
public int getStartRow() {
return startRow;
}
public void setStartRow(int startRow) {
this.startRow = startRow;
}
/**
* 结束行位置, 从1开始计数
*
* @return
*/
public int getEndRow() {
if(endRow>getTotal()){
return getTotal();
}
return endRow;
}
public void setEndRow(int endRow) {
this.endRow = endRow;
}
public int getPageCount() {
return pageCount;
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
}
public class PageTag extends SimpleTagSupport {
private Pagination pagination;
private String baseurl;
public void setPagination(Pagination pagination) {
this.pagination = pagination;
}
public void setBaseurl(String baseurl) {
this.baseurl = baseurl;
}
// 分页区页码最大长度
private final static int MAX_PAGE_HTML_LENGTH = 8;
// 首部和尾部连续页码的长度
private final static int PRE_LAST_PAGE_LENGTH = 5;
@Override
public void doTag() throws JspException, IOException {
// TODO Auto-generated method stub
StringBuilder sb = new StringBuilder();
if (pagination == null || pagination.getItems() == null
|| pagination.getItems().size() == 0) {
getJspContext().getOut().print("");
return;
}
// 上一页
if (pagination.getPageCount() > 1) {
if (pagination.getPageIndex() == 1) {
sb.append(createPrePage(0, true));
} else {
sb.append(createPrePage(pagination.getPageIndex() - 1, false));
}
}
// 当总页数小于8时显示全部页码
if (pagination.getPageCount() <= MAX_PAGE_HTML_LENGTH) {
for (int i = 1; i <= pagination.getPageCount(); i++) {
if (i == pagination.getPageIndex()) {
sb.append(createPageIndex(i, true));
} else {
sb.append(createPageIndex(i, false));
}
}
} else {
// 当页码小于等于5时
if (pagination.getPageIndex() <= PRE_LAST_PAGE_LENGTH) {
sb.append(createHeader(pagination.getPageIndex() + 1,
pagination));
sb.append(createMiddle(0, pagination));
sb.append(createFooter(pagination));
} else if (pagination.getPageIndex() + 4 >= pagination
.getPageCount()) {// 当页码为后5页时
sb.append(createHeader(2, pagination));
sb.append(createMiddle(0, pagination));
sb.append(createFooter(pagination));
} else {
sb.append(createHeader(2, pagination));
sb.append(createMiddle(3, pagination));
sb.append(createFooter(pagination));
}
}
// 下一页
if (pagination.getPageCount() > 1) {
if (pagination.getPageIndex() == pagination.getPageCount()) {
sb.append(createNextPage(0, true));
} else {
sb.append(createNextPage(pagination.getPageIndex() + 1, false));
}
}
sb.append(createPageInfo(pagination));
getJspContext().getOut().print(sb.toString());
}
/**
* 头部创建
*
* @param length
* @param pagination
* @return
*/
private String createHeader(int length, Pagination pagination) {
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= length; i++) {
if (i == pagination.getPageIndex()) {
sb.append(createPageIndex(i, true));
} else {
sb.append(createPageIndex(i, false));
}
}
return sb.toString();
}
/**
* 中部创建
*
* @param length
* @param pagination
* @return
*/
private String createMiddle(int length, Pagination pagination) {
StringBuilder sb = new StringBuilder();
if (length == 0) {// 创建点...
sb.append(createPoint());
} else {
sb.append(createPoint());
for (int i = pagination.getPageIndex() - 1; i <= pagination
.getPageIndex() + 1; i++) {
if (i == pagination.getPageIndex()) {
sb.append(createPageIndex(i, true));
} else {
sb.append(createPageIndex(i, false));
}
}
sb.append(createPoint());
}
return sb.toString();
}
/**
* 尾部创建
*
* @param pagination
* @return
*/
private String createFooter(Pagination pagination) {
StringBuilder sb = new StringBuilder();
if (pagination.getPageIndex() <= 5
|| pagination.getPageIndex() + 4 < pagination.getPageCount()) {
for (int i = pagination.getPageCount() - 1; i <= pagination
.getPageCount(); i++) {
sb.append(createPageIndex(i, false));
}
} else {
for (int i = pagination.getPageIndex() - 1; i <= pagination
.getPageCount(); i++) {
if (i == pagination.getPageIndex()) {
sb.append(createPageIndex(i, true));
} else {
sb.append(createPageIndex(i, false));
}
}
}
return sb.toString();
}
private String createPrePage(int pageIndex, boolean distable) {
StringBuilder sb = new StringBuilder();
if (distable) {
sb.append("<span class='disqp'>« 上一页</span>");
} else {
sb.append("<a href='" + baseurl + getUrlStartChar()+"pageindex=" + pageIndex
+ "' class='qp'>« 上一页</a>");
}
return sb.toString();
}
private String createNextPage(int pageIndex, boolean distable) {
StringBuilder sb = new StringBuilder();
if (distable) {
sb.append("<span class='disqp'>下一页 »</span>");
} else {
sb.append("<a href='" + baseurl + getUrlStartChar()+"pageindex=" + pageIndex
+ "' class='qp'>下一页 »</a>");
}
return sb.toString();
}
private String createPageIndex(int pageIndex, boolean cur) {
StringBuilder sb = new StringBuilder();
if (!cur) {
sb.append("<a href='" + baseurl + getUrlStartChar()+"pageindex=" + pageIndex + "'>");
sb.append(pageIndex);
sb.append("</a>");
} else {
sb.append("<span class='focuspage'>" + pageIndex + "</span>");
}
return sb.toString();
}
private String createPoint() {
return " <span class='point'>...</span>";
}
private String createPageInfo(Pagination pagination) {
return "<span class='info'>共<font color=red>" + pagination.getTotal()
+ "</font>条 共<font color=red>"
+ pagination.getPageCount() + "</font>页</span>";
}
/**
* 返回URL参数首字符
* @return
*/
private String getUrlStartChar(){
if(baseurl.indexOf("?")>=0){
return "&";
}else{
return "?";
}
}
}
body{
font-size:12px;
}
.red_star{
color:red;
}
.pagearea{
margin-top:20px;
float:right;
text-align:right;
width:100%;
font-size:12px;
}
.pagearea .qp{
border:#AAAADD solid 1px;
width:60px;
height:10px;
margin-right:3px;
margin-left:3px;
text-align:center;
}
.pagearea .disqp{
border:#EEEEEE solid 1px;
width:60px;
height:10px;
margin-right:3px;
margin-left:3px;
color:#EEEEEE;
}
.pagearea a {
border:#AAAADD solid 1px;
width:25px;
height:10px;
margin-right:3px;
margin-left:3px;
text-align:center;
}
.pagearea .point{
width:25px;
height:10px;
margin-right:3px;
margin-left:3px;
}
.pagearea .focuspage{
border:#FD6D01 solid 1px;
background-color:#FFEDE1;
color:#FD6D01;
width:25px;
height:10px;
margin-right:3px;
margin-left:3px;
font-weight:bold;
text-align:center;
}
.pagearea info{
color:#666666;
}
.pagearea a{
TEXT-DECORATION:none;
color:#3366CC;
}
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<tlib-version>1.0</tlib-version>
<short-name>asktag</short-name>
<uri>http://com.ask.jstl</uri>
<tag>
<name>page</name>
<tag-class>com.ask.page.taglib.PageTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>pagination</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>baseurl</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>