前段时间为了练习想做个博客,结果博客的本事我没有,就做了个留言板。
效果就像这样了。
struts2和hibernate用上了。
贴一下分页组件和留言页面的代码吧。
分页组件
package com.leaves.util; import java.util.List; /** * 用来进行分页实现的javaBean组件 * * @author leaves * */ public class PageModel { private int currPage; // 当前页 private int totalRecords; // 总记录数 private List<?> list; // 结果集 private int pageSize; // 每页记录数 public int getCurrPage() { return currPage; } public void setCurrPage(int currPage) { this.currPage = currPage; } public int getTotalRecords() { return totalRecords; } public void setTotalRecords(int totalRecords) { this.totalRecords = totalRecords; } public List<?> getList() { return list; } public void setList(List<?> list) { this.list = list; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } /** * 获取第一页 * * @return */ public int getFirstPage() { return 1; } /** * 获取最后一页(即最后一页) * * @return */ public int getLastPage() { return (totalRecords + pageSize - 1) / pageSize; } /** * 获取下一页 * * @return */ public int getNextPage() { return (currPage >= getLastPage()) ? getLastPage() : (currPage + 1); } /** * 获取前一页 * * @return */ public int getPreviousPage() { return currPage <= 1 ? currPage : (currPage - 1); } @Override public String toString() { return "PageModel:[currPage:" + getCurrPage() + ",pageSize:" + getPageSize() + ",listSize:" + getList().size()+ "]"; } }
处理查看和保存的action
package com.leaves.action; import java.util.ArrayList; import java.util.Date; import java.util.List; import com.leaves.dao.LeaveWordDaoImpl; import com.leaves.dao.UserDaoImpl; import com.leaves.dao.WordDaoImpl; import com.leaves.model.LeaveWord; import com.leaves.util.PageModel; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class LeaveWordActiong extends ActionSupport { private LeaveWord word; private String tip; private PageModel pageModel;// 用来进行查询操作的分页模型 private List<Integer> pageList; public List<Integer> getPageList() { return pageList; } public void setPageList(List<Integer> pageList) { this.pageList = pageList; } public PageModel getPageModel() { return pageModel; } public void setPageModel(PageModel pageModel) { this.pageModel = pageModel; } public String getTip() { return tip; } public void setTip(String tip) { this.tip = tip; } public LeaveWord getWord() { return word; } public void setWord(LeaveWord word) { this.word = word; } // 处理用户保存留言 public String saveWord() throws Exception { String username = (String) ActionContext.getContext().getSession().get("user"); // 如果用户没有登陆 ,直接返回错误信息 if (username == null) { setTip(getText("saveWordWrong.noUser")); return INPUT; } if (word.getWordContent() != null && !word.getWordContent().isEmpty()) { word.setLeaveTime(new Date()); word.setWordAuthor(new UserDaoImpl().getUser(username)); new WordDaoImpl().saveWord(word); // 防止乱码,用国际化索引来指定提示信息。 // 此处发现国际化可以防止直接传输中XML和servlet页面之间产生乱码的问题。 setTip("saveSuccMsg"); return SUCCESS; } return INPUT; } // 处理查看所有留言 public String wordView() throws Exception { if (pageModel == null) { System.out.println("分页模型为空,创建新模型"); pageModel = new PageModel(); } // 做初始化工作,防止第一次访问出错 if (pageModel.getCurrPage() == 0) { System.out.println("当前页为0,设置为1"); pageModel.setCurrPage(1); // 设置默认页面当前页面为1.方便第一次访问页面 } if (pageModel.getPageSize() == 0) { System.out.println("当前页条数为0设置为5"); pageModel.setPageSize(5);// 设置每页显示的记录条数。默认为5条 } // 把从页面传参后得到的PageModel传入到getSubWord方法中来获取一个得到结果集的PageModel pageModel = new LeaveWordDaoImpl().getSubWord(getPageModel()); if (pageList==null||pageList.get(pageList.size()-1)!=pageModel.getLastPage()) { pageList=new ArrayList<Integer>(); for (int i = 1; i <= pageModel.getLastPage(); i++) { pageList.add(i); } } //根据当前页码和每页数量获得一个分页模型 setPageModel(pageModel); // 根据传送进来的国际化索引来设置提示信息。 setTip(getText(getTip())); return SUCCESS; } }
留言处理的impl
package com.leaves.dao; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import com.leaves.model.LeaveWord; import com.leaves.util.HibernateUtil; import com.leaves.util.PageModel; /** * 此类实现LeaveWordDao,实现具体对 LeaveWord进行操作的方法 * * @author leaves * */ public class LeaveWordDaoImpl implements LeaveWordDao { @Override public void saveWord(LeaveWord leaveWord) { Session session = HibernateUtil.getSession(); try { session.beginTransaction(); // 此处不允许用户修改留言,直接只用save方法 session.save(leaveWord); session.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); session.getTransaction().rollback(); }finally{ HibernateUtil.closeSession(); } } @Override public void deleteWord(Integer wordid) { } @SuppressWarnings("unchecked") @Override public List<LeaveWord> getWords() { Session session = HibernateUtil.getSession(); List<LeaveWord> words = null; try { session.beginTransaction(); // 在事务中间进行查询处理 String hql = new String("from LeaveWord l order by l.wordId asc"); words = session.createQuery(hql).list(); session.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); session.getTransaction().rollback(); }finally{ HibernateUtil.closeSession(); } return words; } @SuppressWarnings("unchecked") @Override public PageModel getSubWord(PageModel pageModel) { Session session=HibernateUtil.getSession(); List<LeaveWord> words = null; session.beginTransaction(); try { // 在事务中间进行查询处理 String hql = new String("from LeaveWord as l order by wordId asc"); // 进行HQL分页查询 Query query = session.createQuery(hql); // 设置起始位置 query.setFirstResult((pageModel.getCurrPage() - 1) * pageModel.getPageSize()); // 设置总记录条数 query.setMaxResults(pageModel.getPageSize()); // 获取结果 words = query.list(); // 获取数据库总记录数 int totalRecords = this.getTotalRecords(session); if (totalRecords >= 1) { // 设置总记录数 pageModel.setTotalRecords(totalRecords); // 设置记录集合 pageModel.setList(words); } session.getTransaction().commit(); //---------------------------------------------------------------------------------------- } catch (Exception e) { e.printStackTrace(); session.getTransaction().rollback(); }finally{ //HibernateUtil.closeSession(); } return pageModel; } /** * 获取总记录数的方法 * * @param session * @return */ public int getTotalRecords(Session session) { String hql = "select count(*) from LeaveWord"; Long longResult = (Long) session.createQuery(hql).uniqueResult(); return longResult.intValue(); } }
现实留言页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> <%@taglib uri="/struts-tags" prefix="s"%> <!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>allWords</title> <script type="text/javascript"> function submitByPage(currPage){ document.getElementById("pageModel.currPage").value=currPage; theForm=document.getElementById("getWords"); theForm.action="wordView"; theForm.submit(); } function submitBySize(){ document.getElementById("pageModel.pageSize").value=document.getElementById("pageModel.pageSize").value; theForm=document.getElementById("getWords"); theForm.action="wordView"; theForm.submit(); } </script> </head> <body> <h1><s:i18n name="pageI18N.leavesWords"><s:text name="h1.allWordMsg"/></s:i18n></h1> <s:set value="pageModel.list" name="wordList" /> <table border="1" width=100% align="center"> <s:iterator value="wordList" var="oneWord" status="st"> <tr> <td colspan="2"> <!-- 输出留言内容 --> <s:property value="#oneWord.wordContent" /> </td> </tr> <tr bgcolor="#c8eb50" align="right"> <td> <!-- 输出楼层 --> <s:text name="view.floorNum"> <s:param><font color=red><s:property value="#st.count"/></font></s:param> </s:text> <!-- 输出作者 --> <s:text name="view.author"> <s:param><font color=blue><s:property value="#oneWord.wordAuthor.username"/></font></s:param> </s:text> <!-- 输出时间 --> <s:text name="view.leaveTime"> <s:param><font color=green><s:date name="#oneWord.leaveTime" format="yyyy-MM-dd hh:mm:ss"/></font></s:param> </s:text> </td> </tr> </s:iterator> </table> <div> <s:i18n name="pageI18N.leavesWords"> <form id="getWords"> <s:token/> <s:text name="page.goto"> <s:param> <!-- 此处暂未实现下拉选择页码 --> <s:select id="pageModel.currPage" value="pageModel.currPage" onchange="submitByPage(this.value);" list="pageList" name="pageModel.currPage"></s:select> </s:param> </s:text> <!-- 页码控制--> <a href="#" onclick="submitByPage(${pageModel.firstPage});"><s:text name="page.first"/></a> <a href="#" onclick="submitByPage(${pageModel.previousPage});"><s:text name="page.previous"/></a> <a href="#" onclick="submitByPage(${pageModel.nextPage});"><s:text name="page.next"/></a> <a href="#" onclick="submitByPage(${pageModel.lastPage});"><s:text name="page.last"/></a> <s:text name="results.OnePage"> <s:param> <s:select id="pageModel.pageSize" onchange="submitBySize();" list="{1,2,3,4,5,6,7,8,9}" name="pageModel.pageSize" value="pageModel.pageSize"/> </s:param> </s:text> </form> </s:i18n> </div> <jsp:include page="leaveWord.jsp"></jsp:include> </body> </html>
lib包
发上源码(只有src&WebContent,用的是eclipse for ee )