基于SSH框架的小型论坛项目  

  一、项目入门  传送门

  二、框架整合  传送门

  三、用户模块  传送门

  四、页面显示  传送门

  五、帖子模块  传送门

  六、点赞模块  传送门

  七、辅助模块  传送门

回复帖子

  分析回复帖子

JavaWeb_(SSH论坛)_五、帖子模块-LMLPHP

  拿到paste.title标签

                                        <a href="${pageContext.request.contextPath }/PasteAction_getDetail?pasteid=<s:property value="#paste.id"/>">
<s:property value="#paste.title" />
</a>

  编写查看帖子模型驱动

  帖子模型驱动Web层

    //查看帖子详细信息
public String getDetail() throws Exception{ Paste paste = pasteService.findAllPasteByIdReturnPaste(pasteid); ActionContext.getContext().put("paste", paste); return "detail";
}

  帖子模型驱动Service层

    public Paste findAllPasteByIdReturnPaste(String pasteid) {

        return pasteDao.findPasteByIdReturnPaste(pasteid);
}

  帖子模板Dao层

    public Paste findPasteByIdReturnPaste(String pasteid) {
Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
String sql = "select * from paste where id = ?";
NativeQuery query = session.createSQLQuery(sql);
query.addEntity(Paste.class);
query.setParameter(1, pasteid);
Paste result = (Paste) query.uniqueResult();
return result;
}

  帖子前端页面

  JavaWeb_(SSH论坛)_五、帖子模块-LMLPHP

<!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>
<iframe src="head.html" scrolling="no" width="100%" height="65px" ></iframe>
<div class="main layui-clear">
<div class="wrap">
<div class="content detail">
<div class="fly-panel detail-box">
<h1>帖子标题</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> 2
</div>
</div>
<div class="detail-about">
<a class="jie-user" href=""> <img
src="res/images/uer.jpg" alt="头像"> <cite> 压缩
<em>2017-05-01发布</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>帖子内容</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>
<li data-id="13"><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 style="color:#FF9E3F">活雷锋</em> </cite> </a>
<div class="detail-hits">
<span>刚刚</span>
</div>
</div>
<div class="detail-body jieda-body">
<p>蓝瘦</p>
</div>
<div class="jieda-reply">
<span class="jieda-zan" type="zan"><i
class="iconfont icon-zan"></i><em>0</em>
</span>
<div class="jieda-admin">
<span type="del"><a href="#" class="layui-btn layui-btn-danger layui-btn-small">删除</a></span>
<span class="jieda-accept" type="accept">
<a href="#" class="layui-btn layui-btn-small">采纳</a></span>
</div>
</div></li> <!-- <li class="fly-none">没有任何回答</li> -->
</ul>
<span
id="toName">@ 压缩(楼主)</span>
<div class="layui-form layui-form-pane">
<form action="">
<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>
</div>
</div>
</div>
</div> <div class="edge">
<dl class="fly-panel fly-list-one">
<dt class="fly-panel-title">最近热帖</dt>
<dd>
<a href="">使用 layui 秒搭后台大布局(基本结构)</a> <span><i
class="iconfont"></i> 6087</span>
</dd>
<dd>
<a href="">Java实现LayIM后端的核心代码</a> <span><i class="iconfont"></i>
767</span>
</dd>
<dd>
<a href="">使用 layui 秒搭后台大布局(基本结构)</a> <span><i
class="iconfont"></i> 6087</span>
</dd>
<dd>
<a href="">Java实现LayIM后端的核心代码</a> <span><i class="iconfont"></i>
767</span>
</dd>
<dd>
<a href="">使用 layui 秒搭后台大布局(基本结构)</a> <span><i
class="iconfont"></i> 6087</span>
</dd>
<dd>
<a href="">Java实现LayIM后端的核心代码</a> <span><i class="iconfont"></i>
767</span>
</dd>
<dd>
<a href="">使用 layui 秒搭后台大布局(基本结构)</a> <span><i
class="iconfont"></i> 6087</span>
</dd>
<dd>
<a href="">Java实现LayIM后端的核心代码</a> <span><i class="iconfont"></i>
767</span>
</dd>
</dl> <dl class="fly-panel fly-list-one">
<dt class="fly-panel-title">近期热议</dt>
<dd>
<a href="">使用 layui 秒搭后台大布局之基本结构</a> <span><i
class="iconfont"></i> 96</span>
</dd>
<dd>
<a href="">使用 layui 秒搭后台大布局之基本结构</a> <span><i
class="iconfont"></i> 96</span>
</dd>
<dd>
<a href="">使用 layui 秒搭后台大布局之基本结构</a> <span><i
class="iconfont"></i> 96</span>
</dd>
<dd>
<a href="">使用 layui 秒搭后台大布局之基本结构</a> <span><i
class="iconfont"></i> 96</span>
</dd>
<dd>
<a href="">使用 layui 秒搭后台大布局之基本结构</a> <span><i
class="iconfont"></i> 96</span>
</dd>
<dd>
<a href="">使用 layui 秒搭后台大布局之基本结构</a> <span><i
class="iconfont"></i> 96</span>
</dd>
<dd>
<a href="">使用 layui 秒搭后台大布局之基本结构</a> <span><i
class="iconfont"></i> 96</span>
</dd>
<dd>
<a href="">使用 layui 秒搭后台大布局之基本结构</a> <span><i
class="iconfont"></i> 96</span>
</dd>
</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>
</body>
</html>

detail.html

  动态从数据库中获取帖子模块内容

                <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>

  JavaWeb_(SSH论坛)_五、帖子模块-LMLPHP

  Web层

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;
private String pasteid; //查看帖子详细信息
public String getDetail() throws Exception{ Paste paste = pasteService.findAllPasteByIdReturnPaste(pasteid); ActionContext.getContext().put("paste", paste); return "detail";
} //添加帖子
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;
} }

PasteAction.java

  Service层

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 Paste findAllPasteByIdReturnPaste(String pasteid) { return pasteDao.findPasteByIdReturnPaste(pasteid);
} 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;
} }

PasteService.java

  Dao层

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;
} //查看帖子
public Paste findPasteByIdReturnPaste(String pasteid) {
Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
String sql = "select * from paste where id = ?";
NativeQuery query = session.createSQLQuery(sql);
query.addEntity(Paste.class);
query.setParameter(1, pasteid);
Paste result = (Paste) query.uniqueResult();
return result;
} }

PasteDao.java

  帖子模块前端页面

<%@ 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>
<li data-id="13"><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 style="color: #FF9E3F">活雷锋</em>
</cite>
</a>
<div class="detail-hits">
<span>刚刚</span>
</div>
</div>
<div class="detail-body jieda-body">
<p>蓝瘦</p>
</div>
<div class="jieda-reply">
<span class="jieda-zan" type="zan"><i class="iconfont icon-zan"></i><em>0</em> </span>
<div class="jieda-admin">
<span type="del"><a href="#" class="layui-btn layui-btn-danger layui-btn-small">删除</a></span> <span class="jieda-accept" type="accept"> <a href="#" class="layui-btn layui-btn-small">采纳</a></span>
</div>
</div></li> <!-- <li class="fly-none">没有任何回答</li> -->
</ul>
<span id="toName">@ 压缩(楼主)</span>
<div class="layui-form layui-form-pane">
<form action="">
<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>
</div>
</div>
</div>
</div> <div class="edge">
<dl class="fly-panel fly-list-one">
<dt class="fly-panel-title">最近热帖</dt>
<dd>
<a href="">使用 layui 秒搭后台大布局(基本结构)</a> <span><i class="iconfont"></i> 6087</span>
</dd>
<dd>
<a href="">Java实现LayIM后端的核心代码</a> <span><i class="iconfont"></i> 767</span>
</dd>
<dd>
<a href="">使用 layui 秒搭后台大布局(基本结构)</a> <span><i class="iconfont"></i> 6087</span>
</dd>
<dd>
<a href="">Java实现LayIM后端的核心代码</a> <span><i class="iconfont"></i> 767</span>
</dd>
<dd>
<a href="">使用 layui 秒搭后台大布局(基本结构)</a> <span><i class="iconfont"></i> 6087</span>
</dd>
<dd>
<a href="">Java实现LayIM后端的核心代码</a> <span><i class="iconfont"></i> 767</span>
</dd>
<dd>
<a href="">使用 layui 秒搭后台大布局(基本结构)</a> <span><i class="iconfont"></i> 6087</span>
</dd>
<dd>
<a href="">Java实现LayIM后端的核心代码</a> <span><i class="iconfont"></i> 767</span>
</dd>
</dl> <dl class="fly-panel fly-list-one">
<dt class="fly-panel-title">近期热议</dt>
<dd>
<a href="">使用 layui 秒搭后台大布局之基本结构</a> <span><i class="iconfont"></i> 96</span>
</dd>
<dd>
<a href="">使用 layui 秒搭后台大布局之基本结构</a> <span><i class="iconfont"></i> 96</span>
</dd>
<dd>
<a href="">使用 layui 秒搭后台大布局之基本结构</a> <span><i class="iconfont"></i> 96</span>
</dd>
<dd>
<a href="">使用 layui 秒搭后台大布局之基本结构</a> <span><i class="iconfont"></i> 96</span>
</dd>
<dd>
<a href="">使用 layui 秒搭后台大布局之基本结构</a> <span><i class="iconfont"></i> 96</span>
</dd>
<dd>
<a href="">使用 layui 秒搭后台大布局之基本结构</a> <span><i class="iconfont"></i> 96</span>
</dd>
<dd>
<a href="">使用 layui 秒搭后台大布局之基本结构</a> <span><i class="iconfont"></i> 96</span>
</dd>
<dd>
<a href="">使用 layui 秒搭后台大布局之基本结构</a> <span><i class="iconfont"></i> 96</span>
</dd>
</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>
</body>
</html>

detail.jsp

  当浏览一个帖子时,帖子的浏览数目需要+1  (数据库中glanceover+1)

  Service层中findAllPasteByIdReturnPaste()编写传送数据到Dao层修改数据库中glanceover语句

    public Paste findAllPasteByIdReturnPaste(String pasteid) {

        pasteDao.addPasteGlanceover(pasteid);
//SQL语句写法:update paste set glanceover = glanceover+1 where id = ?; return pasteDao.findPasteByIdReturnPaste(pasteid);
}

  帖子回复glanceover+1

public void addPasteGlanceover(String pasteid) {
Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
String sql = "update paste set glanceover = glanceover+1 where id = ?";
NativeQuery query = session.createSQLQuery(sql);
query.setParameter(1, pasteid);
query.executeUpdate();
}

JavaWeb_(SSH论坛)_五、帖子模块-LMLPHP

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 Paste findAllPasteByIdReturnPaste(String pasteid) { pasteDao.addPasteGlanceover(pasteid);
//SQL语句写法:update paste set glanceover = glanceover+1 where id = ?; return pasteDao.findPasteByIdReturnPaste(pasteid);
} 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;
} }

PasteService.java

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;
} //查看帖子
public Paste findPasteByIdReturnPaste(String pasteid) {
Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
String sql = "select * from paste where id = ?";
NativeQuery query = session.createSQLQuery(sql);
query.addEntity(Paste.class);
query.setParameter(1, pasteid);
Paste result = (Paste) query.uniqueResult();
return result;
} //帖子回复glanceover+1
public void addPasteGlanceover(String pasteid) {
Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
String sql = "update paste set glanceover = glanceover+1 where id = ?";
NativeQuery query = session.createSQLQuery(sql);
query.setParameter(1, pasteid);
query.executeUpdate();
} }

PasteDao.java

  为了尽可能少的访问数据,利用数据持久状态

    public Paste findPasteByIdReturnPaste(String pasteid) {

        //pasteDao.addPasteGlanceover(pasteid);
//数据库SQL语句:update paste set glanceover = glanceover+1 where id = ?;
Paste paste = pasteDao.findPasteByIdReturnPaste(pasteid);
paste.setGlanceover(paste.getGlanceover()+1);
return paste;
}

  编写帖子模块最近热帖和最近热议

  帖子模块的最近热帖和最近热议应该动态从数据库中获取

  PasteAction.java

 //查看帖子详细信息
public String getDetail() throws Exception{ //得到最近热帖
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); return "detail";
}

  修改detail.jsp

        <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>

JavaWeb_(SSH论坛)_五、帖子模块-LMLPHP

  Web层

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;
}
}

GetDataAction.java

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.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 PasteService pasteService;
private String pasteid; //查看帖子详细信息
public String getDetail() throws Exception{ //得到最近热帖
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); return "detail";
} //添加帖子
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;
} }

PasteAction.java

  前端页面

<%@ 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>
<li data-id="13"><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 style="color: #FF9E3F">活雷锋</em>
</cite>
</a>
<div class="detail-hits">
<span>刚刚</span>
</div>
</div>
<div class="detail-body jieda-body">
<p>蓝瘦</p>
</div>
<div class="jieda-reply">
<span class="jieda-zan" type="zan"><i class="iconfont icon-zan"></i><em>0</em> </span>
<div class="jieda-admin">
<span type="del"><a href="#" class="layui-btn layui-btn-danger layui-btn-small">删除</a></span> <span class="jieda-accept" type="accept"> <a href="#" class="layui-btn layui-btn-small">采纳</a></span>
</div>
</div></li> <!-- <li class="fly-none">没有任何回答</li> -->
</ul>
<span id="toName">@ 压缩(楼主)</span>
<div class="layui-form layui-form-pane">
<form action="">
<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>
</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>
</body>
</html>

detail.jsp

分析回复帖子模块

JavaWeb_(SSH论坛)_五、帖子模块-LMLPHP

  创建回复帖子Action骨架

public class AnswerAction extends ActionSupport implements ModelDriven<Answer>{

    public Answer answer = new Answer();
private AnswerService answerService; public String addAnswer() throws Exception { answerService.addAnswer(answer); return "success";
} public Answer getModel() { return answer;
} }

  完成Answer实体

    private String id;
//内容
private String content;
//回复时间
private String anstime;
//同意数
private Integer agree;
//是否解决
private Integer solve; private User user; private Paste paste; //登录的用户是否点赞了改回复
private Integer loginUserIsAgree;
package com.Gary.domain;

public class Answer {

    private String id;
//内容
private String content;
//回复时间
private String anstime;
//同意数
private Integer agree;
//是否解决
private Integer solve; private User user; private Paste paste; //登录的用户是否点赞了改回复
private Integer loginUserIsAgree; public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content.replaceAll("<br />", ".");;
}
public String getAnstime() {
return anstime;
}
public void setAnstime(String anstime) {
this.anstime = anstime;
}
public Integer getAgree() {
return agree;
}
public void setAgree(Integer agree) {
this.agree = agree;
}
public Integer getSolve() {
return solve;
}
public void setSolve(Integer solve) {
this.solve = solve;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Paste getPaste() {
return paste;
}
public void setPaste(Paste paste) {
this.paste = paste;
}
public Integer getLoginUserIsAgree() {
return loginUserIsAgree;
}
public void setLoginUserIsAgree(Integer loginUserIsAgree) {
this.loginUserIsAgree = loginUserIsAgree;
} }

Answer.java

  Web层

package com.Gary.web;

import com.Gary.domain.Answer;
import com.Gary.service.AnswerService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven; public class AnswerAction extends ActionSupport implements ModelDriven<Answer>{ public Answer answer = new Answer();
private AnswerService answerService; public String addAnswer() throws Exception { answerService.addAnswer(answer); return "success";
} public Answer getModel() { return answer;
} }

AnswerAction.java

  Service层

package com.Gary.service;

import com.Gary.dao.AnswerDao;
import com.Gary.domain.Answer; public class AnswerService { private AnswerDao answerDao; public AnswerDao getAnswerDao() {
return answerDao;
} public void setAnswerDao(AnswerDao answerDao) {
this.answerDao = answerDao;
} public void addAnswer(Answer answer) {
// TODO Auto-generated method stub
answerDao.addAnswer(answer);
} }

AnswerService.java

  Dao层

package com.Gary.dao;

import org.hibernate.Session;
import org.hibernate.query.NativeQuery;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport; import com.Gary.domain.Answer; public class AnswerDao extends HibernateDaoSupport{ public void addAnswer(Answer answer) {
Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
session.save(answer);
} }

AnswerDao.java

  User.java用户模块中添加一个用户回复Set<Paste> answerPasteSet

    //用户回复
private Set<Paste> answerPasteSet = new HashSet<Paste>(); public Set<Paste> getAnswerPasteSet() {
return answerPasteSet;
}
public void setAnswerPasteSet(Set<Paste> answerPasteSet) {
this.answerPasteSet = answerPasteSet;
}

  在User.hbm.xml中添加一对多的关系

        <set name="answerPasteSet">
<key column="pasteid"></key>
<one-to-many class="Answer"/>
</set>

  分析用户帖子一对多的关系

JavaWeb_(SSH论坛)_五、帖子模块-LMLPHP

  在Paste.java中添加HashSet<User>用户回复

    private Set<User> userAnswerSet = new HashSet<User>();

    public Set<User> getUserAnswerSet() {
return userAnswerSet;
}
public void setUserAnswerSet(Set<User> userAnswerSet) {
this.userAnswerSet = userAnswerSet;
}

  Paste.hbm.xml中绑定用户与表一对多的关系

     <set name="userAnswerSet">
<!-- 外键的类名 -->
<key column="userid"></key>
<!-- 外键的类名的类 -->
<one-to-many class="Answer"/>
</set>

  

<?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="Answer" table="answer">
<id name="id">
<generator class="uuid"></generator>
</id> <property name="content" column="content"></property>
<property name="anstime" column="anstime"></property>
<property name="agree" column="agree"></property>
<property name="solve" column="solve"></property> <many-to-one name="user" column="userid" class="User"></many-to-one>
<!-- colum为外键的字段名字 -->
<many-to-one name="paste" column="pasteid" class="Paste"></many-to-one> </class>
</hibernate-mapping>

Answer.hbm.xml

<?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> <set name="userAnswerSet">
<!-- 外键的类名 -->
<key column="userid"></key>
<!-- 外键的类名的类 -->
<one-to-many class="Answer"/>
</set> </class>
</hibernate-mapping>

Paste.hbm.xml

<?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="Answer" table="answer">
<id name="id">
<generator class="uuid"></generator>
</id> <property name="content" column="content"></property>
<property name="anstime" column="anstime"></property>
<property name="agree" column="agree"></property>
<property name="solve" column="solve"></property> <many-to-one name="user" column="userid" class="User"></many-to-one>
<!-- colum为外键的字段名字 -->
<many-to-one name="paste" column="pasteid" class="Paste"></many-to-one> </class>
</hibernate-mapping>

Answer.hbm.xml

JavaWeb_(SSH论坛)_五、帖子模块-LMLPHP

  此时点击提交回答按钮后表单无法发送问题!!!

  修改detail中的表单

<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"/>">
<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>

  可以看到,修改完表单后,在未登录的轻快下,点击提交回答按钮后页面会自动跳转到登陆页面

JavaWeb_(SSH论坛)_五、帖子模块-LMLPHP

<%@ 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>
<li data-id="13"><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 style="color: #FF9E3F">活雷锋</em>
</cite>
</a>
<div class="detail-hits">
<span>刚刚</span>
</div>
</div>
<div class="detail-body jieda-body">
<p>蓝瘦</p>
</div>
<div class="jieda-reply">
<span class="jieda-zan" type="zan"><i class="iconfont icon-zan"></i><em>0</em> </span>
<div class="jieda-admin">
<span type="del"><a href="#" class="layui-btn layui-btn-danger layui-btn-small">删除</a></span> <span class="jieda-accept" type="accept"> <a href="#" class="layui-btn layui-btn-small">采纳</a></span>
</div>
</div></li> <!-- <li class="fly-none">没有任何回答</li> -->
</ul>
<span id="toName">@ 压缩(楼主)</span>
<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}/PasteAction_addPaste">
<input type="hidden" name="pasteid" value="<s:property value="#paste.id"/>">
<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>

detail.jsp

  测试登陆用户发帖模块

JavaWeb_(SSH论坛)_五、帖子模块-LMLPHP

  

<%@ 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>
<li data-id="13"><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 style="color: #FF9E3F">活雷锋</em>
</cite>
</a>
<div class="detail-hits">
<span>刚刚</span>
</div>
</div>
<div class="detail-body jieda-body">
<p>蓝瘦</p>
</div>
<div class="jieda-reply">
<span class="jieda-zan" type="zan"><i class="iconfont icon-zan"></i><em>0</em> </span>
<div class="jieda-admin">
<span type="del"><a href="#" class="layui-btn layui-btn-danger layui-btn-small">删除</a></span> <span class="jieda-accept" type="accept"> <a href="#" class="layui-btn layui-btn-small">采纳</a></span>
</div>
</div></li> <!-- <li class="fly-none">没有任何回答</li> -->
</ul>
<span id="toName">@ 压缩(楼主)</span>
<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"/>">
<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>

detail.jsp

  此时发现跳转过来的页面全部问题、最近热帖、最近热议、专家排行榜都是空的白的,接下来需要实现重定向到Action

  添加addAnswer()请求,如果用户成功回复,则返回"toDetail“

    public String addAnswer() throws Exception {

        User user =    (User) ActionContext.getContext().getSession().get("user");
if(user == null)
{
ActionContext.getContext().put("error", "未登录不能回复!!");
return "error";
} // //回复时间
// private String anstime;
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String anstime = format.format(date);
answer.setAnstime(anstime);
// //同意数
// private Integer agree;
answer.setAgree(0);
// //是否解决
// private Integer solve;
answer.setSolve(0);
//
// private User user;
answer.setUser(user);
//
// private Paste paste;
Paste paste = pasteService.findPasteByIdReturnPaste(pasteid);
answer.setPaste(paste); answerService.addAnswer(answer,paste);
answerService.addAnswer(answer); return "toDetail";
}

  在struts.xml中配置toDetail结果集成重定向到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>
</package>

JavaWeb_(SSH论坛)_五、帖子模块-LMLPHP

package com.Gary.web;

import java.text.SimpleDateFormat;
import java.util.Date;
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.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven; public class AnswerAction extends ActionSupport implements ModelDriven<Answer>{ public Answer answer = new Answer();
private AnswerService answerService;
private String pasteid;
private PasteService pasteService; public AnswerService getAnswerService() {
return answerService;
} public void setAnswerService(AnswerService answerService) {
this.answerService = answerService;
} public PasteService getPasteService() {
return pasteService;
} public void setPasteService(PasteService pasteService) {
this.pasteService = pasteService;
} public String getPasteid() {
return pasteid;
} public void setPasteid(String pasteid) {
this.pasteid = pasteid;
} public String addAnswer() throws Exception { User user = (User) ActionContext.getContext().getSession().get("user");
if(user == null)
{
ActionContext.getContext().put("error", "未登录不能回复!!");
return "error";
} // //回复时间
// private String anstime;
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String anstime = format.format(date);
answer.setAnstime(anstime);
// //同意数
// private Integer agree;
answer.setAgree(0);
// //是否解决
// private Integer solve;
answer.setSolve(0);
//
// private User user;
answer.setUser(user);
//
// private Paste paste;
Paste paste = pasteService.findPasteByIdReturnPaste(pasteid);
answer.setPaste(paste); answerService.addAnswer(answer,paste);
answerService.addAnswer(answer); return "toDetail";
} public Answer getModel() { return answer;
} }

AnswerAction.java

<?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> <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>
</package> </struts>

struts.xml

查看所有回复

  分析查看回复

JavaWeb_(SSH论坛)_五、帖子模块-LMLPHP

  Web层

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.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; //查看帖子详细信息
public String getDetail() throws Exception{ //得到最近热帖
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);
ActionContext.getContext().put("answerList", answerList); return "detail";
} 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;
} }

PasteAction.java

  Service层

package com.Gary.service;

import java.util.List;

import com.Gary.dao.AnswerDao;
import com.Gary.domain.Answer;
import com.Gary.domain.Paste; public class AnswerService { private AnswerDao answerDao; public List<Answer> findAllAnswerByPasteid(String pasteid) {
List<Answer> answerList = answerDao.findAllAnswerByPasteid(pasteid);
return answerList;
} public AnswerDao getAnswerDao() {
return answerDao;
} public void setAnswerDao(AnswerDao answerDao) {
this.answerDao = answerDao;
} public void addAnswer(Answer answer) {
// TODO Auto-generated method stub
answerDao.addAnswer(answer);
} public void addAnswer(Answer answer, Paste paste) { paste.setAnsnum(paste.getAnsnum() + 1);
answerDao.addAnswer(answer);
} }

AnswerService.java

  Dao层

package com.Gary.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.query.NativeQuery;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport; import com.Gary.domain.Answer; public class AnswerDao extends HibernateDaoSupport{ public void addAnswer(Answer answer) {
Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
session.save(answer);
} public List<Answer> findAllAnswerByPasteid(String pasteid) {
Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
String sql = "select * from answer where pasteid = ?";
NativeQuery query = session.createSQLQuery(sql);
query.addEntity(Answer.class);
query.setParameter(1, pasteid);
List<Answer> list = query.list();
return list;
} }

AnswerDao.java

  前端样式的美化

                    <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>

JavaWeb_(SSH论坛)_五、帖子模块-LMLPHP

<%@ 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>
</div>
<div class="detail-body jieda-body">
<p><s:property value="#answer.content" /></p>
</div>
<div class="jieda-reply">
<span class="jieda-zan" type="zan"><i class="iconfont icon-zan"></i>
<em> <s:property value="#answer.agree" /></em>
</span>
<div class="jieda-admin"> <span type="del"><a href="#" class="layui-btn layui-btn-danger layui-btn-small">删除</a></span> <span class="jieda-accept" type="accept"> <a href="#" class="layui-btn layui-btn-small">采纳</a></span>
</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>

detail.jsp

  删除与采纳的显示

  判断当前回复帖子人是登陆名自己的,显示可以删除帖子

    <s:if test="#session.user.username==#answer.user.username ">
      <span type="del"><a href="#" 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="#" class="layui-btn layui-btn-small">采纳</a></span>
    </s:if>

JavaWeb_(SSH论坛)_五、帖子模块-LMLPHP

<%@ 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>
</div>
<div class="detail-body jieda-body">
<p><s:property value="#answer.content" /></p>
</div>
<div class="jieda-reply">
<span class="jieda-zan" type="zan"><i class="iconfont icon-zan"></i>
<em> <s:property value="#answer.agree" /></em>
</span>
<div class="jieda-admin">
<s:if test="#session.user.username==#answer.user.username ">
<span type="del"><a href="#" 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="#" 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>

detail.jsp

分析删除回复

JavaWeb_(SSH论坛)_五、帖子模块-LMLPHP

  删除和采纳

                                    <div class="jieda-admin">
<s:if test="#session.user.username==#answer.user.username ">
<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="#" class="layui-btn layui-btn-small">采纳</a></span>
</s:if>
</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">
<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>
</div>
<div class="detail-body jieda-body">
<p>
<s:property value="#answer.content" />
</p>
</div>
<div class="jieda-reply">
<span class="jieda-zan" type="zan"><i class="iconfont icon-zan"></i> <em> <s:property value="#answer.agree" /></em> </span>
<div class="jieda-admin">
<s:if test="#session.user.username==#answer.user.username ">
<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="#" 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>

detail.jsp

  Web层:当增加帖子时,帖子数+1

//删除answer
public String deleteAnswer() throws Exception { Paste paste = pasteService.findPasteByIdReturnPaste(pasteid); answerService.deleteAnswerById(answerid,paste);
ActionContext.getContext().put("pasteid", pasteid);
return "toDetail";
} //添加answer
public String addAnswer() throws Exception { User user = (User) ActionContext.getContext().getSession().get("user");
if(user == null)
{
ActionContext.getContext().put("error", "未登录不能回复!!");
return "error";
}
package com.Gary.web;

import java.text.SimpleDateFormat;
import java.util.Date;
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.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven; public class AnswerAction extends ActionSupport implements ModelDriven<Answer>{ public Answer answer = new Answer();
private AnswerService answerService;
private String pasteid;
private PasteService pasteService;
private String answerid; public String getAnswerid() {
return answerid;
} public void setAnswerid(String answerid) {
this.answerid = answerid;
} public AnswerService getAnswerService() {
return answerService;
} public void setAnswerService(AnswerService answerService) {
this.answerService = answerService;
} public PasteService getPasteService() {
return pasteService;
} public void setPasteService(PasteService pasteService) {
this.pasteService = pasteService;
} public String getPasteid() {
return pasteid;
} public void setPasteid(String pasteid) {
this.pasteid = pasteid;
} //删除answer
public String deleteAnswer() throws Exception { Paste paste = pasteService.findPasteByIdReturnPaste(pasteid); answerService.deleteAnswerById(answerid,paste);
ActionContext.getContext().put("pasteid", pasteid);
return "toDetail";
} //添加answer
public String addAnswer() throws Exception { User user = (User) ActionContext.getContext().getSession().get("user");
if(user == null)
{
ActionContext.getContext().put("error", "未登录不能回复!!");
return "error";
} // //回复时间
// private String anstime;
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String anstime = format.format(date);
answer.setAnstime(anstime);
// //同意数
// private Integer agree;
answer.setAgree(0);
// //是否解决
// private Integer solve;
answer.setSolve(0);
//
// private User user;
answer.setUser(user);
//
// private Paste paste;
Paste paste = pasteService.findPasteByIdReturnPaste(pasteid);
answer.setPaste(paste); answerService.addAnswer(answer,paste); return "toDetail";
} public Answer getModel() { return answer;
} }

AnswerAction.java

  Service层

    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);
}
package com.Gary.service;

import java.util.List;

import com.Gary.dao.AnswerDao;
import com.Gary.domain.Answer;
import com.Gary.domain.Paste; public class AnswerService { private AnswerDao answerDao; public List<Answer> findAllAnswerByPasteid(String pasteid) {
List<Answer> 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;
} }

AnswerService.java

  Dao层:操作数据库

public void addAnswer(Answer answer) {
Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
session.save(answer);
} public List<Answer> findAllAnswerByPasteid(String pasteid) {
Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
String sql = "select * from answer where pasteid = ?";
NativeQuery query = session.createSQLQuery(sql);
query.addEntity(Answer.class);
query.setParameter(1, pasteid);
List<Answer> list = query.list();
return list;
} public void deleteAnswerById(String answerid) {
Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
String sql = "delete from answer where id = ?";
NativeQuery query = session.createSQLQuery(sql);
query.setParameter(1, answerid);
query.executeUpdate(); //session.delete(answerid, Answer.class); }
package com.Gary.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.query.NativeQuery;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport; import com.Gary.domain.Answer; public class AnswerDao extends HibernateDaoSupport{ public void addAnswer(Answer answer) {
Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
session.save(answer);
} public List<Answer> findAllAnswerByPasteid(String pasteid) {
Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
String sql = "select * from answer where pasteid = ?";
NativeQuery query = session.createSQLQuery(sql);
query.addEntity(Answer.class);
query.setParameter(1, pasteid);
List<Answer> list = query.list();
return list;
} public void deleteAnswerById(String answerid) {
Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
String sql = "delete from answer where id = ?";
NativeQuery query = session.createSQLQuery(sql);
query.setParameter(1, answerid);
query.executeUpdate(); //session.delete(answerid, Answer.class); } }

AnswerDao.java

  可以看到,进入帖子后,帖子浏览数目+1,当增加回帖时,帖子数+1,当删除回帖时,帖子数-1

JavaWeb_(SSH论坛)_五、帖子模块-LMLPHP

  添加属性与元数据字段

    private String answerid;
        <property name="answerid" column="answerid"></property>

  

  分析采纳帖子

JavaWeb_(SSH论坛)_五、帖子模块-LMLPHP

  detail.jsp

                                        <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>

  Web层:采纳用户回帖

    public String solvePaste() throws Exception{

        pasteService.solvePasteByIdAndAnswerid(pasteid,paste.getAnswerid());
ActionContext.getContext().put("pasteid", pasteid); return "toDetail";
}

  Service层:获得Web层数据传递到Dao层

    public void solvePasteByIdAndAnswerid(String pasteid, String answerid) {
pasteDao.solvePasteByIdAndAnswerid(pasteid,answerid);
}

  Dao层:进行数据的处理

    public void solvePasteByIdAndAnswerid(String pasteid, String answerid) {
Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
String sql ="update paste set solve = 1, answerid = ? where id = ?";
NativeQuery query = session.createSQLQuery(sql);
query.setParameter(1, answerid);
query.setParameter(2, pasteid);
query.executeUpdate();
}

JavaWeb_(SSH论坛)_五、帖子模块-LMLPHP

<%@ 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>
</div>
<div class="detail-body jieda-body">
<p>
<s:property value="#answer.content" />
</p>
</div>
<div class="jieda-reply">
<span class="jieda-zan" type="zan"><i class="iconfont icon-zan"></i> <em> <s:property value="#answer.agree" /></em> </span>
<div class="jieda-admin">
<s:if test="#session.user.username==#answer.user.username ">
<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 ">
<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>
</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>

detail.jsp

  实现采纳问题

  <div class="jieda-admin">
    <s:if test="#session.user.username==#answer.user.username ">
      <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 ">
      <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>
    </s:if>
  </div>

  Service层

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;
}
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 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;
}
}

AnswerService.java

  Dao层

    public Answer findAnswerById(String answerid) {
Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
String sql = "select * from answer where id = ?";
NativeQuery query = session.createSQLQuery(sql);
query.addEntity(Answer.class);
query.setParameter(1, answerid);
Answer result = (Answer) query.uniqueResult();
return result;
}
package com.Gary.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.query.NativeQuery;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport; import com.Gary.domain.Answer; public class AnswerDao extends HibernateDaoSupport{ public void addAnswer(Answer answer) {
Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
session.save(answer);
} public List<Answer> findAllAnswerByPasteid(String pasteid) {
Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
String sql = "select * from answer where pasteid = ?";
NativeQuery query = session.createSQLQuery(sql);
query.addEntity(Answer.class);
query.setParameter(1, pasteid);
List<Answer> list = query.list();
return list;
} public void deleteAnswerById(String answerid) {
Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
String sql = "delete from answer where id = ?";
NativeQuery query = session.createSQLQuery(sql);
query.setParameter(1, answerid);
query.executeUpdate(); //session.delete(answerid, Answer.class); } public Answer findAnswerById(String answerid) {
Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
String sql = "select * from answer where id = ?";
NativeQuery query = session.createSQLQuery(sql);
query.addEntity(Answer.class);
query.setParameter(1, answerid);
Answer result = (Answer) query.uniqueResult();
return result;
} }

AnswerDao.java

JavaWeb_(SSH论坛)_五、帖子模块-LMLPHP

  发现当采纳问题后,应该隐藏其它问题的采纳按钮,已采纳的问题也不可删除

    <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>

  添加采纳金币模块

  <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>

  Service层

    public void solvePasteByIdAndAnswerid(String pasteid, String answerid) {
//给回复的人添加金币
Paste paste = pasteDao.findPasteByIdReturnPaste(pasteid);
//通过answerid查找user
Answer answer = answerDao.findAnswerById(answerid);
//通过userid找user对象
//User user = userDao.finduserByIdReturnUser(answer.getUser().getId());
//获得answer下的user对象(持久化数据类型)
User user = answer.getUser();
//因为是持久化类型对象,即可直接修改属性,同步到数据库
user.setCoin(user.getCoin()+paste.getOffer());
//解决问题
pasteDao.solvePasteByIdAndAnswerid(pasteid,answerid);
}
package com.Gary.service;

import java.util.List;

import com.Gary.dao.AnswerDao;
import com.Gary.dao.PasteDao;
import com.Gary.dao.UserDao;
import com.Gary.domain.Answer;
import com.Gary.domain.Paste;
import com.Gary.domain.User;
import com.Gary.utils.PageBean; public class PasteService { private PasteDao pasteDao;
private AnswerDao answerDao;
private UserDao userDao; public void solvePasteByIdAndAnswerid(String pasteid, String answerid) {
//给回复的人添加金币
Paste paste = pasteDao.findPasteByIdReturnPaste(pasteid);
//通过answerid查找user
Answer answer = answerDao.findAnswerById(answerid);
//通过userid找user对象
//User user = userDao.finduserByIdReturnUser(answer.getUser().getId());
//获得answer下的user对象(持久化数据类型)
User user = answer.getUser();
//因为是持久化类型对象,即可直接修改属性,同步到数据库
user.setCoin(user.getCoin()+paste.getOffer());
//解决问题
pasteDao.solvePasteByIdAndAnswerid(pasteid,answerid);
} public AnswerDao getAnswerDao() {
return answerDao;
} public void setAnswerDao(AnswerDao answerDao) {
this.answerDao = answerDao;
} public UserDao getUserDao() {
return userDao;
} public void setUserDao(UserDao userDao) {
this.userDao = userDao;
} public Paste findPasteByIdReturnPaste(String pasteid) { //pasteDao.addPasteGlanceover(pasteid);
//数据库SQL语句:update paste set glanceover = glanceover+1 where id = ?;
Paste paste = pasteDao.findPasteByIdReturnPaste(pasteid);
paste.setGlanceover(paste.getGlanceover()+1);
return paste;
} 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;
} }

AnswerDao.java

JavaWeb_(SSH论坛)_五、帖子模块-LMLPHP

<?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> <!-- 配置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> <!-- 配置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> </beans>

applicationContext.xml

05-11 22:27