基于SSH框架的小型论坛项目
一、项目入门 传送门
二、框架整合 传送门
三、用户模块 传送门
四、页面显示 传送门
五、帖子模块 传送门
六、点赞模块 传送门
七、辅助模块 传送门
联合主键
创建praise表
创建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=" 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=" 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=" 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=" 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; } }