基於SSH的預約掛號系統之公告模組
阿新 • • 發佈:2018-12-16
後臺管理頁面
建立一個公告的 JavaBean
private Integer noticeId;//公告Id private String title;//標題 private String text;//內容 private Admin author;//釋出人 private Date createTime;//建立時間 private Date updateTime;//最後一次修改時間 private Integer isShow;//是否在前端頁面展示
對映檔案 Notice.hbm.xml
<hibernate-mapping> <class name="com.zhc.entities.Notice" table="NOTICE"> <id name="noticeId" type="java.lang.Integer"> <column name="NOTICE_ID" /> <generator class="native" /> </id> <property name="title" type="java.lang.String"> <column name="TITLE" /> </property> <property name="text" type="java.lang.String"> <column name="TEXT" sql-type="text"/> </property> <property name="createTime" type="java.util.Date"> <column name="CREATE_TIME" sql-type="datetime"></column> </property> <property name="updateTime" type="java.util.Date"> <column name="UPDATE_TIME" sql-type="datetime"></column> </property> <property name="isShow" type="java.lang.Integer"> <column name="IS_SHOW" sql-type="tinyint"></column> </property> <many-to-one name="author" class="com.zhc.entities.Admin" column="ADMIN_ID"></many-to-one> </class> </hibernate-mapping>
新增和修改公告
Action 方法
//新增公告 public String addNotice() { noticeService.save(notice); return SUCCESS; } public void prepareAddNotice() { Date now = new Date();
//如果Id為空則為新增公告 if(noticeId == null) { notice = new Notice(); notice.setCreateTime(now); notice.setUpdateTime(now); notice.setIsShow(0);//預設不在主頁展示區
//否則為修改公告,先從資料庫獲取該公告資訊 }else { notice = noticeService.getById(noticeId); notice.setUpdateTime(now); } }
其中 prepareAddNotice 方法通過判斷是否傳入了 NoticeId來判斷是新增公告還是修改公告,並且準備相應的 Model
Service 方法
public void save(Notice notice) { noticeDao.save(notice); }
Dao 方法
public void save(T t) { this.getHibernateTemplate().save(t); }
刪除公告
Action 方法
//刪除公告 public String delete() { noticeService.delete(noticeService.getById(noticeId)); return SUCCESS; }
Service 方法
public void delete(Notice notice) { noticeDao.delete(notice); }
Dao 方法
public void delete(T t) { this.getHibernateTemplate().delete(t); }
查詢方法
由於涉及到分頁,建立一個用於分頁的 PageBean
private Integer currPage;// 當前頁數 private Integer pageSize;// 每頁顯示的記錄數 private Integer totalCount;// 總記錄數 private Integer totalPage;// 總頁數 private List<T> list;// 資料集合
Action中的方法
呼叫Action中的 list 方法時需要兩個引數,currPage 為當前頁面,pageSize為每頁顯示個數
private Integer currPage = 1;//獲取當前頁數 public void setCurrPage(Integer currPage) { if(currPage == null) { currPage = 1; } this.currPage = currPage; } private Integer pageSize = 3;//每頁顯示個數 public void setPageSize(Integer pageSize) { if(pageSize == null) { pageSize = 3; } this.pageSize = pageSize; } //公告列表 public String list() { /*DetachedCriteria物件,條件查詢+分頁*/ DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Notice.class); PageBean<Notice> pageBean = noticeService.findByPage(detachedCriteria, currPage, pageSize); request.put("pageBean", pageBean); return "list"; }
Service 方法
@Override //分頁查詢公告 public PageBean<Notice> findByPage(DetachedCriteria detachedCriteria, Integer currPage, Integer pageSize) { PageBean<Notice> pageBean = new PageBean<Notice>(); pageBean.setPageSize(pageSize);//封裝每頁顯示記錄數 //封裝總記錄數 Integer totalCount = noticeDao.findCount(detachedCriteria); pageBean.setTotalCount(totalCount); //封裝總頁數(向上取整) Double tc = totalCount.doubleValue(); int num = (int) Math.ceil(tc/pageSize); //如果當前頁大於總頁數,則跳到最後一頁 if(currPage > num) { currPage = num; } pageBean.setCurrPage(currPage);//封裝當前頁數 pageBean.setTotalPage(num); //計算起始位置 Integer begin = (currPage - 1)* pageSize; List<Notice> list = noticeDao.findByPage(detachedCriteria, begin, pageSize); pageBean.setList(list); return pageBean; }
Dao 方法
//查詢總記錄數 @Override public Integer findCount(DetachedCriteria detachedCriteria) { detachedCriteria.setProjection(Projections.rowCount()); List<Long> list = (List<Long>) this.getHibernateTemplate().findByCriteria(detachedCriteria); if(list.size() > 0) { return list.get(0).intValue(); } return null; } //分頁查詢公告 @Override public List<Notice> findByPage(DetachedCriteria detachedCriteria, Integer begin, Integer pageSize) { //由於在findCount中設定了detachedCriteria.setProjection(Projections.rowCount())來查詢記錄數,需要清空 detachedCriteria.setProjection(null); return (List<Notice>) this.getHibernateTemplate().findByCriteria(detachedCriteria, begin, pageSize); }
前端頁面通過 <s:iterator> 遍歷顯示
<s:iterator value="#request.pageBean.list">
<tr>
<td>${title }</td>
<td>${text }</td>
<td>${author.adminName }</td>
<td>
<s:if test="isShow == 1">是</s:if>
<s:else>否</s:else>
</td>
<td><s:date name="createTime"
format="yyyy-MM-dd hh:mm:ss" /></td>
<td><s:date name="updateTime"
format="yyyy-MM-dd hh:mm:ss" /></td>
<td><a
href="${pageContext.request.contextPath }/notice-edit.action?noticeId=<s:property value="noticeId"/>">修改</a>
<a
href="${pageContext.request.contextPath }/notice-delete.action?noticeId=<s:property value="noticeId"/>">刪除</a>
</td>
</tr>
</s:iterator>
CSS 控制表格中文字溢位時顯示省略號
table { font-family: verdana, arial, sans-serif; font-size: 12px; color: #333333; border-width: 1px; border-color: #999999; border-collapse: collapse; table-layout: fixed; /* 表格寬度不隨文字增多而變長 */ } table td { border-width: 1px; padding: 8px; border-style: solid; border-color: #a9c6c9; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
分頁選單的實現
<!-- 分頁選單開始 -->
<table id="pagelink">
<tbody>
<tr>
<td>
<div
style="LINE-HEIGHT: 20px; HEIGHT: 20px; TEXT-ALIGN: right">
共[<B>${pageBean.totalCount }</B>]條記錄,[<B>${pageBean.totalPage }</B>]頁
,每頁顯示 <select name="pageSize" onchange="to_page()">
<option value="3"
<s:if test="#request.pageBean.pageSize == 3">selected</s:if>>3</option>
<option value="5"
<s:if test="#request.pageBean.pageSize == 5">selected</s:if>>5</option>
<option value="10"
<s:if test="#request.pageBean.pageSize == 10">selected</s:if>>10</option>
</select> 條
<s:if test="#request.pageBean.currPage != 1">
[<A href="javascript:to_page(1)">首頁</A>]
[<A href="javascript:to_page(<s:property value="#request.pageBean.currPage-1"/>)">上一頁</A>]
</s:if>
<B>
<s:iterator var="i" begin="1" end="#request.pageBean.totalPage">
<s:if test="#i == #request.pageBean.currPage">
<s:property value="#i" />
</s:if>
<s:else>
<a href="javascript:to_page(<s:property value="#i"/>)"><s:property value="#i" /></a>
</s:else>
</s:iterator>
</B>
<s:if
test="#request.pageBean.currPage != #request.pageBean.totalPage">
[<A
href="javascript:to_page(<s:property value="#request.pageBean.currPage+1"/>)">下一頁</A>]
[<A
href="javascript:to_page(<s:property value="#request.pageBean.totalPage"/>)">尾頁</A>]
</s:if>
到 <input type="text" size="3" id="page" name="currPage" /> 頁
<input type="button" value="Go" onclick="to_page()" />
</div>
</td>
</tr>
</tbody>
</table>
<!-- 分頁選單結束 -->
當更改頁面顯示數目或者點選其他頁的超連結時,呼叫下面的方法提交表單呼叫 Action中的 list 方法
function to_page(page) { if (page) { $("#page").val(page);/*設定跳轉的頁面*/ } $("#noticeForm").submit(); }
主頁顯示公告模組
通過AJAX非同步查詢
$.post("${pageContext.request.contextPath }/notice-findInShow.action",{"time": new Date()}, function(data){ // 遍歷json的資料: $(data.showNotice).each(function(i,n){ $("#show_notice").append("<li><span>公告</span><a href='#'>"+n.title+"</li>"); }); },"json");
Action 方法
//儲存返回的資料 private Map<String, Object> dataMap = new HashMap<>(); public void setDataMap(Map<String, Object> dataMap) { this.dataMap = dataMap; } public Map<String, Object> getDataMap() { return dataMap; } //查詢首頁顯示的公告 public String findInShow() { dataMap.put("showNotice", noticeService.findInShow()); return "ajax-success"; }
Service 方法
@Override public List<Notice> findInShow() { return noticeDao.findInShow(); }
Dao 方法
//查詢首頁展示區的公告 @Override public List<Notice> findInShow() { return (List<Notice>) this.getHibernateTemplate().find("from Notice where isShow = ?", 1); }
struts配置檔案
<result name="ajax-success" type="json"> <param name="root">dataMap</param> </result>