基於SSH框架的小型論壇項目 javascript
1、項目入門 傳送門css
2、框架整合 傳送門html
3、用戶模塊 傳送門前端
4、頁面顯示 傳送門java
5、帖子模塊 傳送門mysql
6、點贊模塊 傳送門jquery
7、輔助模塊 傳送門web
聯合主鍵spring
建立praise表sql
建立Praise.java和PrimaryKey.java並在Praise.hbm.xml中配置聯合組件
package com.Gary.domain; import com.Gary.vo.PrimaryKey; public class Praise { private PrimaryKey primaryKey; public PrimaryKey getPrimaryKey() { return primaryKey; } public void setPrimaryKey(PrimaryKey primaryKey) { this.primaryKey = primaryKey; } }
package com.Gary.vo; import java.io.Serializable; import com.Gary.domain.Answer; import com.Gary.domain.User; public class PrimaryKey implements Serializable{ private User user; private Answer answer; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public Answer getAnswer() { return answer; } public void setAnswer(Answer answer) { this.answer = answer; } }
<!-- 類名-對應表名字 --> <class name="Praise" table="praise"> <!-- 聯合主鍵的名字 聯合主鍵的類 --> <composite-id name="primaryKey" class="com.Gary.vo.PrimaryKey"> <!-- primarykey中的屬性 對應數據庫中的列 屬性對應的類 --> <key-many-to-one name="user" column="userid" class="User"></key-many-to-one> <key-many-to-one name="answer" column="answerid" class="Answer"></key-many-to-one> </composite-id> </class>
<?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="Praise" table="praise"> <!-- 聯合主鍵的名字 聯合主鍵的類 --> <composite-id name="primaryKey" class="com.Gary.vo.PrimaryKey"> <!-- primarykey中的屬性 對應數據庫中的列 屬性對應的類 --> <key-many-to-one name="user" column="userid" class="User"></key-many-to-one> <key-many-to-one name="answer" column="answerid" class="Answer"></key-many-to-one> </composite-id> </class> </hibernate-mapping>
分析點贊功
detail.jsp
<a href="${pageContext.request.contextPath }/PraiseAction_addPraise?answerid=<s:property value="#answer.id" />&pasteid=<s:property value="#paste.id" />"> <span class="jieda-zan" type="zan"> <i class="iconfont icon-zan"></i> <em> <s:property value="#answer.agree" /> </em> </span> </a>
Web層
//添加贊 public String addPraise() throws Exception{ //確定能夠得到(用戶未登陸不能點贊,只有登陸後才能夠點贊) User user = (User) ActionContext.getContext().getSession().get("user"); if(user == null) { ActionContext.getContext().put("error", "未登陸不容許點贊!!"); return "error"; } //根據answerid查找Answer對象 Answer answer = answerService.findAnswerByIdReturnAnswer(answerid); //申明聯合主鍵 PrimaryKey primaryKey = new PrimaryKey(); //放置對象 primaryKey.setAnswer(answer); primaryKey.setUser(user); //建立讚的類 Praise praise = new Praise(); //將聯合主鍵放入類的對象中 praise.setPrimaryKey(primaryKey); praiseService.addPraise(praise); ActionContext.getContext().put("pasteid", pasteid); return "toDetail"; }
package com.Gary.web; import com.Gary.domain.Answer; import com.Gary.domain.Praise; import com.Gary.domain.User; import com.Gary.service.AnswerService; import com.Gary.service.PraiseService; import com.Gary.vo.PrimaryKey; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class PraiseAction extends ActionSupport{ private String answerid; private String pasteid; private AnswerService answerService; private PraiseService praiseService; public String getPasteid() { return pasteid; } public void setPasteid(String pasteid) { this.pasteid = pasteid; } //添加贊 public String addPraise() throws Exception{ //確定能夠得到(用戶未登陸不能點贊,只有登陸後才能夠點贊) User user = (User) ActionContext.getContext().getSession().get("user"); if(user == null) { ActionContext.getContext().put("error", "未登陸不容許點贊!!"); return "error"; } //根據answerid查找Answer對象 Answer answer = answerService.findAnswerByIdReturnAnswer(answerid); //申明聯合主鍵 PrimaryKey primaryKey = new PrimaryKey(); //放置對象 primaryKey.setAnswer(answer); primaryKey.setUser(user); //建立讚的類 Praise praise = new Praise(); //將聯合主鍵放入類的對象中 praise.setPrimaryKey(primaryKey); praiseService.addPraise(praise); ActionContext.getContext().put("pasteid", pasteid); return "toDetail"; } public AnswerService getAnswerService() { return answerService; } public void setAnswerService(AnswerService answerService) { this.answerService = answerService; } public PraiseService getPraiseService() { return praiseService; } public void setPraiseService(PraiseService praiseService) { this.praiseService = praiseService; } public String getAnswerid() { return answerid; } public void setAnswerid(String answerid) { this.answerid = answerid; } }
service層
public void addPraise(Praise praise) { praiseDao.addPraise(praise); }
package com.Gary.service; import com.Gary.dao.PraiseDao; import com.Gary.domain.Praise; public class PraiseService { private PraiseDao praiseDao; public void addPraise(Praise praise) { praiseDao.addPraise(praise); } public PraiseDao getPraiseDao() { return praiseDao; } public void setPraiseDao(PraiseDao praiseDao) { this.praiseDao = praiseDao; } }
Dao層
//添加贊 public void addPraise(Praise praise) { Session session = getHibernateTemplate().getSessionFactory().getCurrentSession(); session.save(praise); }
package com.Gary.dao; import org.hibernate.Session; import org.hibernate.query.NativeQuery; import org.springframework.orm.hibernate5.support.HibernateDaoSupport; import com.Gary.domain.Praise; public class PraiseDao extends HibernateDaoSupport{ //添加贊 public void addPraise(Praise praise) { Session session = getHibernateTemplate().getSessionFactory().getCurrentSession(); session.save(praise); } }
<%@ 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> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> <meta name="keywords" content="fly,layui,前端社區"> <meta name="description" content=""> <link rel="stylesheet" href="res/layui/css/layui.css"> <link rel="stylesheet" href="res/css/global.css"> <script src="res/layui/layui.js"></script> <script src="js/jquery.js"></script> <style type="text/css" rel="stylesheet"> form { margin: 0; } .editor { margin-top: 5px; margin-bottom: 5px; } </style> </head> <body style="margin: -2px"> <iframe src="head.jsp" scrolling="no" width="100%" height="110px"></iframe> <div class="main layui-clear"> <div class="wrap"> <div class="content detail"> <div class="fly-panel detail-box"> <h1> <s:property value="#paste.title" /> </h1> <div class="fly-tip fly-detail-hint" data-id=""> <span class="fly-tip-stick">置頂帖</span> <span class="jie-admin"> <a href="">點擊置頂</a> </span> <span class="layui-btn layui-btn-mini jie-admin"> <a href="">取消置頂</a> </span> <span class="jie-admin" type="del" style="margin-left: 20px;"> <a>刪除該帖</a> </span> </span> <div class="fly-list-hint"> <i class="iconfont" title="回答"></i> <s:property value="#paste.ansnum" /> </div> </div> <div class="detail-about"> <a class="jie-user" href=""> <img src="${pageContext.request.contextPath }/<s:property value="#paste.user.image"/>" alt="頭像"> <cite> <s:property value="#paste.user.username" /> <em> <s:property value="#paste.createtime" /> 發佈 </em> </cite> </a> <div class="detail-hits" data-id="{{rows.id}}"> <span class="layui-btn layui-btn-mini jie-admin"> <a href="#">已完帖,沒法編輯</a> </span> <span class="layui-btn layui-btn-mini jie-admin" type="collect" data-type="add"> <a id="collectPost">收藏</a> </span> <span class="layui-btn layui-btn-mini jie-admin layui-btn-danger" type="collect" data-type="add"> <a>取消收藏</a> </span> </div> </div> <div class="detail-body photos" style="margin-bottom: 20px;"> <p> <s:property value="#paste.content" /> </p> </div> </div> <div class="fly-panel detail-box" style="padding-top: 0;"> <a name="comment"></a> <ul class="jieda photos" id="jieda"> <!-- <li data-id="12" class="jieda-daan"><a name="item-121212121212"></a> <div class="detail-about detail-about-reply"> <a class="jie-user" href=""> <img src="res/images/uer.jpg" alt=""> <cite> <i>紙飛機</i> <em>(樓主)</em> <em style="color:#5FB878">(管理員)</em> </cite> </a> <div class="detail-hits"> <span>3分鐘前</span> </div> <i class="iconfont icon-caina" title="最佳答案"></i> </div> <div class="detail-body jieda-body"> <p>麼麼噠</p> </div> <div class="jieda-reply"> <span class="jieda-zan zanok" type="zan"><i class="iconfont icon-zan"></i><em>12</em> </span> <!-- <div class="jieda-admin"> <span type="del">刪除</span> <span class="jieda-accept" type="accept">採納</span> </div> </div></li> --> <s:iterator value="#answerList" var="answer"> <li data-id="13"><a name="item-121212121212"></a> <div class="detail-about detail-about-reply"> <a class="jie-user" href=""> <img src="${pageContext.request.contextPath }/<s:property value="#answer.user.image"/>" alt=""> <cite> <i><s:property value="#answer.user.username" /></i> <em style="color: #FF9E3F">活雷鋒</em> </cite> </a> <div class="detail-hits"> <span> <s:property value="#answer.anstime" /> </span> </div> <s:if test="#answer.id==#paste.answerid"> <i class="iconfont icon-caina" title="最佳答案"></i> </s:if> </div> <div class="detail-body jieda-body"> <p> <s:property value="#answer.content" /> </p> </div> <div class="jieda-reply"> <a href="${pageContext.request.contextPath }/PraiseAction_addPraise?answerid=<s:property value="#answer.id" />&pasteid=<s:property value="#paste.id" />"> <span class="jieda-zan" type="zan"> <i class="iconfont icon-zan"></i> <em> <s:property value="#answer.agree" /> </em> </span> </a> <div class="jieda-admin"> <s:if test="#session.user.username==#answer.user.username && #paste.answerid!=#answer.id"> <span type="del"> <a href="${pageContext.request.contextPath }/AnswerAction_deleteAnswer?answerid=<s:property value="#answer.id"/>&pasteid=<s:property value="#paste.id"/>" class="layui-btn layui-btn-danger layui-btn-small">刪除</a> </span> </s:if> <s:if test="#session.user.username==#paste.user.username && #paste.solve==0"> <span class="jieda-accept" type="accept"> <a href="${pageContext.request.contextPath }/PasteAction_solvePaste?answerid=<s:property value="#answer.id"/>&pasteid=<s:property value="#paste.id"/>" class="layui-btn layui-btn-small">採納</a> </span> </s:if> </div> </div></li> </s:iterator> <s:if test="#answerList.size()==0"> <li class="fly-none">沒有任何回答</li> </s:if> </ul> <div class="layui-form layui-form-pane"> <!-- <form action="${pageContext.request.contextPath }/AnswerAction_addAnswer"> <input type="hidden" name="pasteid" value="<s:property value="#paste.id"/>"> <div class="layui-form-item layui-form-text"> <div class="layui-input-block"> <textarea id="L_content" name="content" required lay-verify="required" placeholder="我要回答" class="layui-textarea fly-editor" style="height: 150px;"></textarea> </div> </div> <div class="layui-form-item"> <button class="layui-btn" lay-filter="*" lay-submit>提交回答</button> </div> </form> --> <form action="${pageContext.request.contextPath}/AnswerAction_addAnswer"> <input type="hidden" name="pasteid" value="<s:property value="#paste.id"/>"> <label for="L_title" class="layui-form-label" style="width: 690px; height: 40px;"> <div style="margin-left: -580px">回答問題:</div> </label> <div class="layui-form-item layui-form-text"> <div class="layui-input-block"> <div class="editor"> <textarea id="content" name="content" style="width: 690px; height: 300px; visibility: hidden;"></textarea> </div> </div> </div> <div class="layui-form-item"> <button class="layui-btn" lay-filter="*" lay-submit>當即發佈</button> </div> </form> </div> </div> </div> </div> <div class="edge"> <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> </div> <%-- <script type="text/javascript" charset="utf-8" src="res/js/kindeditor.js"></script> <script type="text/javascript"> KE.show({ id : 'L_content', resizeMode : 1, items : [ 'fontname', 'fontsize', 'textcolor', 'bgcolor', 'bold', 'italic', 'underline', 'removeformat', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist', 'insertunorderedlist', 'emoticons', 'image', 'link' ] }); </script> --%> <script type="text/javascript" charset="utf-8" src="js/kindeditor.js"></script> <script type="text/javascript"> KE.show({ id : 'content', resizeMode : 1, cssPath : './index.css', items : [ 'fontname', 'fontsize', 'textcolor', 'bgcolor', 'bold', 'italic', 'underline', 'removeformat', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist', 'insertunorderedlist', 'emoticons', 'image', 'link' ] }); </script> <script> layui.cache.page = ''; layui.cache.user = { username : '遊客', uid : -1, avatar : '../res/images/avatar/00.jpg', experience : 83, sex : '男' }; layui.config({ version : "2.0.0", base : '../res/mods/' }).extend({ fly : 'index' }).use('fly'); </script> </body> </html>
package com.Gary.web;
import com.Gary.domain.Answer;
import com.Gary.domain.Praise;
import com.Gary.domain.User;
import com.Gary.service.AnswerService;
import com.Gary.service.PraiseService;
import com.Gary.vo.PrimaryKey;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class PraiseAction extends ActionSupport{
private String answerid;
private String pasteid;
private AnswerService answerService;
private PraiseService praiseService;
public String getPasteid() {
return pasteid;
}
public void setPasteid(String pasteid) {
this.pasteid = pasteid;
}
//添加贊
public String addPraise() throws Exception{
//確定能夠得到(用戶未登陸不能點贊,只有登陸後才能夠點贊)
User user = (User) ActionContext.getContext().getSession().get("user");
if(user == null)
{
ActionContext.getContext().put("error", "未登陸不容許點贊!!");
return "error";
}
//根據answerid查找Answer對象
Answer answer = answerService.findAnswerByIdReturnAnswer(answerid);
//申明聯合主鍵
PrimaryKey primaryKey = new PrimaryKey();
//放置對象
primaryKey.setAnswer(answer);
primaryKey.setUser(user);
//建立讚的類
Praise praise = new Praise();
//將聯合主鍵放入類的對象中
praise.setPrimaryKey(primaryKey);
praiseService.addPraise(praise);
answer.setAgree(answer.getAgree()+1);
ActionContext.getContext().put("pasteid", pasteid);
return "toDetail";
}
public AnswerService getAnswerService() {
return answerService;
}
public void setAnswerService(AnswerService answerService) {
this.answerService = answerService;
}
public PraiseService getPraiseService() {
return praiseService;
}
public void setPraiseService(PraiseService praiseService) {
this.praiseService = praiseService;
}
public String getAnswerid() {
return answerid;
}
public void setAnswerid(String answerid) {
this.answerid = answerid;
}
}
package com.Gary.service; import com.Gary.dao.PraiseDao; import com.Gary.domain.Praise; public class PraiseService { private PraiseDao praiseDao; public void addPraise(Praise praise) { praiseDao.addPraise(praise); } public PraiseDao getPraiseDao() { return praiseDao; } public void setPraiseDao(PraiseDao praiseDao) { this.praiseDao = praiseDao; } }
package com.Gary.dao; import org.hibernate.Session; import org.hibernate.query.NativeQuery; import org.springframework.orm.hibernate5.support.HibernateDaoSupport; import com.Gary.domain.Praise; public class PraiseDao extends HibernateDaoSupport{ //添加贊 public void addPraise(Praise praise) { Session session = getHibernateTemplate().getSessionFactory().getCurrentSession(); session.save(praise); } }
<?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">/default.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}"> <!-- 從定向到jsp --> <result name="toIndex" type="redirect">/default.jsp</result> <result name="error">/login.jsp</result> <!-- 轉發到jsp --> <result name="detail">/detail.jsp</result> <!-- 重定向到Action --> <result name="toDetail" type="redirectAction"> <param name="actionName"> PasteAction_getDetail </param> <!-- struts不認識pasteid則會封裝到重定向的action中 --> <param name="pasteid">${pasteid}</param> </result> </action> <action name="GetDataAction_*" class="com.Gary.web.GetDataAction" method="{1}"> <result name="index" >/index.jsp</result> </action> <action name="AnswerAction_*" class="com.Gary.web.AnswerAction" method="{1}"> <result name="error">/login.jsp</result> <!-- 重定向到Action --> <result name="toDetail" type="redirectAction"> <param name="actionName">PasteAction_getDetail</param> <!-- struts不認識pasteid則會封裝到重定向的action中 --> <param name="pasteid">${pasteid}</param> </result> </action> <action name="PraiseAction_*" class="com.Gary.web.PraiseAction" method="{1}"> <result name="error">/login.jsp</result> <result name="toDetail" type="redirectAction"> <param name="actionName">PasteAction_getDetail</param> <param name="pasteid">${pasteid}</param> </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> <property name="answerService" ref="answerService"></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> <bean name="answerAction" class="com.Gary.web.AnswerAction" scope="prototype"> <property name="answerService" ref="answerService"></property> <property name="pasteService" ref="pasteService"></property> </bean> <bean name="praiseAction" class="com.Gary.web.PraiseAction" scope="prototype"> <property name="answerService" ref="answerService"></property> <property name="praiseService" ref="praiseService"></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> <property name="userDao" ref="userDao"></property> <property name="answerDao" ref="answerDao"></property> </bean> <bean name="answerService" class="com.Gary.service.AnswerService"> <property name="answerDao" ref="answerDao"></property> <property name="pasteDao" ref="pasteDao"></property> </bean> <bean name="praiseService" class="com.Gary.service.PraiseService"> <property name="praiseDao" ref="praiseDao"></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> <bean name="answerDao" class="com.Gary.dao.AnswerDao"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean name="praiseDao" class="com.Gary.dao.PraiseDao"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> </beans>
前端分析點贊
用戶點贊後,後端處理邏輯及修改數據庫的值
前端點贊
<div class="jieda-reply"> <!-- 登陸用戶點讚了哪些回覆 --> <s:if test="#answer.loginUserIsAgree==0"> <a href="${pageContext.request.contextPath }/PraiseAction_addPraise?answerid=<s:property value="#answer.id" />&pasteid=<s:property value="#paste.id" />"> <span class="jieda-zan" type="zan"> <i class="iconfont icon-zan"></i> <em> <s:property value="#answer.agree" /> </em> </span> </a> </s:if> <s:else> <span class="jieda-zan zanok" type="zan"> <i class="iconfont icon-zan"></i> <em> <s:property value="#answer.agree" /> </em> </span> </s:else>
Web層得到用戶點贊後查看帖子詳細信息,當用戶未登錄時,需用戶跳轉到登錄界面登錄,當用戶成功登錄,則點贊數+1
//查看帖子詳細信息 public String getDetail() throws Exception{ User user = (User)ActionContext.getContext().getSession().get("user"); //獲得最近熱帖 PageBean glanceoverPageBean = pasteService.getGlanceoverPageBean(null); ActionContext.getContext().put("glanceoverPageBean", glanceoverPageBean); //獲得最近熱議 PageBean ansnumPageBean = pasteService.getAnsnumPageBean(null); ActionContext.getContext().put("ansnumPageBean", ansnumPageBean); //得到帖子 Paste paste = pasteService.findPasteByIdReturnPaste(pasteid); ActionContext.getContext().put("paste", paste); //得到該帖子的全部回覆 List<Answer> answerList = answerService.findAllAnswerByPasteid(pasteid); for(Answer answer:answerList) { if(user != null) { //判斷用戶是否點讚了該回復 boolean success = praiseService.findPraiseByIdReturnPraise(user.getId(),answer.getId()); if(success) { //若是查詢到用戶點讚了該回復,將loginUserIsAgree設置爲1 answer.setLoginUserIsAgree(1); } else { //若是沒有查詢到用戶點讚了該回復,將loginUserIsAgree設置爲0 answer.setLoginUserIsAgree(0); } } else { //若是沒有登錄,將loginUserIsAgree設置爲0 answer.setLoginUserIsAgree(0); } } ActionContext.getContext().put("answerList", answerList); return "detail"; }
Dao層進行數據庫的修改
public void addAnswerAgree(String answerid) { Answer answer = answerDao.findAnswerById(answerid); answer.setAgree(answer.getAgree() + 1); //第二種方式sql //update answer set agree = agree +1 where answer id = ? }
<%@ 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> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> <meta name="keywords" content="fly,layui,前端社區"> <meta name="description" content=""> <link rel="stylesheet" href="res/layui/css/layui.css"> <link rel="stylesheet" href="res/css/global.css"> <script src="res/layui/layui.js"></script> <script src="js/jquery.js"></script> <style type="text/css" rel="stylesheet"> form { margin: 0; } .editor { margin-top: 5px; margin-bottom: 5px; } </style> </head> <body style="margin: -2px"> <iframe src="head.jsp" scrolling="no" width="100%" height="110px"></iframe> <div class="main layui-clear"> <div class="wrap"> <div class="content detail"> <div class="fly-panel detail-box"> <h1> <s:property value="#paste.title" /> </h1> <div class="fly-tip fly-detail-hint" data-id=""> <span class="fly-tip-stick">置頂帖</span> <span class="jie-admin"> <a href="">點擊置頂</a> </span> <span class="layui-btn layui-btn-mini jie-admin"> <a href="">取消置頂</a> </span> <span class="jie-admin" type="del" style="margin-left: 20px;"> <a>刪除該帖</a> </span> </span> <div class="fly-list-hint"> <i class="iconfont" title="回答"></i> <s:property value="#paste.ansnum" /> </div> </div> <div class="detail-about"> <a class="jie-user" href=""> <img src="${pageContext.request.contextPath }/<s:property value="#paste.user.image"/>" alt="頭像"> <cite> <s:property value="#paste.user.username" /> <em> <s:property value="#paste.createtime" /> 發佈 </em> </cite> </a> <div class="detail-hits" data-id="{{rows.id}}"> <span class="layui-btn layui-btn-mini jie-admin"> <a href="#">已完帖,沒法編輯</a> </span> <span class="layui-btn layui-btn-mini jie-admin" type="collect" data-type="add"> <a id="collectPost">收藏</a> </span> <span class="layui-btn layui-btn-mini jie-admin layui-btn-danger" type="collect" data-type="add"> <a>取消收藏</a> </span> </div> </div> <div class="detail-body photos" style="margin-bottom: 20px;"> <p> <s:property value="#paste.content" /> </p> </div> </div> <div class="fly-panel detail-box" style="padding-top: 0;"> <a name="comment"></a> <ul class="jieda photos" id="jieda"> <!-- <li data-id="12" class="jieda-daan"><a name="item-121212121212"></a> <div class="detail-about detail-about-reply"> <a class="jie-user" href=""> <img src="res/images/uer.jpg" alt=""> <cite> <i>紙飛機</i> <em>(樓主)</em> <em style="color:#5FB878">(管理員)</em> </cite> </a> <div class="detail-hits"> <span>3分鐘前</span> </div> <i class="iconfont icon-caina" title="最佳答案"></i> </div> <div class="detail-body jieda-body"> <p>麼麼噠</p> </div> <div class="jieda-reply"> <span class="jieda-zan zanok" type="zan"><i class="iconfont icon-zan"></i><em>12</em> </span> <!-- <div class="jieda-admin"> <span type="del">刪除</span> <span class="jieda-accept" type="accept">採納</span> </div> </div></li> --> <s:iterator value="#answerList" var="answer"> <li data-id="13"><a name="item-121212121212"></a> <div class="detail-about detail-about-reply"> <a class="jie-user" href=""> <img src="${pageContext.request.contextPath }/<s:property value="#answer.user.image"/>" alt=""> <cite> <i><s:property value="#answer.user.username" /></i> <em style="color: #FF9E3F">活雷鋒</em> </cite> </a> <div class="detail-hits"> <span> <s:property value="#answer.anstime" /> </span> </div> <s:if test="#answer.id==#paste.answerid"> <i class="iconfont icon-caina" title="最佳答案"></i> </s:if> </div> <div class="detail-body jieda-body"> <p> <s:property value="#answer.content" /> </p> </div> <div class="jieda-reply"> <!-- 登陸用戶點讚了哪些回覆 --> <s:if test="#answer.loginUserIsAgree==0"> <a href="${pageContext.request.contextPath }/PraiseAction_addPraise?answerid=<s:property value="#answer.id" />&pasteid=<s:property value="#paste.id" />"> <span class="jieda-zan" type="zan"> <i class="iconfont icon-zan"></i> <em> <s:property value="#answer.agree" /> </em> </span> </a> </s:if> <s:else> <span class="jieda-zan zanok" type="zan"> <i class="iconfont icon-zan"></i> <em> <s:property value="#answer.agree" /> </em> </span> </s:else> <div class="jieda-admin"> <s:if test="#session.user.username==#answer.user.username && #paste.answerid!=#answer.id"> <span type="del"> <a href="${pageContext.request.contextPath }/AnswerAction_deleteAnswer?answerid=<s:property value="#answer.id"/>&pasteid=<s:property value="#paste.id"/>" class="layui-btn layui-btn-danger layui-btn-small">刪除</a> </span> </s:if> <s:if test="#session.user.username==#paste.user.username && #paste.solve==0"> <span class="jieda-accept" type="accept"> <a href="${pageContext.request.contextPath }/PasteAction_solvePaste?answerid=<s:property value="#answer.id"/>&pasteid=<s:property value="#paste.id"/>" class="layui-btn layui-btn-small">採納</a> </span> </s:if> </div> </div></li> </s:iterator> <s:if test="#answerList.size()==0"> <li class="fly-none">沒有任何回答</li> </s:if> </ul> <div class="layui-form layui-form-pane"> <!-- <form action="${pageContext.request.contextPath }/AnswerAction_addAnswer"> <input type="hidden" name="pasteid" value="<s:property value="#paste.id"/>"> <div class="layui-form-item layui-form-text"> <div class="layui-input-block"> <textarea id="L_content" name="content" required lay-verify="required" placeholder="我要回答" class="layui-textarea fly-editor" style="height: 150px;"></textarea> </div> </div> <div class="layui-form-item"> <button class="layui-btn" lay-filter="*" lay-submit>提交回答</button> </div> </form> --> <form action="${pageContext.request.contextPath}/AnswerAction_addAnswer"> <input type="hidden" name="pasteid" value="<s:property value="#paste.id"/>"> <label for="L_title" class="layui-form-label" style="width: 690px; height: 40px;"> <div style="margin-left: -580px">回答問題:</div> </label> <div class="layui-form-item layui-form-text"> <div class="layui-input-block"> <div class="editor"> <textarea id="content" name="content" style="width: 690px; height: 300px; visibility: hidden;"></textarea> </div> </div> </div> <div class="layui-form-item"> <button class="layui-btn" lay-filter="*" lay-submit>當即發佈</button> </div> </form> </div> </div> </div> </div> <div class="edge"> <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> </div> <%-- <script type="text/javascript" charset="utf-8" src="res/js/kindeditor.js"></script> <script type="text/javascript"> KE.show({ id : 'L_content', resizeMode : 1, items : [ 'fontname', 'fontsize', 'textcolor', 'bgcolor', 'bold', 'italic', 'underline', 'removeformat', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist', 'insertunorderedlist', 'emoticons', 'image', 'link' ] }); </script> --%> <script type="text/javascript" charset="utf-8" src="js/kindeditor.js"></script> <script type="text/javascript"> KE.show({ id : 'content', resizeMode : 1, cssPath : './index.css', items : [ 'fontname', 'fontsize', 'textcolor', 'bgcolor', 'bold', 'italic', 'underline', 'removeformat', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist', 'insertunorderedlist', 'emoticons', 'image', 'link' ] }); </script> <script> layui.cache.page = ''; layui.cache.user = { username : '遊客', uid : -1, avatar : '../res/images/avatar/00.jpg', experience : 83, sex : '男' }; layui.config({ version : "2.0.0", base : '../res/mods/' }).extend({ fly : 'index' }).use('fly'); </script> </body> </html>
package com.Gary.web; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import com.Gary.domain.Answer; import com.Gary.domain.Paste; import com.Gary.domain.User; import com.Gary.service.AnswerService; import com.Gary.service.PasteService; import com.Gary.service.PraiseService; import com.Gary.utils.PageBean; 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 String pasteid; private PasteService pasteService; private AnswerService answerService; private PraiseService praiseService; //解決帖子 public String solvePaste() throws Exception{ pasteService.solvePasteByIdAndAnswerid(pasteid,paste.getAnswerid()); ActionContext.getContext().put("pasteid", pasteid); return "toDetail"; } //查看帖子詳細信息 public String getDetail() throws Exception{ User user = (User)ActionContext.getContext().getSession().get("user"); //獲得最近熱帖 PageBean glanceoverPageBean = pasteService.getGlanceoverPageBean(null); ActionContext.getContext().put("glanceoverPageBean", glanceoverPageBean); //獲得最近熱議 PageBean ansnumPageBean = pasteService.getAnsnumPageBean(null); ActionContext.getContext().put("ansnumPageBean", ansnumPageBean); //得到帖子 Paste paste = pasteService.findPasteByIdReturnPaste(pasteid); ActionContext.getContext().put("paste", paste); //得到該帖子的全部回覆 List<Answer> answerList = answerService.findAllAnswerByPasteid(pasteid); for(Answer answer:answerList) { if(user != null) { //判斷用戶是否點讚了該回復 boolean success = praiseService.findPraiseByIdReturnPraise(user.getId(),answer.getId()); if(success) { //若是查詢到用戶點讚了該回復,將loginUserIsAgree設置爲1 answer.setLoginUserIsAgree(1); } else { //若是沒有查詢到用戶點讚了該回復,將loginUserIsAgree設置爲0 answer.setLoginUserIsAgree(0); } } else { //若是沒有登錄,將loginUserIsAgree設置爲0 answer.setLoginUserIsAgree(0); } } ActionContext.getContext().put("answerList", answerList); return "detail"; } public PraiseService getPraiseService() { return praiseService; } public void setPraiseService(PraiseService praiseService) { this.praiseService = praiseService; } public AnswerService getAnswerService() { return answerService; } public void setAnswerService(AnswerService answerService) { this.answerService = answerService; } //添加帖子 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; } public String getPasteid() { return pasteid; } public void setPasteid(String pasteid) { this.pasteid = pasteid; } }
package com.Gary.service; import com.Gary.dao.PraiseDao; import com.Gary.domain.Praise; public class PraiseService { private PraiseDao praiseDao; public boolean findPraiseByIdReturnPraise(String userid, String answerid) { Praise praise = praiseDao.findPraiseByIdReturnPraise(userid,answerid); return praise==null?false:true; } public void addPraise(Praise praise) { praiseDao.addPraise(praise); } public PraiseDao getPraiseDao() { return praiseDao; } public void setPraiseDao(PraiseDao praiseDao) { this.praiseDao = praiseDao; } }
package com.Gary.web; import com.Gary.domain.Answer; import com.Gary.domain.Praise; import com.Gary.domain.User; import com.Gary.service.AnswerService; import com.Gary.service.PraiseService; import com.Gary.vo.PrimaryKey; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class PraiseAction extends ActionSupport{ private String answerid; private String pasteid; private AnswerService answerService; private PraiseService praiseService; public String getPasteid() { return pasteid; } public void setPasteid(String pasteid) { this.pasteid = pasteid; } //添加贊 public String addPraise() throws Exception{ //確定能夠得到(用戶未登陸不能點贊,只有登陸後才能夠點贊) User user = (User) ActionContext.getContext().getSession().get("user"); if(user == null) { ActionContext.getContext().put("error", "未登陸不容許點贊!!"); return "error"; } //根據answerid查找Answer對象 Answer answer = answerService.findAnswerByIdReturnAnswer(answerid); //申明聯合主鍵 PrimaryKey primaryKey = new PrimaryKey(); //放置對象 primaryKey.setAnswer(answer); primaryKey.setUser(user); //建立讚的類 Praise praise = new Praise(); //將聯合主鍵放入類的對象中 praise.setPrimaryKey(primaryKey); praiseService.addPraise(praise); answerService.addAnswerAgree(answerid); ActionContext.getContext().put("pasteid", pasteid); return "toDetail"; } public AnswerService getAnswerService() { return answerService; } public void setAnswerService(AnswerService answerService) { this.answerService = answerService; } public PraiseService getPraiseService() { return praiseService; } public void setPraiseService(PraiseService praiseService) { this.praiseService = praiseService; } public String getAnswerid() { return answerid; } public void setAnswerid(String answerid) { this.answerid = answerid; } }
package com.Gary.service; import java.util.List; import com.Gary.dao.AnswerDao; import com.Gary.dao.PasteDao; import com.Gary.domain.Answer; import com.Gary.domain.Paste; public class AnswerService { private AnswerDao answerDao; private PasteDao pasteDao; public void addAnswerAgree(String answerid) { Answer answer = answerDao.findAnswerById(answerid); answer.setAgree(answer.getAgree() + 1); //第二種方式sql //update answer set agree = agree +1 where answer id = ? } //根據answerid查找answer對象 public Answer findAnswerByIdReturnAnswer(String answerid) { Answer answer = answerDao.findAnswerById(answerid); return answer; } public List<Answer> findAllAnswerByPasteid(String pasteid) { Paste paste = pasteDao.findPasteByIdReturnPaste(pasteid); List<Answer> answerList = null; // 判斷paste中的solve是否爲1 if (paste.getSolve() == 1) { // 查找最佳答案 Answer answer = answerDao.findAnswerById(paste.getAnswerid()); // 查找全部答案 answerList = answerDao.findAllAnswerByPasteid(pasteid); // 查找全部答案中包括了最佳答案因此將最佳答案刪除 answerList.remove(answer); // 在頭元素中插入最佳答案 answerList.add(0, answer); } else { answerList = answerDao.findAllAnswerByPasteid(pasteid); } return answerList; } public void addAnswer(Answer answer, Paste paste) { paste.setAnsnum(paste.getAnsnum() + 1); answerDao.addAnswer(answer); } public void deleteAnswerById(String answerid, Paste paste) { paste.setAnsnum(paste.getAnsnum()-1); answerDao.deleteAnswerById(answerid); } public AnswerDao getAnswerDao() { return answerDao; } public void setAnswerDao(AnswerDao answerDao) { this.answerDao = answerDao; } public PasteDao getPasteDao() { return pasteDao; } public void setPasteDao(PasteDao pasteDao) { this.pasteDao = pasteDao; } }