<html>
<head>
<title>JSP for SelectActionForm form</title>
<script type="javaScript">
function submitForm()
{
if(document.form1.selectValue.value=="")
{
alert("请输入查找关键字");
document.form1.selectValue.focus();
return false;
}else
{
return true;
}
}
function toPage()
{
if(document.form1.pageText.value=="")
{
alert("请输入要前往的页数");
document.form1.pageText.focus();
return false;
}else
{
a=document.form1.pageText.value;
if(a<=0||a>=${requestScope.pagecount})
a=${requestScope.page}
document.form1.action = "selectAction.do?page="+a+"&selectValue=${requestScope.selectValue}";
return true;
}
}
</script>
</head>
<body>
<center>
<form name="form1" action="selectAction.do" method="POST">
<table>
<tr>
<td>
please input:
</td>
<td>
<input type="text" name="selectValue"
value="${requestScope.selectValue}" />
</td>
<td>
<input type="submit" onclick="submitForm()" value="search" />
</td>
</tr>
</table>
<c:if test="${not empty sessionScope.selectList}">
<table border="1" cellpadding="3" cellspacing="3">
<tr>
<th>
ID
</th>
<th>
Name
</th>
<th>
DESC
</th>
<th>
Date
</th>
<th>
CreateBy
</th>
</tr>
<c:forEach var="cddate" items="${sessionScope.selectList}">
<tr>
<td>
${cddate.pageCategoryId}
</td>
<td>
${cddate.pageItemName}
</td>
<td>
${cddate.pageItemDesc}
</td>
<td>
${cddate.pageItemDate}
</td>
<td>
${cddate.pageItemBy}
</td>
</tr>
</c:forEach>
</table>
<table>
<tr>
<td>
<a
href="selectAction.do?action=frist&selectValue=${requestScope.selectValue}">MainPage</a>
</td>
<td>
<c:if test="${requestScope.page==1}">lastPage</c:if>
<c:if test="${requestScope.page!=1}">
<a
href="selectAction.do?action=back&page=${requestScope.page}&selectValue=${requestScope.selectValue}">lastPage</a>
</c:if>
</td>
<td>
<c:if test="${requestScope.page==requestScope.pagecount}">nextPage</c:if>
<c:if test="${requestScope.page!=requestScope.pagecount}">
<a
href="selectAction.do?action=next&page=${requestScope.page}&selectValue=${requestScope.selectValue}">nextPage</a>
</c:if>
</td>
<td>
<a
href="selectAction.do?action=end&selectValue=${requestScope.selectValue}">endPage</a>
</td>
<td>
${requestScope.page} / ${requestScope.pagecount}
</td>
<td>
changeTo
<input type="text" size="2" name="pageText"
onkeyup="value=value.replace(/[^\d]/g,'') "
onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"
value="${requestScope.page}" />
<input type="submit" onclick="toPage()" value="GO" />
</td>
</tr>
</table>
</c:if>
</form>
</center>
</body>
</html>
3 . struts-config.xml文件
<global-forwards>
<forward name="select" path="/select.jsp" />
</global-forwards>
<form-beans>
<form-bean name="selectActionForm" type="SelectActionForm" />
</form-beans>
<action-mappings>
<action input="/select.jsp" name="selectActionForm" path="/selectAction" scope="request" type="SelectAction" validate="true" />
</action-mappings>
4. SelectActionForm.java
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletRequest;
public class SelectActionForm
extends ActionForm
{
private String pageText; //页面编码
private String selectValue; //查询条件关键字
public String getPageText() //跳转到的页面
{
return pageText;
}
public void setPageText(String pageText)
{
this.pageText = pageText;
}
public void setSelectValue(String selectValue)
{
this.selectValue = selectValue;
}
public String getSelectValue()
{
return selectValue;
}
public ActionErrors validate(ActionMapping actionMapping,
HttpServletRequest httpServletRequest)
{ /** @todo: finish this method, this is just the skeleton.*/
return null;
}
public void reset(ActionMapping actionMapping,
HttpServletRequest servletRequest)
{
}
}
5. SelectAction.java
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.Action;
import com.wang.business.BusinessManage;
import com.wang.module.*;
import java.util.*;
public class SelectAction
extends Action
{
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
{
SelectActionForm selectForm = (SelectActionForm) form;
BusinessManage bm = new BusinessManage();
int page = 1; //初始化为第一页
if (selectForm.getSelectValue() != null)
{
// if(request.getParameter("action")!=null)
if (request.getParameter("page") == null)
{
page = 1;
}
else
{
page = Integer.parseInt(request.getParameter("page"));
}
if (selectForm.getPageText() != null)
{
page = Integer.parseInt(selectForm.getPageText());
}
if (request.getParameter("action") != null)
{
if (request.getParameter("action").equals("frist")) { //跳转到首页
page = 1;
}
else if (request.getParameter("action").equals("end")) //跳转到尾页
{
page = bm.PAGECOUNT;
}
else if (request.getParameter("action").equals("back")) //跳转到上一页
{
page -= 1;
}
else if (request.getParameter("action").equals("next")) //跳转到下一页
{
page += 1;
}
}
List list = bm.selectCDBean(selectForm.getSelectValue(), page, 5); //页面传递三个参数,将取得的值存放于一个list列表中
// ArrayList list1 = new ArrayList(list);
request.getSession().setAttribute("selectList", list); //将页码集合变量存放于字符串变量selectList中,存放于session范围内
int pagecount = bm.PAGECOUNT; //页面总数
request.setAttribute("pagecount", pagecount);
request.getSession().removeAttribute("selectList");
request.getSession().setAttribute("selectList", list);
request.setAttribute("selectValue", selectForm.getSelectValue());
request.setAttribute("page", page);
request.setAttribute("pagecount", pagecount);
}
else
{
request.getSession().removeAttribute("selectList");
}
bm.close();
return mapping.findForward("select");
}
}
6. 业务逻辑
BusinessManage.java
package com.wang.business;
import org.hibernate.*;
import org.hibernate.cfg.*;
import com.wang.module.*;
import java.util.*;
public class BusinessManage
{
private SessionFactory sf = null;
private Session s = null;
private Transaction ts = null;
private Query query = null;
public static int PAGECOUNT;
public BusinessManage()
{
sf = new Configuration().configure().buildSessionFactory();
s = sf.openSession();
ts = s.beginTransaction();
}
public void openSession()
{
s = sf.openSession();
}
public List selectCDBean(String value, int page, int count)
{
List list = null;
int pagelast = 0;
try
{
query = s.createQuery("from ViewPage cd where cd.pageItemName like '%"
+ value + "%'");
if (query.list().size() / count == 0) //页数为偶数
{
PAGECOUNT = query.list().size() / count; //页面总数
}
else
{
PAGECOUNT = query.list().size() / count + 1; //页数为基数
pagelast = query.list().size() / count;
}
int begin = page * count - count; //count为每页显示的纪录数,
int end = page * count;
if (page == PAGECOUNT)
{
end = query.list().size();
}
list = query.list().subList(begin, end);
}
catch (Exception ex)
{
list = null;
ex.printStackTrace();
}
return list;
}
public void close()
{
s.close();
}
}