基於SSH框架的小型論壇項目 javascript
1、項目入門 傳送門css
2、框架整合 傳送門html
3、用戶模塊 傳送門前端
4、頁面顯示 傳送門java
5、帖子模塊 傳送門mysql
6、點贊模塊 傳送門jquery
7、輔助模塊 傳送門web
帖子表與回覆表spring
編寫帖子模塊
添加帖子
查看帖子
查看帖子
頁面的改變
-用戶
-文本
內容去掉br標籤
用戶頭像
分頁
引入資源
分析
pageSize
currentPage
totalCount
totalPage
所須要的參數:
*起始索引
pageSize(本身定義的)
currentPage
totalCount select count(*) from paste
總頁數 = 總條數/每頁顯示條數 + 1 Math.ceil() 2.3 3
4 10 3 3.333 3 3
4 12 3 4 3.00001 4
select * from paste limit ?,?
第一個問號:起始索引
頁數 頁面大小 起始索引
1 2 0
2 2 2
3 2 4
n 2 (第一個數-1)* 第二個數
第二個問號:查詢多少數據(pageSize)
經過頁面分析:
currentPage
尾頁:總頁數
編碼
回覆帖子
1)進入帖子
點擊標題進入detail.jsp
2)回覆帖子
關係分析圖sql
數據庫中貼子表的建立
create table paste( id varchar(50)primary key, title varchar(1000) not null, content varchar(3000) not null, offer int not null, ansnum int default 0, createtime varchar(100) not null, glanceover int default 0, solve int default 0, isdelete int default 0, answerid varchar(50), userid varchar(50) )
回覆表
create table answer( id varchar(50) primary key, userid varchar(50) not null, pasteid varchar(50) not null, content varchar(3000) not null, anstime varchar(100) not null, agree int default 0, solve int default 0 )
添加帖子
add.jsp中添加帖子表單路徑
<form action="${pageContext.request.contextPath}/PasteAction_addPaste">
使用模型驅動,建立一個PasteAction.class
public class PasteAction extends ActionSupport implements ModelDriven<Paste>
建立封裝PasteAction.class(Web層)、PasteService.class(Service層)、PasteDao.class(Dao層)數據
package com.Gary.web; import java.text.SimpleDateFormat; import java.util.Date; import com.Gary.domain.Paste; import com.Gary.domain.User; import com.Gary.service.PasteService; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; public class PasteAction extends ActionSupport implements ModelDriven<Paste> { public Paste paste = new Paste(); private PasteService pasteService; public String addPaste() throws Exception { User user = (User)ActionContext.getContext().getSession().get("user"); if(user==null) { ActionContext.getContext().put("error", "只有登錄以後才能夠發帖子!!"); return "error"; } //private Integer ansnum; paste.setAnsnum(0); //private String createtime; Date date = new Date(System.currentTimeMillis()); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); String createtime = format.format(date); paste.setCreatetime(createtime); //private Integer glanceover; paste.setGlanceover(0); //是否結帖 0未結 1結束 //private Integer solve; paste.setSolve(0); //private Integer isdelete; paste.setIsdelete(0); //private User user; paste.setUser(user); pasteService.addPaste(paste); //重定向到主頁 return "toIndex"; } public PasteService getPasteService() { return pasteService; } public void setPasteService(PasteService pasteService) { this.pasteService = pasteService; } @Override public Paste getModel() { // TODO Auto-generated method stub return paste; } }
package com.Gary.service; import com.Gary.dao.PasteDao; import com.Gary.domain.Paste; public class PasteService { private PasteDao pasteDao; public PasteDao getPasteDao() { return pasteDao; } public void setPasteDao(PasteDao pasteDao) { this.pasteDao = pasteDao; } public void addPaste(Paste paste) { pasteDao.addPaste(paste); } }
package com.Gary.dao; import org.hibernate.Session; import org.springframework.orm.hibernate5.support.HibernateDaoSupport; import com.Gary.domain.Paste; public class PasteDao extends HibernateDaoSupport{ public void addPaste(Paste paste) { Session session = getHibernateTemplate().getSessionFactory().getCurrentSession(); session.save(paste); } }
添加帖子屬性
private String id; private String title; private String content; private Integer offer; private Integer ansnum; private String createtime; private Integer glanceover; //是否結帖 private Integer solve; private Integer delete;
package com.Gary.domain; public class Paste { private String id; private String title; private String content; private Integer offer; private Integer ansnum; private String createtime; private Integer glanceover; //是否結帖 private Integer solve; private Integer delete; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public Integer getOffer() { return offer; } public void setOffer(Integer offer) { this.offer = offer; } public Integer getAnsnum() { return ansnum; } public void setAnsnum(Integer ansnum) { this.ansnum = ansnum; } public String getCreatetime() { return createtime; } public void setCreatetime(String createtime) { this.createtime = createtime; } public Integer getGlanceover() { return glanceover; } public void setGlanceover(Integer glanceover) { this.glanceover = glanceover; } public Integer getSolve() { return solve; } public void setSolve(Integer solve) { this.solve = solve; } public Integer getDelete() { return delete; } public void setDelete(Integer delete) { this.delete = delete; } }
回覆帖屬性
public class PasteService { private PasteDao pasteDao; public PasteDao getPasteDao() { return pasteDao; } public void setPasteDao(PasteDao pasteDao) { this.pasteDao = pasteDao; } public void addPaste(Paste paste) { pasteDao.addPaste(paste); } }
配置一對多屬性
Paste中配置用戶帖子
private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; }
User.class中表明該用戶發表的全部帖子
private Set<Paste> pasteSet = new HashSet<Paste>(); public Set<Paste> getPasteSet() { return pasteSet; } public void setPasteSet(Set<Paste> pasteSet) { this.pasteSet = pasteSet; }
User.hbm.xml中配置外鍵
<set name="pasteSet">
<!-- 外鍵列名 -->
<key column="userid"></key>
<one-to-many class="Paste"/>
</set>
Paste.hbx.xml中配置一對多的關係
<many-to-one name="user" class="User" column="userid" insert="true"></many-to-one>
配置applicationContext.xml
<!-- 配置Action --> <bean name="userAction" class="com.Gary.web.UserAction" scope="prototype"> <property name="userService" ref="userService"></property> </bean> <bean name="pasteAction" class="com.Gary.web.PasteAction" scope="prototype"> <property name="pasteService" ref="userService"></property> </bean> <!-- 配置Service --> <bean name="userService" class="com.Gary.service.UserService"> <property name="userDao" ref="userDao"></property> </bean> <bean name="pasteService" class="com.Gary.service.PasteService"> <property name="pasteDao" ref="pasteDao"></property> </bean> <!-- 配置Dao --> <bean name="userDao" class="com.Gary.dao.UserDao"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean name="pasteDao" class="com.Gary.dao.PasteDao"> <property name="sessionFactory" ref="sessionFactory"></property> </bean>
登錄成功後,測試發帖功能
package com.Gary.dao; import org.hibernate.Session; import org.springframework.orm.hibernate5.support.HibernateDaoSupport; import com.Gary.domain.Paste; public class PasteDao extends HibernateDaoSupport{ public void addPaste(Paste paste) { Session session = getHibernateTemplate().getSessionFactory().getCurrentSession(); session.save(paste); } }
package com.Gary.domain; public class Paste { private String id; private String title; private String content; private Integer offer; private Integer ansnum; private String createtime; private Integer glanceover; //是否結帖 private Integer solve; private Integer isdelete; private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public Integer getOffer() { return offer; } public void setOffer(Integer offer) { this.offer = offer; } public Integer getAnsnum() { return ansnum; } public void setAnsnum(Integer ansnum) { this.ansnum = ansnum; } public String getCreatetime() { return createtime; } public void setCreatetime(String createtime) { this.createtime = createtime; } public Integer getGlanceover() { return glanceover; } public void setGlanceover(Integer glanceover) { this.glanceover = glanceover; } public Integer getSolve() { return solve; } public void setSolve(Integer solve) { this.solve = solve; } public Integer getIsdelete() { return isdelete; } public void setIsdelete(Integer isdelete) { this.isdelete = isdelete; } }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package = "com.Gary.domain"> <class name="Paste" table="paste"> <id name="id"> <generator class="uuid"></generator> </id> <!--private String title; private String content; private Integer offer; private Integer ansnum; private String createtime; private Integer glanceover; //是否結帖 private Integer solve; private Integer isdelete;--> <property name="title" column="title"></property> <property name="content" column="content"></property> <property name="offer" column="offer"></property> <property name="ansnum" column="ansnum"></property> <property name="createtime" column="createtime"></property> <property name="glanceover" column="glanceover"></property> <property name="solve" column="solve"></property> <property name="isdelete" column="isdelete"></property> <many-to-one name="user" class="User" column="userid" insert="true"></many-to-one> </class> </hibernate-mapping>
package com.Gary.service;
import com.Gary.dao.PasteDao;
import com.Gary.domain.Paste;
public class PasteService {
private PasteDao pasteDao;
public PasteDao getPasteDao() {
return pasteDao;
}
public void setPasteDao(PasteDao pasteDao) {
this.pasteDao = pasteDao;
}
public void addPaste(Paste paste) {
pasteDao.addPaste(paste);
}
}
package com.Gary.web; import java.text.SimpleDateFormat; import java.util.Date; import com.Gary.domain.Paste; import com.Gary.domain.User; import com.Gary.service.PasteService; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; public class PasteAction extends ActionSupport implements ModelDriven<Paste> { public Paste paste = new Paste(); private PasteService pasteService; public String addPaste() throws Exception { User user = (User)ActionContext.getContext().getSession().get("user"); if(user==null) { ActionContext.getContext().put("error", "只有登錄以後才能夠發帖子!!"); return "error"; } //private Integer ansnum; paste.setAnsnum(0); //private String createtime; Date date = new Date(System.currentTimeMillis()); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); String createtime = format.format(date); paste.setCreatetime(createtime); //private Integer glanceover; paste.setGlanceover(0); //是否結帖 0未結 1結束 //private Integer solve; paste.setSolve(0); //private Integer isdelete; paste.setIsdelete(0); //private User user; paste.setUser(user); pasteService.addPaste(paste); //重定向到主頁 return "toIndex"; } public PasteService getPasteService() { return pasteService; } public void setPasteService(PasteService pasteService) { this.pasteService = pasteService; } @Override public Paste getModel() { // TODO Auto-generated method stub return paste; } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 配置數據源 --> <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="jdbcUrl" value="jdbc:mysqL:///garyssh_forum"></property> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="user" value="root"></property> <property name="password" value="123456"></property> </bean> <!-- 配置sessionFactory --> <bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sqp">true</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> <property name="mappingDirectoryLocations" value="classpath:com/Gary/domain"></property> </bean> <!-- 配置事務的核心管理器 --> <bean name="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 通知 --> <tx:advice id="advice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*" /> </tx:attributes> </tx:advice> <!-- 織入 --> <aop:config> <!-- 切入點 --> <aop:pointcut expression="execution(* com.Gary.service.*.*(..))" id="pc" /> <!-- 配置切面 切入點+通知 --> <aop:advisor advice-ref="advice" pointcut-ref="pc" /> </aop:config> <!-- 配置Action --> <bean name="userAction" class="com.Gary.web.UserAction" scope="prototype"> <property name="userService" ref="userService"></property> </bean> <bean name="pasteAction" class="com.Gary.web.PasteAction" scope="prototype"> <property name="pasteService" ref="pasteService"></property> </bean> <!-- 配置Service --> <bean name="userService" class="com.Gary.service.UserService"> <property name="userDao" ref="userDao"></property> </bean> <bean name="pasteService" class="com.Gary.service.PasteService"> <property name="pasteDao" ref="pasteDao"></property> </bean> <!-- 配置Dao --> <bean name="userDao" class="com.Gary.dao.UserDao"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean name="pasteDao" class="com.Gary.dao.PasteDao"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> </beans>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <!--開啓動態方法調用 --> <constant name="struts.devMode" value="true"></constant> <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant> <!-- 告訴struts不用本身建立Action,Spring來幫你建立 --> <constant name="struts.objectFactory" value="spring"></constant> <package name="Gary_SSHForum" namespace="/" extends="struts-default"> <!-- 容許所有方法 --> <global-allowed-methods>regex:.*</global-allowed-methods> <action name="UserAction_*" class="com.Gary.web.UserAction" method="{1}"> <result name="toLogin" type="redirect">/login.jsp</result> <result name="login">/login.jsp</result> <result name="toIndex" type="redirect">/index.jsp</result> <result name="error">/login.jsp</result> <result name="toRegisterSuccess" type="redirect">/registerSuccess.jsp</result> </action> <action name="PasteAction_*" class="com.Gary.web.PasteAction" method="{1}"> <result name="toIndex" type="redirect">/index.jsp</result> <result name="error">/login.jsp</result> </action> </package> </struts>
完善發帖
對用戶登錄進行判斷
<!--描述:未登陸的樣子--> <s:if test="#session.user!=null"> <a class="avatar" href=""> <img src="res/images/avatar/11.jpg"> <cite><s:property value="#session.user.username" /></cite> </a> <div class="nav"> <a href=""> <i class="iconfont icon-tuichu" style="top: 0; font-size: 22px;"></i> 退出 </a> </div> </s:if> <!--描述:未登陸的樣子--> <s:if test="#session.user==null"> <a class="iconfont icon-touxiang layui-hide-xs" style="margin-top: 4px; display: inline-block;"> </a> <div class="nav" style="font-size: 14px; color: white; margin-top: -5px; margin-left: 1px;" /> <a href="login.jsp" target="_parent">登陸</a> <a href="register.jsp" target="_parent">註冊</a> </s:if>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <link rel="stylesheet" href="res/layui/css/layui.css"> <link rel="stylesheet" href="res/css/global.css"> <link rel="stylesheet" type="text/css" href="css/index.css"> <script src="res/layui/layui.js"></script> <style> </style> </head> <body> <div class="dvhead"> <div class="dvlogo"> <a href="index.jsp" target="_parent" fount-size="34px">論壇</a> </div>+ <div class="dvsearch">Cynical丶Gary</div> <div class="nav-user" style="top: 0px; right: 100px;"> <!--描述:未登陸的樣子--> <s:if test="#session.user!=null"> <a class="avatar" href=""> <img src="res/images/avatar/11.jpg"> <cite><s:property value="#session.user.username" /></cite> </a> <div class="nav"> <a href=""> <i class="iconfont icon-tuichu" style="top: 0; font-size: 22px;"></i> 退出 </a> </div> </s:if> <!--描述:未登陸的樣子--> <s:if test="#session.user==null"> <a class="iconfont icon-touxiang layui-hide-xs" style="margin-top: 4px; display: inline-block;"> </a> <div class="nav" style="font-size: 14px; color: white; margin-top: -5px; margin-left: 1px;" /> <a href="login.jsp" target="_parent">登陸</a> <a href="register.jsp" target="_parent">註冊</a> </s:if> </div> </div> </body> </html>
查詢帖子
分析查詢帖子全部功能
查詢帖子全部信息,用一個List<>泛型去接收
Web層建立一個GetDataAction.java去Service中查詢數據
public class GetDataAction extends ActionSupport{ private PasteService pasteService; public String getAllPaste() throws Exception { List<Paste> pasteList = pasteService.findAllPaste(); ActionContext.getContext().put("pasteList", pasteList); return "index"; } public PasteService getPasteService() { return pasteService; } public void setPasteService(PasteService pasteService) { this.pasteService = pasteService; } }
Service層中建立PasteService.java在Dao層查詢數據庫
public class PasteService { private PasteDao pasteDao; public PasteDao getPasteDao() { return pasteDao; } public List<Paste> findAllPaste() { return pasteDao.findAllPaste(); } public void setPasteDao(PasteDao pasteDao) { this.pasteDao = pasteDao; } public void addPaste(Paste paste) { pasteDao.addPaste(paste); } }
Dao層建立PasteDao.java在使用HQL在數據庫中查詢數據
public class PasteDao extends HibernateDaoSupport{ public void addPaste(Paste paste) { Session session = getHibernateTemplate().getSessionFactory().getCurrentSession(); session.save(paste); } //HQL public List<Paste> findAllPaste() { Session session = getHibernateTemplate().getSessionFactory().getCurrentSession(); //書寫SQL語句 String hql = "form com.Gary.domain.Paste"; Query query = session.createQuery(hql); List<Paste> list = query.list(); return list; } }
package com.Gary.web; import java.util.List; import com.Gary.domain.Paste; import com.Gary.service.PasteService; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class GetDataAction extends ActionSupport{ private PasteService pasteService; public String getAllPaste() throws Exception { List<Paste> pasteList = pasteService.findAllPaste(); ActionContext.getContext().put("pasteList", pasteList); return "index"; } public PasteService getPasteService() { return pasteService; } public void setPasteService(PasteService pasteService) { this.pasteService = pasteService; } }
package com.Gary.service; import java.util.List; import com.Gary.dao.PasteDao; import com.Gary.domain.Paste; public class PasteService { private PasteDao pasteDao; public PasteDao getPasteDao() { return pasteDao; } public List<Paste> findAllPaste() { return pasteDao.findAllPaste(); } public void setPasteDao(PasteDao pasteDao) { this.pasteDao = pasteDao; } public void addPaste(Paste paste) { pasteDao.addPaste(paste); } }
package com.Gary.dao; import java.util.List; import org.hibernate.Session; import org.hibernate.query.Query; import org.springframework.orm.hibernate5.support.HibernateDaoSupport; import com.Gary.domain.Paste; public class PasteDao extends HibernateDaoSupport{ public void addPaste(Paste paste) { Session session = getHibernateTemplate().getSessionFactory().getCurrentSession(); session.save(paste); } //HQL public List<Paste> findAllPaste() { Session session = getHibernateTemplate().getSessionFactory().getCurrentSession(); //書寫SQL語句 String hql = "form com.Gary.domain.Paste"; Query query = session.createQuery(hql); List<Paste> list = query.list(); return list; } }
配置struts.xml
<action name="GetDataAction_*" class="com.Gary.web.GetDataAction" method="{1}"> <result name="index">/index.jsp</result> </action>
配置application.xml
<!-- 配置Action --> <bean name="userAction" class="com.Gary.web.UserAction" scope="prototype"> <property name="userService" ref="userService"></property> </bean> <bean name="pasteAction" class="com.Gary.web.PasteAction" scope="prototype"> <property name="pasteService" ref="pasteService"></property> </bean> <bean name="getDataAction" class="com.Gary.web.GetDataAction" scope="prototype"> <property name="pasteService" ref="pasteService"></property> </bean>
測試發帖,動態編譯得到數據庫中的帖子數據
<div class="tab"> <s:iterator value="pasteList" var="paste"> <div class="dvques"> <div class="quesCount"> <div class="count">8</div> <div class="ques"> <s:property value="#paste.ansnum" /> </div> </div> <div class="quesContent"> <div class="quesTitle"> <s:property value="#paste.offer" /> <image src="images/bean.jpg" class="bean"> <span class="spanques"><s:property value="#paste.title" /></span> </div> <div class="qContent"><s:property value="#paste.content"/></div> <div class="tags"> <span class="tag">excel</span><span class="tag">程序</span> </div> <div class="quesUser"> <image src="images/0.gif" class="imguser" /> <div class="userName"> <s:property value="#paste.user.username"/> <div class="liulan">瀏覽(<s:property value="#paste.createtime"/>) <s:property value="#paste.user.username"/></div> </div> </div> </div> </div> </s:iterator> </div>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <link rel="stylesheet" type="text/css" href="css/index.css"> </head> <div class="dvhead"> <div class="dvlogo"> <a href="index.html">你問我答</a> </div> <div class="dvsearch">10秒鐘註冊帳號,找到你的同窗</div> <div class="dvreg"> 已有帳號,當即 <a href="login.html">登陸</a> </div> </div> <div class="dvContent"> <div class="dvquesleft"> <div class="dvqstitle"> <image class="imgbean" src="images/bean.jpg"> <span class="qsTitle">問答</span> <span class="back"><ab href="">《《返回上一頁</a></span> </div> <div class="dvtabhead"> <div class="tabheads tabcurrent">所有問題</div> <div class="tabheads">個人問題</div> <div class="tabheads">關注問題</div> <div class="tabheads">問題標籤</div> </div> <div class="tabContent"> <div class="dvtags"> <a class="curenttag">待解決</a><span class="line"></span><a>高分</a><span class="line"></span><a>新回答</a><span class="line"></span><a>已解決</a> </div> <div class="tab"> <s:iterator value="pasteList" var="paste"> <div class="dvques"> <div class="quesCount"> <div class="count">8</div> <div class="ques"> <s:property value="#paste.ansnum" /> </div> </div> <div class="quesContent"> <div class="quesTitle"> <s:property value="#paste.offer" /> <image src="images/bean.jpg" class="bean"> <span class="spanques"><s:property value="#paste.title" /></span> </div> <div class="qContent"><s:property value="#paste.content"/></div> <div class="tags"> <span class="tag">excel</span><span class="tag">程序</span> </div> <div class="quesUser"> <image src="images/0.gif" class="imguser" /> <div class="userName"> <s:property value="#paste.user.username"/> <div class="liulan">瀏覽(<s:property value="#paste.createtime"/>) <s:property value="#paste.user.username"/></div> </div> </div> </div> </div> </s:iterator> </div> <div class="tab hidden">2</div> <div class="tab hidden">3</div> <div class="tab hidden">4</div> </div> </div> <div class="dvquesright"> <div> <buton class="btnques" onclick="location.href='add.html'">提個問題</buton> </div> <div class="dvorder"> <div class="orderTitle">專家排行榜</div> <div class="users"> <image class="userface" src="images/0.gif" /> <div class="dvuser"> <div class="userTitle">陳有龍</div> <div class="userdeital">大牛6級 豆:14006</div> </div> </div> <div class="users"> <image class="userface" src="images/1.gif" /> <div class="dvuser"> <div class="userTitle">陳有龍</div> <div class="userdeital">大牛6級 豆:14006</div> </div> </div> <div class="users"> <image class="userface" src="images/2.gif" /> <div class="dvuser"> <div class="userTitle">陳有龍</div> <div class="userdeital">大牛6級 豆:14006</div> </div> </div> <div class="users"> <image class="userface" src="images/3.gif" /> <div class="dvuser"> <div class="userTitle">陳有龍</div> <div class="userdeital">大牛6級 豆:14006</div> </div> </div> <div class="users"> <image class="userface" src="images/4.gif" /> <div class="dvuser"> <div class="userTitle">陳有龍</div> <div class="userdeital">大牛6級 豆:14006</div> </div> </div> <div class="users"> <image class="userface" src="images/5.gif" /> <div class="dvuser"> <div class="userTitle">陳有龍</div> <div class="userdeital">大牛6級 豆:14006</div> </div> </div> <div class="users"> <image class="userface" src="images/6.gif" /> <div class="dvuser"> <div class="userTitle">陳有龍</div> <div class="userdeital">大牛6級 豆:14006</div> </div> </div> </div> </div> </div> <script type="text/javascript" src="js/jquery-1.7.2.min.js"></script> <script type="text/javascript"> $(function() { $(".tabheads").click( function() { $(".tabheads").removeClass("tabcurrent") .eq($(this).index()).addClass("tabcurrent"); $(".tab").hide().eq($(this).index()).show(); }); }); </script> <body> </body> </html>
修改帖子內容樣式
<div class="quesContent" style="width:px;height:px;overflow:hidden;white-space:normal;text-overflow:ellipsis;">
修改帖子頭像位置樣式
<div class="quesUser" style="margin-top:28px;margin-right:20px">
設置人物頭像
在用戶註冊時String register()方法中隨機設置用戶頭像
UserAction.java
public String register() throws Exception { // TODO Auto-generated method stub //沒有的數據手動封裝 user.setState(0); user.setCode(UUID.randomUUID().toString()); Random r = new Random(); user.setImage("/images/"+r.nextInt(21)+".gif"); user.setLevel(1); user.setCoin(1000); //是否添加成功 userService.addUser(user); MailUtils.sendMail(user.getEmail(), "請激活", "恭喜您註冊成功,請點擊下面的連接激活!! <a href='http://localhost:8080/Gary_SSHForum/UserAction_active?userCode="+user.getCode()+"'>點擊這裏</a>"); return "toRegisterSuccess"; }
提問區動態設置提問者頭像(index.jsp)
<div class="quesContent" style="width:px;height:px;overflow:hidden;white-space:normal;text-overflow:ellipsis;"> <div class="quesTitle"> <s:property value="#paste.offer" /> <image src="images/bean.jpg" class="bean"> <span class="spanques"><s:property value="#paste.title" /></span> </div> <div class="qContent"><s:property value="#paste.content"/></div> <div class="tags"> <span class="tag">excel</span><span class="tag">程序</span> </div> <div class="quesUser" style="margin-top:28px;margin-right:20px"> <image src="${pageContext.request.contextPath }/<s:property value="#paste.user.image"/>" class="imguser" /> <div class="userName"> <s:property value="#paste.user.username"/> <div class="liulan">瀏覽(<s:property value="#paste.createtime"/>) <s:property value="#paste.user.username"/></div> </div> </div>
設置登錄時登陸者頭像(head.jsp)
<!--描述:登陸的樣子--> <s:if test="#session.user!=null"> <a class="avatar" href=""> <img src="${pageContext.request.contextPath }/<s:property value="#session.user.image"/>"> <cite> <s:property value="#session.user.username" /> </cite> </a> </div> </s:if>
分頁Div
<div style="text-align: center"> <div class="laypage-main"><span class="laypage-curr">1</span><a href="/jie/page/2/">2</a><a href="/jie/page/3/">3</a><a href="/jie/page/4/">4</a><a href="/jie/page/5/">5</a><span>…</span><a href="/jie/page/148/" class="laypage-last" title="尾頁">尾頁</a><a href="/jie/page/2/" class="laypage-next">下一頁</a></div> </div>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <link rel="stylesheet" type="text/css" href="css/index.css"> </head> <body style="margin: -2px"> <iframe src="head.jsp" scrolling="no" width="100%" height="110px"></iframe> <div class="dvContent"> <div class="dvquesleft"> <div class="dvqstitle"> <image class="imgbean" src="images/bean.jpg"> <span class="qsTitle">問答</span> <span class="back"><ab href="">《《返回上一頁</a></span> </div> <div class="dvtabhead"> <div class="tabheads tabcurrent">所有問題</div> <div class="tabheads">個人問題</div> <div class="tabheads">關注問題</div> <div class="tabheads">問題標籤</div> </div> <div class="tabContent"> <div class="dvtags"> <a class="curenttag">待解決</a><span class="line"></span><a>高分</a><span class="line"></span><a>新回答</a><span class="line"></span><a>已解決</a> </div> <div class="tab"> <s:iterator value="pasteList" var="paste"> <div class="dvques"> <div class="quesCount"> <div class="count">8</div> <div class="ques"> <s:property value="#paste.ansnum" /> </div> </div> <div class="quesContent" style="width: px; height: px; overflow: hidden; white-space: normal; text-overflow: ellipsis;"> <div class="quesTitle"> <s:property value="#paste.offer" /> <image src="images/bean.jpg" class="bean"> <span class="spanques"><s:property value="#paste.title" /></span> </div> <div class="qContent"> <s:property value="#paste.content" /> </div> <div class="tags"> <span class="tag">excel</span><span class="tag">程序</span> </div> <div class="quesUser" style="margin-top: 28px; margin-right: 20px"> <image src="${pageContext.request.contextPath }/<s:property value="#paste.user.image"/>" class="imguser" /> <div class="userName"> <s:property value="#paste.user.username" /> <div class="liulan"> 瀏覽( <s:property value="#paste.createtime" /> ) <s:property value="#paste.user.username" /> </div> </div> </div> </div> </div> </s:iterator> <div style="text-align: center"> <div class="laypage-main"> <span class="laypage-curr">1</span><a href="/jie/page/2/">2</a><a href="/jie/page/3/">3</a><a href="/jie/page/4/">4</a><a href="/jie/page/5/">5</a><span>…</span><a href="/jie/page/148/" class="laypage-last" title="尾頁">尾頁</a><a href="/jie/page/2/" class="laypage-next">下一頁</a> </div> </div> </div> <div class="tab hidden">2</div> <div class="tab hidden">3</div> <div class="tab hidden">4</div> </div> </div> <div class="dvquesright"> <div> <buton class="btnques" onclick="location.href='add.jsp'">提個問題</buton> </div> <div class="dvorder"> <div class="orderTitle">專家排行榜</div> <div class="users"> <image class="userface" src="images/0.gif" /> <div class="dvuser"> <div class="userTitle">陳有龍</div> <div class="userdeital">大牛6級 豆:14006</div> </div> </div> <div class="users"> <image class="userface" src="images/1.gif" /> <div class="dvuser"> <div class="userTitle">陳有龍</div> <div class="userdeital">大牛6級 豆:14006</div> </div> </div> <div class="users"> <image class="userface" src="images/2.gif" /> <div class="dvuser"> <div class="userTitle">陳有龍</div> <div class="userdeital">大牛6級 豆:14006</div> </div> </div> <div class="users"> <image class="userface" src="images/3.gif" /> <div class="dvuser"> <div class="userTitle">陳有龍</div> <div class="userdeital">大牛6級 豆:14006</div> </div> </div> <div class="users"> <image class="userface" src="images/4.gif" /> <div class="dvuser"> <div class="userTitle">陳有龍</div> <div class="userdeital">大牛6級 豆:14006</div> </div> </div> <div class="users"> <image class="userface" src="images/5.gif" /> <div class="dvuser"> <div class="userTitle">陳有龍</div> <div class="userdeital">大牛6級 豆:14006</div> </div> </div> <div class="users"> <image class="userface" src="images/6.gif" /> <div class="dvuser"> <div class="userTitle">陳有龍</div> <div class="userdeital">大牛6級 豆:14006</div> </div> </div> </div> </div> </div> <script type="text/javascript" src="js/jquery-1.7.2.min.js"></script> <script type="text/javascript"> $(function() { $(".tabheads").click( function() { $(".tabheads").removeClass("tabcurrent").eq( $(this).index()).addClass("tabcurrent"); $(".tab").hide().eq($(this).index()).show(); }); }); </script> </body> </html>
分析四個參數
pageSize
currentPage
totalCount
totalPage
書寫PageBean.java
PageBean須要的屬性
//頁面大小 private Integer pageSize; //當前頁 private Integer currentPage; //總條數 private Integer totalCount; //總頁數 private Integer totalPage; //每一頁顯示的數據 private List list;
書寫PageBean構造方法
public PageBean(Integer currentPage,Integer totalCount,Integer pageSize) { this.currentPage = currentPage; this.totalCount = totalCount; this.pageSize=pageSize; //安全校驗 if(currentPage==null) { this.currentPage=1; } if(this.pageSize==null) { this.pageSize=8; } //計算totalPage this.totalPage=(int)Math.ceil(1.0*this.totalCount/this.pageSize); //安全校驗 if(this.currentPage>this.totalPage) { this.currentPage=this.totalPage; } if(this.currentPage<1) { this.currentPage=1; } }
書寫PageBean獲得起始索引的方法
public Integer getStart() { return (this.currentPage-1)*this.pageSize; }
分析PageBean
帖子Web層
public String getData() throws Exception { PageBean pastePageBean = pasteService.getPastePageBean(currentPage); ActionContext.getContext().put("pastePageBean", pastePageBean); return "index"; }
帖子Service層
public PageBean getPastePageBean(Integer currentPage) { Integer totalCount = pasteDao.findAllPasteNum(); PageBean pageBean = new PageBean(currentPage,totalCount,8); List<Paste> list = pasteDao.getPastePageList(pageBean.getStart(),pageBean.getPageSize()); pageBean.setList(list); return pageBean; }
得到數據庫中帖子Dao層
//查找全部帖子的數量 public Integer findAllPasteNum() { Session session = getHibernateTemplate().getSessionFactory().getCurrentSession(); String sql = "select count(*) from paste"; NativeQuery query = session.createSQLQuery(sql); BigInteger result = (BigInteger)query.uniqueResult(); return result.intValue(); } //分頁 public List<Paste> getPastePageList(Integer start, Integer pageSize) { Session session = getHibernateTemplate().getSessionFactory().getCurrentSession(); String sql = "select * from paste limit ?,?"; NativeQuery query = session.createSQLQuery(sql); query.addEntity(Paste.class); query.setParameter(1, start); query.setParameter(2, pageSize); List list = query.list(); return list; }
package com.Gary.utils; import java.util.List; public class PageBean { // pageSize // currentPage // totalCount // totalPage //頁面大小 private Integer pageSize; //當前頁 private Integer currentPage; //總條數 private Integer totalCount; //總頁數 private Integer totalPage; //每一頁顯示的數據 private List list; public PageBean(Integer currentPage,Integer totalCount,Integer pageSize) { this.currentPage = currentPage; this.totalCount = totalCount; this.pageSize = pageSize; //安全校驗 if(this.currentPage == null) { this.currentPage = 1; } if(this.pageSize == null) { this.pageSize = 8; } //計算totalPage this.totalPage = (int) Math.ceil(1.0 * this.totalCount/this.pageSize);//全部的條數之和除以每頁顯示的條數向上取整 //安全校驗 if(this.currentPage > this.totalPage) { this.currentPage = this.totalPage; } if(this.currentPage < 1) { this.currentPage = 1; } } //得到起始索引 public Integer getStart() { return (this.currentPage-1)*this.pageSize;//(第一個數-1)*第二個數 } public Integer getPageSize() { return pageSize; } public void setPageSize(Integer pageSize) { this.pageSize = pageSize; } public Integer getCurrentPage() { return currentPage; } public void setCurrentPage(Integer currentPage) { this.currentPage = currentPage; } public Integer getTotalCount() { return totalCount; } public void setTotalCount(Integer totalCount) { this.totalCount = totalCount; } public Integer getTotalPage() { return totalPage; } public void setTotalPage(Integer totalPage) { this.totalPage = totalPage; } public List getList() { return list; } public void setList(List list) { this.list = list; } }
package com.Gary.web; import java.util.List; import com.Gary.domain.Paste; import com.Gary.service.PasteService; import com.Gary.utils.PageBean; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class GetDataAction extends ActionSupport{ private PasteService pasteService; private Integer currentPage; public String getData() throws Exception { PageBean pastePageBean = pasteService.getPastePageBean(currentPage); ActionContext.getContext().put("pastePageBean", pastePageBean); return "index"; } public PasteService getPasteService() { return pasteService; } public void setPasteService(PasteService pasteService) { this.pasteService = pasteService; } public Integer getCurrentPage() { return currentPage; } public void setCurrentPage(Integer currentPage) { this.currentPage = currentPage; } }
package com.Gary.service; import java.util.List; import com.Gary.dao.PasteDao; import com.Gary.domain.Paste; import com.Gary.utils.PageBean; public class PasteService { private PasteDao pasteDao; public PageBean getPastePageBean(Integer currentPage) { Integer totalCount = pasteDao.findAllPasteNum(); PageBean pageBean = new PageBean(currentPage,totalCount,8); List<Paste> list = pasteDao.getPastePageList(pageBean.getStart(),pageBean.getPageSize()); pageBean.setList(list); return pageBean; } public PasteDao getPasteDao() { return pasteDao; } public List<Paste> findAllPaste() { return pasteDao.findAllPaste(); } public void setPasteDao(PasteDao pasteDao) { this.pasteDao = pasteDao; } public void addPaste(Paste paste) { pasteDao.addPaste(paste); } }
package com.Gary.dao; import java.math.BigInteger; import java.util.List; import org.hibernate.Session; import org.hibernate.query.NativeQuery; import org.hibernate.query.Query; import org.springframework.orm.hibernate5.support.HibernateDaoSupport; import com.Gary.domain.Paste; public class PasteDao extends HibernateDaoSupport{ public void addPaste(Paste paste) { Session session = getHibernateTemplate().getSessionFactory().getCurrentSession(); session.save(paste); } //HQL public List<Paste> findAllPaste() { Session session = getHibernateTemplate().getSessionFactory().getCurrentSession(); //書寫SQL語句 String hql = "from com.Gary.domain.Paste"; Query query = session.createQuery(hql); List<Paste> list = query.list(); return list; } //查找全部帖子的數量 public Integer findAllPasteNum() { Session session = getHibernateTemplate().getSessionFactory().getCurrentSession(); String sql = "select count(*) from paste"; NativeQuery query = session.createSQLQuery(sql); BigInteger result = (BigInteger)query.uniqueResult(); return result.intValue(); } //分頁 public List<Paste> getPastePageList(Integer start, Integer pageSize) { Session session = getHibernateTemplate().getSessionFactory().getCurrentSession(); String sql = "select * from paste limit ?,?"; NativeQuery query = session.createSQLQuery(sql); query.addEntity(Paste.class); query.setParameter(1, start); query.setParameter(2, pageSize); List list = query.list(); return list; } }
修改分頁Div
<div style="text-align: center"> <div class="laypage-main"> <a href="/jie/page/148/" class="laypage-last" title="尾頁">首頁</a> <a href="/jie/page/2/" class="laypage-next">上一頁</a> <span class="laypage-curr">1</span> <a href="/jie/page/2/">2</a> <a href="/jie/page/3/">3</a> <a href="/jie/page/4/">4</a> <a href="/jie/page/5/">5</a> <span>…</span> <a href="/jie/page/148/" class="laypage-last" title="尾頁">尾頁</a> <a href="/jie/page/2/" class="laypage-next">下一頁</a> </div> </div>
動態實現上一頁,下一頁跳轉
<div style="text-align: center"> <div class="laypage-main"> <a href="/jie/page/148/" class="laypage-last" title="首頁">首頁</a> <a href="${pageContext.request.contextPath}/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage - 1"/>" class="laypage-next">上一頁</a> <span class="laypage-curr">1</span> <a href="/jie/page/2/">2</a> <a href="/jie/page/3/">3</a> <a href="/jie/page/4/">4</a> <a href="/jie/page/5/">5</a> <span>…</span> <a href="${pageContext.request.contextPath}/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage + 1"/>" class="laypage-next">下一頁</a> <a href="/jie/page/148/" class="laypage-last" title="尾頁">尾頁</a> </div> </div>
實現分頁菜單欄
<div style="text-align: center"> <div class="laypage-main"> <a href="${pageContext.request.contextPath}/GetDataAction_getData?currentPage=1" class="laypage-last" title="首頁">首頁</a> <a href="${pageContext.request.contextPath}/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage - 1"/>" class="laypage-next">上一頁</a> <s:if test="#pastePageBean.currentPage-3>0"> <span>…</span> </s:if> <s:if test="#pastePageBean.currentPage - 2>0"> <a href="${pageContext.request.contextPath}/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage - 2"/>"><s:property value="#pastePageBean.currentPage-2"/></a> </s:if> <s:if test="#pastePageBean.currentPage - 1>0"> <a href="${pageContext.request.contextPath}/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage - 1"/>"><s:property value="#pastePageBean.currentPage-1"/></a> </s:if> <!-- 當前頁 --> <span class="laypage-curr"> <s:property value="#pastePageBean.currentPage"/> </span> <s:if test="#pastePageBean.currentPage <#pastePageBean.totalPage"> <a href="${pageContext.request.contextPath}/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage + 1"/>"><s:property value="#pastePageBean.currentPage+1"/></a> </s:if> <s:if test="#pastePageBean.currentPage + 1<#pastePageBean.totalPage"> <a href="${pageContext.request.contextPath}/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage + 2"/>"><s:property value="#pastePageBean.currentPage+2"/></a> </s:if> <s:if test="#pastePageBean.currentPage + 2<#pastePageBean.totalPage"> <span>…</span> </s:if> <a href="${pageContext.request.contextPath}/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage + 1"/>" class="laypage-next">下一頁</a> <a href="${pageContext.request.contextPath}/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.totalPage"/>" class="laypage-last" title="尾頁">尾頁</a> </div> </div>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <link rel="stylesheet" type="text/css" href="css/index.css"> </head> <body style="margin: -2px"> <iframe src="head.jsp" scrolling="no" width="100%" height="110px"></iframe> <div class="dvContent"> <div class="dvquesleft"> <div class="dvqstitle"> <image class="imgbean" src="images/bean.jpg"> <span class="qsTitle">問答</span> <span class="back"><ab href="">《《返回上一頁</a></span> </div> <div class="dvtabhead"> <div class="tabheads tabcurrent">所有問題</div> <div class="tabheads">個人問題</div> <div class="tabheads">關注問題</div> <div class="tabheads">問題標籤</div> </div> <div class="tabContent"> <div class="dvtags"> <a class="curenttag">待解決</a><span class="line"></span><a>高分</a><span class="line"></span><a>新回答</a><span class="line"></span><a>已解決</a> </div> <div class="tab"> <s:iterator value="#pastePageBean.list" var="paste"> <div class="dvques"> <div class="quesCount"> <div class="count">8</div> <div class="ques"> <s:property value="#paste.ansnum" /> </div> </div> <div class="quesContent" style="width: px; height: px; overflow: hidden; white-space: normal; text-overflow: ellipsis;"> <div class="quesTitle"> <s:property value="#paste.offer" /> <image src="images/bean.jpg" class="bean"> <span class="spanques"><s:property value="#paste.title" /></span> </div> <div class="qContent"> <s:property value="#paste.content" /> </div> <div class="tags"> <span class="tag">excel</span><span class="tag">程序</span> </div> <div class="quesUser" style="margin-top: 28px; margin-right: 20px"> <image src="${pageContext.request.contextPath }/<s:property value="#paste.user.image"/>" class="imguser" /> <div class="userName"> <s:property value="#paste.user.username" /> <div class="liulan"> 瀏覽( <s:property value="#paste.createtime" /> ) <s:property value="#paste.user.username" /> </div> </div> </div> </div> </div> </s:iterator> <div style="text-align: center"> <div class="laypage-main"> <a href="${pageContext.request.contextPath}/GetDataAction_getData?currentPage=1" class="laypage-last" title="首頁">首頁</a> <a href="${pageContext.request.contextPath}/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage - 1"/>" class="laypage-next">上一頁</a> <s:if test="#pastePageBean.currentPage-3>0"> <span>…</span> </s:if> <s:if test="#pastePageBean.currentPage - 2>0"> <a href="${pageContext.request.contextPath}/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage - 2"/>"><s:property value="#pastePageBean.currentPage-2"/></a> </s:if> <s:if test="#pastePageBean.currentPage - 1>0"> <a href="${pageContext.request.contextPath}/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage - 1"/>"><s:property value="#pastePageBean.currentPage-1"/></a> </s:if> <!-- 當前頁 --> <span class="laypage-curr"> <s:property value="#pastePageBean.currentPage"/> </span> <s:if test="#pastePageBean.currentPage <#pastePageBean.totalPage"> <a href="${pageContext.request.contextPath}/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage + 1"/>"><s:property value="#pastePageBean.currentPage+1"/></a> </s:if> <s:if test="#pastePageBean.currentPage + 1<#pastePageBean.totalPage"> <a href="${pageContext.request.contextPath}/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage + 2"/>"><s:property value="#pastePageBean.currentPage+2"/></a> </s:if> <s:if test="#pastePageBean.currentPage + 2<#pastePageBean.totalPage"> <span>…</span> </s:if> <a href="${pageContext.request.contextPath}/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage + 1"/>" class="laypage-next">下一頁</a> <a href="${pageContext.request.contextPath}/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.totalPage"/>" class="laypage-last" title="尾頁">尾頁</a> </div> </div> </div> <div class="tab hidden">2</div> <div class="tab hidden">3</div> <div class="tab hidden">4</div> </div> </div> <div class="dvquesright"> <div> <buton class="btnques" onclick="location.href='add.jsp'">提個問題</buton> </div> <div class="dvorder"> <div class="orderTitle">專家排行榜</div> <div class="users"> <image class="userface" src="images/0.gif" /> <div class="dvuser"> <div class="userTitle">陳有龍</div> <div class="userdeital">大牛6級 豆:14006</div> </div> </div> <div class="users"> <image class="userface" src="images/1.gif" /> <div class="dvuser"> <div class="userTitle">陳有龍</div> <div class="userdeital">大牛6級 豆:14006</div> </div> </div> <div class="users"> <image class="userface" src="images/2.gif" /> <div class="dvuser"> <div class="userTitle">陳有龍</div> <div class="userdeital">大牛6級 豆:14006</div> </div> </div> <div class="users"> <image class="userface" src="images/3.gif" /> <div class="dvuser"> <div class="userTitle">陳有龍</div> <div class="userdeital">大牛6級 豆:14006</div> </div> </div> <div class="users"> <image class="userface" src="images/4.gif" /> <div class="dvuser"> <div class="userTitle">陳有龍</div> <div class="userdeital">大牛6級 豆:14006</div> </div> </div> <div class="users"> <image class="userface" src="images/5.gif" /> <div class="dvuser"> <div class="userTitle">陳有龍</div> <div class="userdeital">大牛6級 豆:14006</div> </div> </div> <div class="users"> <image class="userface" src="images/6.gif" /> <div class="dvuser"> <div class="userTitle">陳有龍</div> <div class="userdeital">大牛6級 豆:14006</div> </div> </div> </div> </div> </div> <script type="text/javascript" src="js/jquery-1.7.2.min.js"></script> <script type="text/javascript"> $(function() { $(".tabheads").click( function() { $(".tabheads").removeClass("tabcurrent").eq( $(this).index()).addClass("tabcurrent"); $(".tab").hide().eq($(this).index()).show(); }); }); </script> </body> </html>
分析最近熱議模塊
將數據傳遞給前端
最近熱帖Web層
public String getData() throws Exception { //獲得帖子 PageBean pastePageBean = pasteService.getPastePageBean(currentPage); ActionContext.getContext().put("pastePageBean", pastePageBean); //獲得最近熱帖 PageBean glanceoverPagebean = pasteService.getGlanceoverPageBean(currentPage); ActionContext.getContext().put("glanceoverPagebean", glanceoverPagebean); return "index"; }
最近熱帖Service層
public PageBean getGlanceoverPageBean(Integer currentPage) { Integer totalCount = pasteDao.findAllPasteNum(); PageBean glanceoverPageBean = new PageBean(currentPage,totalCount,8); List<Paste> list = pasteDao.getGlanceoverPageList(); glanceoverPageBean.setList(list); return glanceoverPageBean; } public PageBean getPastePageBean(Integer currentPage) { Integer totalCount = pasteDao.findAllPasteNum(); PageBean pageBean = new PageBean(currentPage,totalCount,8); List<Paste> list = pasteDao.getPastePageList(pageBean.getStart(),pageBean.getPageSize()); pageBean.setList(list); return pageBean; }
最近熱帖Dao層
//最近熱議 public List<Paste> getGlanceoverPageList() { Session session = getHibernateTemplate().getSessionFactory().getCurrentSession(); String sql = "select * from paste order by offer desc limit 0,8"; NativeQuery query = session.createSQLQuery(sql); query.addEntity(Paste.class); List list = query.list(); return null; }
動態得到數據庫中熱帖數據
<dt class="fly-panel-title">最近熱帖</dt> <s:iterator value="#glanceoverPageBean.list" var="paste"> <dd> <a href="${pageContext.request.contextPath }/PasteAction_getDetail?pasteid=<s:property value="#paste.id"/>"> <s:property value="#paste.title" /> </a> <span> <i class="iconfont"></i> <s:property value="#paste.glanceover" /> </span> </dd> </s:iterator>
分析最近熱議模塊
書寫最近熱議後端代碼
最近熱議Web層
public String getData() throws Exception { //獲得帖子 PageBean pastePageBean = pasteService.getPastePageBean(currentPage); ActionContext.getContext().put("pastePageBean", pastePageBean); //獲得最近熱帖 PageBean glanceoverPageBean = pasteService.getGlanceoverPageBean(currentPage); ActionContext.getContext().put("glanceoverPageBean", glanceoverPageBean); //獲得最近熱議 PageBean ansnumPageBean = pasteService.getAnsnumPageBean(currentPage); ActionContext.getContext().put("ansnumPageBean", ansnumPageBean); return "index"; }
最近熱議Service層
public PageBean getAnsnumPageBean(Integer currentPage) { //得到全部的帖子數目 Integer totalCount = pasteDao.findAllPasteNum(); //建立PageBean PageBean ansnumPageBean = new PageBean(currentPage, totalCount, 8); //獲得List List<Paste> list = pasteDao.getAnsnumPageList(); //封裝List ansnumPageBean.setList(list); return ansnumPageBean; }
Dao層得到數據庫中的數據
public List<Paste> getAnsnumPageList() { //獲得與當前線程綁定的session Session session = getHibernateTemplate().getSessionFactory().getCurrentSession(); //書寫sql String sql = "select * from paste order by ansnum desc limit 0,8"; //得到query NativeQuery query = session.createSQLQuery(sql); //將結果集封裝爲Paste對象 query.addEntity(Paste.class); //得到sql的執行結果(結果爲list) List list = query.list(); return list; }
編寫最近熱議前端
<dl class="fly-panel fly-list-one"> <dt class="fly-panel-title">近期熱議</dt> <s:iterator value="#ansnumPageBean.list" var="paste"> <dd> <a href="${pageContext.request.contextPath }/PasteAction_getDetail?pasteid=<s:property value="#paste.id"/>"> <s:property value="#paste.title" /> </a> <span> <i class="iconfont"></i> <s:property value="#paste.ansnum" /> </span> </dd> </s:iterator> </dl>
查詢專家排行榜
編寫專家排行榜
專家排行榜Web層
private UserService userService; public String getData() throws Exception { //獲得帖子 PageBean pastePageBean = pasteService.getPastePageBean(currentPage); ActionContext.getContext().put("pastePageBean", pastePageBean); //獲得最近熱帖 PageBean glanceoverPageBean = pasteService.getGlanceoverPageBean(currentPage); ActionContext.getContext().put("glanceoverPageBean", glanceoverPageBean); //獲得最近熱議 PageBean ansnumPageBean = pasteService.getAnsnumPageBean(currentPage); ActionContext.getContext().put("ansnumPageBean", ansnumPageBean); //獲得專家排行 PageBean userPageBean = userService.getUserPageBean(currentPage); ActionContext.getContext().put("userPageBean", userPageBean); return "index"; }
專家排行榜Service層
public PageBean getUserPageBean(Integer currentPage) { Integer totalCount = userDao.findAllUserNum(); PageBean userPageBean = new PageBean(currentPage, totalCount, 8); List<User> list = userDao.getUserPageBean(); userPageBean.setList(list); return userPageBean; }
專家排行榜Dao層
public Integer findAllUserNum() { Session session = getHibernateTemplate().getSessionFactory().getCurrentSession(); String sql = "select count(*) from user"; NativeQuery query = session.createSQLQuery(sql); BigInteger result = (BigInteger) query.uniqueResult(); return result.intValue(); } public List<User> getUserPageBean() { Session session = getHibernateTemplate().getSessionFactory().getCurrentSession(); String sql = "select * from user order by concat(coin,level) desc limit 0,8"; NativeQuery query = session.createSQLQuery(sql); query.addEntity(User.class); List list = query.list(); return list; }
專家排行榜前端
<div class="orderTitle">專家排行榜</div> <s:iterator value="#userPageBean.list" var="user"> <div class="users"> <image class="userface" src="${pageContext.request.contextPath }/<s:property value="#user.image"/>" /> <div class="dvuser"> <div class="userTitle"> <s:property value="#user.username" /> </div> <div class="userdeital"> 大牛 <s:property value="#user.level" /> 級 豆: <s:property value="#user.coin" /> </div> </div> </div> </s:iterator>
修改web.xml,實現發佈自動跳轉到得到數據庫中的數據
<display-name>Gary_SSHForum</display-name> <welcome-file-list> <welcome-file>default.jsp</welcome-file> </welcome-file-list>
建立一個default.jsp實現函數頁面跳轉
<body> <% response.sendRedirect(request.getContextPath()+"/GetDataAction_getData"); %> </body>
修改分頁標籤樣式格式
<div style="text-align: center"> <div class="laypage-main"> <a href="${pageContext.request.contextPath }/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage-1"/>" class="laypage-next">上一頁</a> <a href="${pageContext.request.contextPath }/GetDataAction_getData?currentPage=1" class="laypage-last" title="尾頁">首頁</a> <s:if test="#pastePageBean.currentPage-3>0"> <span>…</span> </s:if> <s:if test="#pastePageBean.currentPage-2>0"> <a href="${pageContext.request.contextPath }/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage-2"/>"> <s:property value="#pastePageBean.currentPage-2" /> </a> </s:if> <s:if test="#pastePageBean.currentPage-1>0"> <a href="${pageContext.request.contextPath }/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage-1"/>"> <s:property value="#pastePageBean.currentPage-1" /> </a> </s:if> <!-- 當前頁 --> <span class="laypage-curr"> <s:property value="#pastePageBean.currentPage" /> </span> <s:if test="#pastePageBean.currentPage<#pastePageBean.totalPage"> <a href="${pageContext.request.contextPath }/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage+1"/>"> <s:property value="#pastePageBean.currentPage+1" /> </a> </s:if> <s:if test="#pastePageBean.currentPage+1<#pastePageBean.totalPage"> <a href="${pageContext.request.contextPath }/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage+2"/>"> <s:property value="#pastePageBean.currentPage+2" /> </a> </s:if> <s:if test="#pastePageBean.currentPage+2<#pastePageBean.totalPage"> <span>…</span> </s:if> <a href="${pageContext.request.contextPath }/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.totalPage"/>" class="laypage-last" title="尾頁">尾頁</a> <a href="${pageContext.request.contextPath }/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage+1"/>" class="laypage-next">下一頁</a> </div> </div>
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>Gary_SSHForum</display-name> <welcome-file-list> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!-- 讓spring隨着Web項目的啓動而啓動 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 讀取配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 擴大session的範圍 --> <filter> <filter-name>openSession</filter-name> <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>openSession</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 讓struts啓動 --> <filter> <filter-name>struts</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <!--開啓動態方法調用 --> <constant name="struts.devMode" value="true"></constant> <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant> <!-- 告訴struts不用本身建立Action,Spring來幫你建立 --> <constant name="struts.objectFactory" value="spring"></constant> <package name="Gary_SSHForum" namespace="/" extends="struts-default"> <!-- 容許所有方法 --> <global-allowed-methods>regex:.*</global-allowed-methods> <action name="UserAction_*" class="com.Gary.web.UserAction" method="{1}"> <result name="toLogin" type="redirect">/login.jsp</result> <result name="login">/login.jsp</result> <result name="toIndex" type="redirect">/index.jsp</result> <result name="error">/login.jsp</result> <result name="toRegisterSuccess" type="redirect">/registerSuccess.jsp</result> </action> <action name="PasteAction_*" class="com.Gary.web.PasteAction" method="{1}"> <result name="toIndex" type="redirect">/index.jsp</result> <result name="error">/login.jsp</result> </action> <action name="GetDataAction_*" class="com.Gary.web.GetDataAction" method="{1}"> <result name="index" >/index.jsp</result> </action> </package> </struts>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 配置數據源 --> <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="jdbcUrl" value="jdbc:mysqL:///garyssh_forum"></property> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="user" value="root"></property> <property name="password" value="123456"></property> </bean> <!-- 配置sessionFactory --> <bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sqp">true</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> <property name="mappingDirectoryLocations" value="classpath:com/Gary/domain"></property> </bean> <!-- 配置事務的核心管理器 --> <bean name="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 通知 --> <tx:advice id="advice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*" /> </tx:attributes> </tx:advice> <!-- 織入 --> <aop:config> <!-- 切入點 --> <aop:pointcut expression="execution(* com.Gary.service.*.*(..))" id="pc" /> <!-- 配置切面 切入點+通知 --> <aop:advisor advice-ref="advice" pointcut-ref="pc" /> </aop:config> <!-- 配置Action --> <bean name="userAction" class="com.Gary.web.UserAction" scope="prototype"> <property name="userService" ref="userService"></property> </bean> <bean name="pasteAction" class="com.Gary.web.PasteAction" scope="prototype"> <property name="pasteService" ref="pasteService"></property> </bean> <bean name="getDataAction" class="com.Gary.web.GetDataAction" scope="prototype"> <property name="pasteService" ref="pasteService"></property> <property name="userService" ref="userService"></property> </bean> <!-- 配置Service --> <bean name="userService" class="com.Gary.service.UserService"> <property name="userDao" ref="userDao"></property> </bean> <bean name="pasteService" class="com.Gary.service.PasteService"> <property name="pasteDao" ref="pasteDao"></property> </bean> <!-- 配置Dao --> <bean name="userDao" class="com.Gary.dao.UserDao"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean name="pasteDao" class="com.Gary.dao.PasteDao"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> </beans>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <link rel="stylesheet" type="text/css" href="css/index.css"> <link rel="stylesheet" href="res/css/global.css"> </head> <body style="margin: -2px"> <iframe src="head.jsp" scrolling="no" width="100%" height="110px"></iframe> <div class="dvContent"> <div class="dvquesleft"> <div class="dvqstitle"> <image class="imgbean" src="images/bean.jpg"> <span class="qsTitle">問答</span> <span class="back"> <ab href="">《《返回上一頁</a> </span> </div> <div class="dvtabhead"> <div class="tabheads tabcurrent">所有問題</div> <div class="tabheads">個人問題</div> <div class="tabheads">關注問題</div> <div class="tabheads">問題標籤</div> </div> <div class="tabContent"> <div class="dvtags"> <a class="curenttag">待解決</a> <span class="line"></span> <a>高分</a> <span class="line"></span> <a>新回答</a> <span class="line"></span> <a>已解決</a> </div> <div class="tab"> <s:iterator value="#pastePageBean.list" var="paste"> <div class="dvques"> <div class="quesCount"> <div class="count"> <s:property value="#paste.ansnum" /> </div> <div class="ques">回答數</div> </div> <div class="quesContent"> <div class="quesTitle"> <s:property value="#paste.offer" /> <image src="images/bean.jpg" class="bean"> <span class="spanques"> <a href="${pageContext.request.contextPath }/PasteAction_getDetail?pasteid=<s:property value="#paste.id"/>"> <s:property value="#paste.title" /> </a> </span> </div> <div class="qContent" style="width: 630px; height: 34px; overflow: hidden; white-space: normal; text-overflow: ellipsis;"> <p> <s:property value="#paste.content" /> </p> </div> <div class="tags"> <span class="tag">excel</span> <span class="tag">程序</span> </div> <div class="quesUser" style="margin-top: 20px; margin-right: 20px"> <image src="${pageContext.request.contextPath }/<s:property value="#paste.user.image"/>" class="imguser" /> <div class="userName"> <s:property value="#paste.user.username" /> <div class="liulan"> 瀏覽( <s:property value="#paste.glanceover" /> ) <s:property value="#paste.createtime" /> </div> </div> </div> </div> </div> </s:iterator> <div style="text-align: center"> <div class="laypage-main"> <a href="${pageContext.request.contextPath }/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage-1"/>" class="laypage-next">上一頁</a> <a href="${pageContext.request.contextPath }/GetDataAction_getData?currentPage=1" class="laypage-last" title="尾頁">首頁</a> <s:if test="#pastePageBean.currentPage-3>0"> <span>…</span> </s:if> <s:if test="#pastePageBean.currentPage-2>0"> <a href="${pageContext.request.contextPath }/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage-2"/>"> <s:property value="#pastePageBean.currentPage-2" /> </a> </s:if> <s:if test="#pastePageBean.currentPage-1>0"> <a href="${pageContext.request.contextPath }/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage-1"/>"> <s:property value="#pastePageBean.currentPage-1" /> </a> </s:if> <!-- 當前頁 --> <span class="laypage-curr"> <s:property value="#pastePageBean.currentPage" /> </span> <s:if test="#pastePageBean.currentPage<#pastePageBean.totalPage"> <a href="${pageContext.request.contextPath }/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage+1"/>"> <s:property value="#pastePageBean.currentPage+1" /> </a> </s:if> <s:if test="#pastePageBean.currentPage+1<#pastePageBean.totalPage"> <a href="${pageContext.request.contextPath }/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage+2"/>"> <s:property value="#pastePageBean.currentPage+2" /> </a> </s:if> <s:if test="#pastePageBean.currentPage+2<#pastePageBean.totalPage"> <span>…</span> </s:if> <a href="${pageContext.request.contextPath }/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.totalPage"/>" class="laypage-last" title="尾頁">尾頁</a> <a href="${pageContext.request.contextPath }/GetDataAction_getData?currentPage=<s:property value="#pastePageBean.currentPage+1"/>" class="laypage-next">下一頁</a> </div> </div> </div> <div class="tab hidden">2</div> <div class="tab hidden">3</div> <div class="tab hidden">4</div> </div> </div> <div class="dvquesright"> <div> <buton class="btnques" onclick="location.href='add.jsp'">提個問題</buton> </div> <div class="dvorder"> <dl class="fly-panel fly-list-one"> <dt class="fly-panel-title">最近熱帖</dt> <s:iterator value="#glanceoverPageBean.list" var="paste"> <dd> <a href="${pageContext.request.contextPath }/PasteAction_getDetail?pasteid=<s:property value="#paste.id"/>"> <s:property value="#paste.title" /> </a> <span> <i class="iconfont"></i> <s:property value="#paste.glanceover" /> </span> </dd> </s:iterator> </dl> <dl class="fly-panel fly-list-one"> <dt class="fly-panel-title">近期熱議</dt> <s:iterator value="#ansnumPageBean.list" var="paste"> <dd> <a href="${pageContext.request.contextPath }/PasteAction_getDetail?pasteid=<s:property value="#paste.id"/>"> <s:property value="#paste.title" /> </a> <span> <i class="iconfont"></i> <s:property value="#paste.ansnum" /> </span> </dd> </s:iterator> </dl> <div class="orderTitle">專家排行榜</div> <s:iterator value="#userPageBean.list" var="user"> <div class="users"> <image class="userface" src="${pageContext.request.contextPath }/<s:property value="#user.image"/>" /> <div class="dvuser"> <div class="userTitle"> <s:property value="#user.username" /> </div> <div class="userdeital"> 大牛 <s:property value="#user.level" /> 級 豆: <s:property value="#user.coin" /> </div> </div> </div> </s:iterator> </div> </div> </div> <script type="text/javascript" src="js/jquery-1.7.2.min.js"></script> <script type="text/javascript"> $(function() { $(".tabheads").click( function() { $(".tabheads").removeClass("tabcurrent").eq( $(this).index()).addClass("tabcurrent"); $(".tab").hide().eq($(this).index()).show(); }); }); </script> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% response.sendRedirect(request.getContextPath()+"/GetDataAction_getData"); %> </body> </html>
package com.Gary.utils; import java.util.List; public class PageBean { // pageSize // currentPage // totalCount // totalPage //頁面大小 private Integer pageSize; //當前頁 private Integer currentPage; //總條數 private Integer totalCount; //總頁數 private Integer totalPage; //每一頁顯示的數據 private List list; public PageBean(Integer currentPage,Integer totalCount,Integer pageSize) { this.currentPage = currentPage; this.totalCount = totalCount; this.pageSize = pageSize; //安全校驗 if(this.currentPage == null) { this.currentPage = 1; } if(this.pageSize == null) { this.pageSize = 8; } //計算totalPage this.totalPage = (int) Math.ceil(1.0 * this.totalCount/this.pageSize);//全部的條數之和除以每頁顯示的條數向上取整 //安全校驗 if(this.currentPage > this.totalPage) { this.currentPage = this.totalPage; } if(this.currentPage < 1) { this.currentPage = 1; } } //得到起始索引 public Integer getStart() { return (this.currentPage-1)*this.pageSize;//(第一個數-1)*第二個數 } public Integer getPageSize() { return pageSize; } public void setPageSize(Integer pageSize) { this.pageSize = pageSize; } public Integer getCurrentPage() { return currentPage; } public void setCurrentPage(Integer currentPage) { this.currentPage = currentPage; } public Integer getTotalCount() { return totalCount; } public void setTotalCount(Integer totalCount) { this.totalCount = totalCount; } public Integer getTotalPage() { return totalPage; } public void setTotalPage(Integer totalPage) { this.totalPage = totalPage; } public List getList() { return list; } public void setList(List list) { this.list = list; } }
package com.Gary.web; import java.util.List; import com.Gary.domain.Paste; import com.Gary.service.PasteService; import com.Gary.service.UserService; import com.Gary.utils.PageBean; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class GetDataAction extends ActionSupport{ private PasteService pasteService; private UserService userService; public UserService getUserService() { return userService; } public void setUserService(UserService userService) { this.userService = userService; } private Integer currentPage; public String getData() throws Exception { //獲得帖子 PageBean pastePageBean = pasteService.getPastePageBean(currentPage); ActionContext.getContext().put("pastePageBean", pastePageBean); //獲得最近熱帖 PageBean glanceoverPageBean = pasteService.getGlanceoverPageBean(currentPage); ActionContext.getContext().put("glanceoverPageBean", glanceoverPageBean); //獲得最近熱議 PageBean ansnumPageBean = pasteService.getAnsnumPageBean(currentPage); ActionContext.getContext().put("ansnumPageBean", ansnumPageBean); //獲得專家排行 PageBean userPageBean = userService.getUserPageBean(currentPage); ActionContext.getContext().put("userPageBean", userPageBean); return "index"; } public PasteService getPasteService() { return pasteService; } public void setPasteService(PasteService pasteService) { this.pasteService = pasteService; } public Integer getCurrentPage() { return currentPage; } public void setCurrentPage(Integer currentPage) { this.currentPage = currentPage; } }
package com.Gary.service; import java.util.List; import com.Gary.dao.PasteDao; import com.Gary.domain.Paste; import com.Gary.utils.PageBean; public class PasteService { private PasteDao pasteDao; public PageBean getGlanceoverPageBean(Integer currentPage) { Integer totalCount = pasteDao.findAllPasteNum(); PageBean glanceoverPageBean = new PageBean(currentPage, totalCount, 8); List<Paste> list = pasteDao.getGlanceoverPageList(); glanceoverPageBean.setList(list); return glanceoverPageBean; } public PageBean getPastePageBean(Integer currentPage) { Integer totalCount = pasteDao.findAllPasteNum(); PageBean pageBean = new PageBean(currentPage,totalCount,8); List<Paste> list = pasteDao.getPastePageList(pageBean.getStart(),pageBean.getPageSize()); pageBean.setList(list); return pageBean; } public PasteDao getPasteDao() { return pasteDao; } public List<Paste> findAllPaste() { return pasteDao.findAllPaste(); } public void setPasteDao(PasteDao pasteDao) { this.pasteDao = pasteDao; } public void addPaste(Paste paste) { pasteDao.addPaste(paste); } public PageBean getAnsnumPageBean(Integer currentPage) { //得到全部的帖子數目 Integer totalCount = pasteDao.findAllPasteNum(); //建立PageBean PageBean ansnumPageBean = new PageBean(currentPage, totalCount, 8); //獲得List List<Paste> list = pasteDao.getAnsnumPageList(); //封裝List ansnumPageBean.setList(list); return ansnumPageBean; } }
package com.Gary.service; import java.math.BigInteger; import java.util.List; import com.Gary.dao.UserDao; import com.Gary.domain.User; import com.Gary.utils.PageBean; public class UserService { private UserDao userDao; public PageBean getUserPageBean(Integer currentPage) { Integer totalCount = userDao.findAllUserNum(); PageBean userPageBean = new PageBean(currentPage, totalCount, 8); List<User> list = userDao.getUserPageBean(); userPageBean.setList(list); return userPageBean; } public User findUserByUsernameReturnUser(User user) { return userDao.findUserByUsernameReturnUser(user); } public int checkUser(User user) { User temp = userDao.findUserByUsernameReturnUser(user); //用戶名不存在 if(temp==null) { return 1; } //判斷密碼是否相同 if(temp.getPassword().equals(user.getPassword())) { if(temp.getState()==1) { //登錄成功 return 0; }else { //沒有激活 return 3; } }else { //密碼錯誤 return 2; } } public UserDao getUserDao() { return userDao; } public void activeUser(String userCode) { // TODO Auto-generated method stub userDao.activeUser(userCode); } public void setUserDao(UserDao userDao) { this.userDao = userDao; } public void addUser(User user) { // TODO Auto-generated method stub userDao.addUser(user); } public boolean findUserByUsername(String username) { // TODO Auto-generated method stub Long count = userDao.findUserByUsernameReturnNum(username); if(count==0) return true; else return false; } }
package com.Gary.domain; public class Paste { private String id; private String title; private String content; private Integer offer; private Integer ansnum; private String createtime; private Integer glanceover; //是否結帖 private Integer solve; private Integer isdelete; private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public Integer getOffer() { return offer; } public void setOffer(Integer offer) { this.offer = offer; } public Integer getAnsnum() { return ansnum; } public void setAnsnum(Integer ansnum) { this.ansnum = ansnum; } public String getCreatetime() { return createtime; } public void setCreatetime(String createtime) { this.createtime = createtime; } public Integer getGlanceover() { return glanceover; } public void setGlanceover(Integer glanceover) { this.glanceover = glanceover; } public Integer getSolve() { return solve; } public void setSolve(Integer solve) { this.solve = solve; } public Integer getIsdelete() { return isdelete; } public void setIsdelete(Integer isdelete) { this.isdelete = isdelete; } }
package com.Gary.dao; import java.math.BigInteger; import java.util.List; import org.hibernate.Session; import org.hibernate.query.NativeQuery; import org.hibernate.query.Query; import org.springframework.orm.hibernate5.support.HibernateDaoSupport; import com.Gary.domain.Paste; public class PasteDao extends HibernateDaoSupport{ public void addPaste(Paste paste) { Session session = getHibernateTemplate().getSessionFactory().getCurrentSession(); session.save(paste); } //HQL public List<Paste> findAllPaste() { Session session = getHibernateTemplate().getSessionFactory().getCurrentSession(); //書寫SQL語句 String hql = "from com.Gary.domain.Paste"; Query query = session.createQuery(hql); List<Paste> list = query.list(); return list; } //查找全部帖子的數量 public Integer findAllPasteNum() { Session session = getHibernateTemplate().getSessionFactory().getCurrentSession(); String sql = "select count(*) from paste"; NativeQuery query = session.createSQLQuery(sql); BigInteger result = (BigInteger)query.uniqueResult(); return result.intValue(); } //分頁 public List<Paste> getPastePageList(Integer start, Integer pageSize) { Session session = getHibernateTemplate().getSessionFactory().getCurrentSession(); String sql = "select * from paste limit ?,?"; NativeQuery query = session.createSQLQuery(sql); query.addEntity(Paste.class); query.setParameter(1, start); query.setParameter(2, pageSize); List<Paste> list = query.list(); return list; } //最近熱議 public List<Paste> getGlanceoverPageList() { Session session = getHibernateTemplate().getSessionFactory().getCurrentSession(); String sql = "select * from paste order by glanceover desc limit 0,8"; NativeQuery query = session.createSQLQuery(sql); query.addEntity(Paste.class); List list = query.list(); return list; } public List<Paste> getAnsnumPageList() { //獲得與當前線程綁定的session Session session = getHibernateTemplate().getSessionFactory().getCurrentSession(); //書寫sql String sql = "select * from paste order by ansnum desc limit 0,8"; //得到query NativeQuery query = session.createSQLQuery(sql); //將結果集封裝爲Paste對象 query.addEntity(Paste.class); //得到sql的執行結果(結果爲list) List list = query.list(); return list; } }