基于SSH的预约挂号系统之公告模块

后台管理页面

创建一个公告的 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>
                &nbsp;&nbsp; <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>
                        &nbsp;&nbsp;
                        <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>&nbsp;&nbsp;
                        <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>

完成日期:2018/12/14-2018/12/15

猜你喜欢

转载自www.cnblogs.com/zhanghongcan/p/10126684.html