JavaWeb_(SSH论坛)_七、辅助模块

 

 

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

  一、项目入门  传送门

  二、框架整合  传送门

  三、用户模块  传送门

  四、页面显示  传送门

  五、帖子模块  传送门

  六、点赞模块  传送门

  七、辅助模块  传送门

 

  为避免代码冗余,可以在struct.xml中配置全局结果集

<!-- 配置全局结果集 -->
        <global-results>
            <!-- 重定向到Action -->
            <result name="toDetail" type="redirectAction">
                <param name="actionName"> PasteAction_getDetail </param>
                <!-- struts不认识pasteid则会封装到重定向的action中 -->
                <param name="pasteid">${pasteid}</param>
            </result>
            <result name="error">/login.jsp</result>
        </global-results>

 

<?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-results>
            <!-- 重定向到Action -->
            <result name="toDetail" type="redirectAction">
                <param name="actionName"> PasteAction_getDetail </param>
                <!-- struts不认识pasteid则会封装到重定向的action中 -->
                <param name="pasteid">${pasteid}</param>
            </result>
            <result name="error">/login.jsp</result>
        </global-results>
        <!-- 允许全部方法 -->
        <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="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>

            <!-- 转发到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}">

        </action>

        <action name="PraiseAction_*" class="com.Gary.web.PraiseAction" method="{1}">

        </action>

    </package>


</struts>
struct.xml

 

  分析取消赞

  

                                    <!-- 登录用户点赞了哪些回复 -->
                                    <s:if test="#answer.loginUserIsAgree==0">
                                        <a href="${pageContext.request.contextPath }/PraiseAction_addPraise?answerid=<s:property value="#answer.id" />&pasteid=<s:property value="#paste.id" />">
                                            <span class="jieda-zan" type="zan">
                                                <i class="iconfont icon-zan"></i>
                                                <em>
                                                    <s:property value="#answer.agree" />
                                                </em>
                                            </span>
                                        </a>
                                    </s:if>
                                    <s:else>
                                    <!-- 取消赞 -->
                                        <a href="${pageContext.request.contextPath }/PraiseAction_deletePraise?answerid=<s:property value="#answer.id" />&pasteid=<s:property value="#paste.id" />">
                                            <span class="jieda-zan zanok" type="zan">

                                                <i class="iconfont icon-zan"></i>
                                                <em>
                                                    <s:property value="#answer.agree" />
                                                </em>
                                            </span>
                                        </a>
                                    </s:else>

 

   用户取消赞Web层

//取消赞
        public String deletePraise() throws Exception {
            //肯定可以获得(用户未登录不能点赞,只有登录后才可以点赞)
            User user = (User) ActionContext.getContext().getSession().get("user");
            
            //封装user
            //封装answer
            Answer answer = answerService.findAnswerByIdReturnAnswer(answerid);
            //封装primarykey
            PrimaryKey primaryKey = new PrimaryKey();
            primaryKey.setAnswer(answer);
            primaryKey.setUser(user);
            //封装praise
            Praise praise = new Praise();
            praise.setPrimaryKey(primaryKey);
            
            praiseService.deletePraise(praise);
            answerService.deleteAnswerAgree(answerid);
            
            ActionContext.getContext().put("pasteid", pasteid);
            return "toDetail";
            
        }

 

   删除赞Dao层

    public void deletePraise(Praise praise) {
        Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
        session.delete(praise);
    }

 

 

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>问题详情</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="keywords" content="fly,layui,前端社区">
<meta name="description" content="">
<link rel="stylesheet" href="res/layui/css/layui.css">
<link rel="stylesheet" href="res/css/global.css">
<script src="res/layui/layui.js"></script>
<script src="js/jquery.js"></script>
<style type="text/css" rel="stylesheet">
form {
    margin: 0;
}

.editor {
    margin-top: 5px;
    margin-bottom: 5px;
}
</style>

</head>
<body style="margin: -2px">
    <iframe src="head.jsp" scrolling="no" width="100%" height="110px"></iframe>
    <div class="main layui-clear">
        <div class="wrap">
            <div class="content detail">
                <div class="fly-panel detail-box">

                    <h1>
                        <s:property value="#paste.title" />
                    </h1>
                    <div class="fly-tip fly-detail-hint" data-id="">
                        <span class="fly-tip-stick">置顶帖</span> <span class="jie-admin"> <a href="">点击置顶</a>
                        </span> <span class="layui-btn layui-btn-mini jie-admin"> <a href="">取消置顶</a>
                        </span> <span class="jie-admin" type="del" style="margin-left: 20px;"> <a>删除该帖</a>
                        </span> </span>
                        <div class="fly-list-hint">
                            <i class="iconfont" title="回答">&#xe60c;</i>
                            <s:property value="#paste.ansnum" />
                        </div>
                    </div>
                    <div class="detail-about">
                        <a class="jie-user" href=""> <img src="${pageContext.request.contextPath }/<s:property value="#paste.user.image"/>" alt="头像"> <cite> <s:property value="#paste.user.username" /> <em> <s:property value="#paste.createtime" /> 发布
                            </em>
                        </cite>
                        </a>
                        <div class="detail-hits" data-id="{{rows.id}}">
                            <span class="layui-btn layui-btn-mini jie-admin"> <a href="#">已完帖,无法编辑</a>
                            </span> <span class="layui-btn layui-btn-mini jie-admin" type="collect" data-type="add"> <a id="collectPost">收藏</a>
                            </span> <span class="layui-btn layui-btn-mini jie-admin  layui-btn-danger" type="collect" data-type="add"> <a>取消收藏</a>
                            </span>

                        </div>
                    </div>
                    <div class="detail-body photos" style="margin-bottom: 20px;">
                        <p>
                            <s:property value="#paste.content" />
                        </p>
                    </div>
                </div>



                <div class="fly-panel detail-box" style="padding-top: 0;">
                    <a name="comment"></a>
                    <ul class="jieda photos" id="jieda">
                        <!--  <li data-id="12" class="jieda-daan"><a name="item-121212121212"></a>
                            <div class="detail-about detail-about-reply">
                                <a class="jie-user" href=""> <img src="res/images/uer.jpg" alt=""> <cite> <i>纸飞机</i> <em>(楼主)</em>
                  <em style="color:#5FB878">(管理员)</em> 
                                </cite>
                                </a>
                                <div class="detail-hits">
                                    <span>3分钟前</span>
                                </div>
                                <i class="iconfont icon-caina" title="最佳答案"></i>
                            </div>
                            <div class="detail-body jieda-body">
                                <p>么么哒</p>
                            </div>
                            <div class="jieda-reply">
                                <span class="jieda-zan zanok" type="zan"><i class="iconfont icon-zan"></i><em>12</em> </span>
                                <!-- <div class="jieda-admin">
                <span type="del">删除</span>
                <span class="jieda-accept" type="accept">采纳</span>
              </div>
                        </div></li> -->



                        <s:iterator value="#answerList" var="answer">
                            <li data-id="13"><a name="item-121212121212"></a>
                                <div class="detail-about detail-about-reply">
                                    <a class="jie-user" href=""> <img src="${pageContext.request.contextPath }/<s:property value="#answer.user.image"/>" alt=""> <cite> <i><s:property value="#answer.user.username" /></i> <em style="color: #FF9E3F">活雷锋</em>
                                    </cite>
                                    </a>
                                    <div class="detail-hits">
                                        <span> <s:property value="#answer.anstime" />
                                        </span>
                                    </div>
                                    <s:if test="#answer.id==#paste.answerid">
                                        <i class="iconfont icon-caina" title="最佳答案"></i>
                                    </s:if>
                                </div>
                                <div class="detail-body jieda-body">
                                    <p>
                                        <s:property value="#answer.content" />
                                    </p>
                                </div>
                                <div class="jieda-reply">
                                    
                                    <!-- 登录用户点赞了哪些回复 -->
                                    <s:if test="#answer.loginUserIsAgree==0">
                                        <a href="${pageContext.request.contextPath }/PraiseAction_addPraise?answerid=<s:property value="#answer.id" />&pasteid=<s:property value="#paste.id" />">
                                            <span class="jieda-zan" type="zan">
                                                <i class="iconfont icon-zan"></i>
                                                <em>
                                                    <s:property value="#answer.agree" />
                                                </em>
                                            </span>
                                        </a>
                                    </s:if>
                                    <s:else>
                                    <!-- 取消赞 -->
                                        <a href="${pageContext.request.contextPath }/PraiseAction_deletePraise?answerid=<s:property value="#answer.id" />&pasteid=<s:property value="#paste.id" />">
                                            <span class="jieda-zan zanok" type="zan">

                                                <i class="iconfont icon-zan"></i>
                                                <em>
                                                    <s:property value="#answer.agree" />
                                                </em>
                                            </span>
                                        </a>
                                    </s:else>
                                    
                                    <div class="jieda-admin">
                                        <s:if test="#session.user.username==#answer.user.username && #paste.answerid!=#answer.id">
                                            <span type="del">
                                                <a href="${pageContext.request.contextPath }/AnswerAction_deleteAnswer?answerid=<s:property value="#answer.id"/>&pasteid=<s:property value="#paste.id"/>" class="layui-btn layui-btn-danger layui-btn-small">删除</a>
                                            </span>
                                        </s:if>
                                        <s:if test="#session.user.username==#paste.user.username && #paste.solve==0">
                                            <span class="jieda-accept" type="accept">
                                                <a href="${pageContext.request.contextPath }/PasteAction_solvePaste?answerid=<s:property value="#answer.id"/>&pasteid=<s:property value="#paste.id"/>" class="layui-btn  layui-btn-small">采纳</a>
                                            </span>
                                        </s:if>
                                    </div>
                                </div></li>
                        </s:iterator>
                        <s:if test="#answerList.size()==0">
                            <li class="fly-none">没有任何回答</li>
                        </s:if>
                    </ul>
                    <div class="layui-form layui-form-pane">
                        <!-- <form action="${pageContext.request.contextPath }/AnswerAction_addAnswer">
                            <input type="hidden" name="pasteid" value="<s:property value="#paste.id"/>">
                            <div class="layui-form-item layui-form-text">
                                <div class="layui-input-block">
                                    <textarea id="L_content" name="content" required lay-verify="required" placeholder="我要回答" class="layui-textarea fly-editor" style="height: 150px;"></textarea>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <button class="layui-btn" lay-filter="*" lay-submit>提交回答</button>
                            </div>
                        </form> -->
                        <form action="${pageContext.request.contextPath}/AnswerAction_addAnswer">
                            <input type="hidden" name="pasteid" value="<s:property value="#paste.id"/>"> <label for="L_title" class="layui-form-label" style="width: 690px; height: 40px;">
                                <div style="margin-left: -580px">回答问题:</div>
                            </label>
                            <div class="layui-form-item layui-form-text">
                                <div class="layui-input-block">
                                    <div class="editor">
                                        <textarea id="content" name="content" style="width: 690px; height: 300px; visibility: hidden;"></textarea>
                                    </div>
                                </div>

                            </div>

                            <div class="layui-form-item">
                                <button class="layui-btn" lay-filter="*" lay-submit>立即发布</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>

        <div class="edge">
            <dl class="fly-panel fly-list-one">
                <dt class="fly-panel-title">最近热帖</dt>
                <s:iterator value="#glanceoverPageBean.list" var="paste">
                    <dd>

                        <a href="${pageContext.request.contextPath }/PasteAction_getDetail?pasteid=<s:property value="#paste.id"/>"> <s:property value="#paste.title" />
                        </a> <span> <i class="iconfont">&#xe60b;</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">&#xe60c;</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

 

package com.Gary.web;

import com.Gary.domain.Answer;
import com.Gary.domain.Praise;
import com.Gary.domain.User;
import com.Gary.service.AnswerService;
import com.Gary.service.PraiseService;
import com.Gary.vo.PrimaryKey;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class PraiseAction extends ActionSupport{

    private String answerid;
    private String pasteid;
    private AnswerService answerService;
    private PraiseService praiseService;
    

    
    public String getPasteid() {
        return pasteid;
    }


    public void setPasteid(String pasteid) {
        this.pasteid = pasteid;
    }

    
        //取消赞
        public String deletePraise() throws Exception {
            //肯定可以获得(用户未登录不能点赞,只有登录后才可以点赞)
            User user = (User) ActionContext.getContext().getSession().get("user");
            
            //封装user
            //封装answer
            Answer answer = answerService.findAnswerByIdReturnAnswer(answerid);
            //封装primarykey
            PrimaryKey primaryKey = new PrimaryKey();
            primaryKey.setAnswer(answer);
            primaryKey.setUser(user);
            //封装praise
            Praise praise = new Praise();
            praise.setPrimaryKey(primaryKey);
            
            praiseService.deletePraise(praise);
            answerService.deleteAnswerAgree(answerid);
            
            ActionContext.getContext().put("pasteid", pasteid);
            return "toDetail";
            
        }

    //添加赞
    public String addPraise() throws Exception{
        
        //肯定可以获得(用户未登录不能点赞,只有登录后才可以点赞)
        User user = (User) ActionContext.getContext().getSession().get("user");
        
        if(user == null)
        {
            ActionContext.getContext().put("error", "未登录不允许点赞!!");
            return "error";
        }
        //根据answerid查找Answer对象
        Answer answer = answerService.findAnswerByIdReturnAnswer(answerid);
        //申明联合主键
        PrimaryKey primaryKey = new PrimaryKey();
        //放置对象
        primaryKey.setAnswer(answer);
        primaryKey.setUser(user);
        //创建赞的类
        Praise praise = new Praise();
        //将联合主键放入类的对象中
        praise.setPrimaryKey(primaryKey);
        
        praiseService.addPraise(praise);
        answerService.addAnswerAgree(answerid);

        ActionContext.getContext().put("pasteid", pasteid);
        return "toDetail";
    }


    public AnswerService getAnswerService() {
        return answerService;
    }


    public void setAnswerService(AnswerService answerService) {
        this.answerService = answerService;
    }


    public PraiseService getPraiseService() {
        return praiseService;
    }


    public void setPraiseService(PraiseService praiseService) {
        this.praiseService = praiseService;
    }


    public String getAnswerid() {
        return answerid;
    }


    public void setAnswerid(String answerid) {
        this.answerid = answerid;
    }
    
}
PraiseAction.java

 

package com.Gary.service;

import com.Gary.dao.PraiseDao;
import com.Gary.domain.Praise;

public class PraiseService {

    private PraiseDao praiseDao;
    

    public boolean findPraiseByIdReturnPraise(String userid, String answerid) {
        Praise praise = praiseDao.findPraiseByIdReturnPraise(userid,answerid);
        return praise==null?false:true;
    }
    
    public void addPraise(Praise praise) {
        praiseDao.addPraise(praise);
        
    }

    public PraiseDao getPraiseDao() {
        return praiseDao;
    }

    public void setPraiseDao(PraiseDao praiseDao) {
        this.praiseDao = praiseDao;
    }

    public void deletePraise(Praise praise) {
        praiseDao.deletePraise(praise);
    }


}
PraiseService.java

 

package com.Gary.service;

import java.util.List;

import com.Gary.dao.AnswerDao;
import com.Gary.dao.PasteDao;
import com.Gary.domain.Answer;
import com.Gary.domain.Paste;

public class AnswerService {



    private AnswerDao answerDao;
    private PasteDao pasteDao;
    

    public void addAnswerAgree(String answerid) {
        Answer answer = answerDao.findAnswerById(answerid);
        answer.setAgree(answer.getAgree() + 1);
        //第二种方式sql
        //update answer set agree = agree +1 where answer id = ?
    }
    
    //根据answerid查找answer对象
    public Answer findAnswerByIdReturnAnswer(String answerid) {
        Answer answer = answerDao.findAnswerById(answerid);

        return answer;
    }
    
    public List<Answer> findAllAnswerByPasteid(String pasteid) {
        
        Paste paste = pasteDao.findPasteByIdReturnPaste(pasteid);
        List<Answer> answerList = null;
        // 判断paste中的solve是否为1
        if (paste.getSolve() == 1) {
            // 查找最佳答案
            Answer answer = answerDao.findAnswerById(paste.getAnswerid());
            // 查找所有答案
            answerList = answerDao.findAllAnswerByPasteid(pasteid);
            // 查找所有答案中包括了最佳答案所以将最佳答案删除
            answerList.remove(answer);
            // 在头元素中插入最佳答案
            answerList.add(0, answer);
        } else {
            answerList = answerDao.findAllAnswerByPasteid(pasteid);
        }

        return answerList;
    }
    
    public void addAnswer(Answer answer, Paste paste) {

        paste.setAnsnum(paste.getAnsnum() + 1);
        answerDao.addAnswer(answer);
    }
    
    public void deleteAnswerById(String answerid, Paste paste) {
        paste.setAnsnum(paste.getAnsnum()-1);
        answerDao.deleteAnswerById(answerid);
    }
    
    public AnswerDao getAnswerDao() {
        return answerDao;
    }

    public void setAnswerDao(AnswerDao answerDao) {
        this.answerDao = answerDao;
    }

    public PasteDao getPasteDao() {
        return pasteDao;
    }

    public void setPasteDao(PasteDao pasteDao) {
        this.pasteDao = pasteDao;
    }

    public void deleteAnswerAgree(String answerid) {
        Answer answer = answerDao.findAnswerById(answerid);
        answer.setAgree(answer.getAgree() - 1);
    }


}
AnswerService.java

 

package com.Gary.dao;

import org.hibernate.Session;
import org.hibernate.query.NativeQuery;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

import com.Gary.domain.Praise;

public class PraiseDao extends HibernateDaoSupport{

    //添加赞
    public void addPraise(Praise praise) {
        Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
        session.save(praise);    
    }

    //根据id找praise
    public Praise findPraiseByIdReturnPraise(String userid, String answerid) {
        Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
        String sql = "select * from praise where userid = ? and answerid = ?";
        NativeQuery query = session.createSQLQuery(sql);
        query.addEntity(Praise.class);
        query.setParameter(1, userid);
        query.setParameter(2, answerid);
        Praise result = (Praise) query.uniqueResult();
        return result;
    }

    public void deletePraise(Praise praise) {
        Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
        session.delete(praise);
    }

}
PraiseDao

 

posted @ 2018-11-14 00:17  Cynical丶Gary  阅读(341)  评论(2编辑  收藏  举报