首页>代码>基于Spring+Spring MVC+Mybatis开发的社区论坛系统>/Genesis-master/src/main/java/com/withstars/controller/TopicController.java
001package com.withstars.controller;
002 
003import com.withstars.domain.Reply;
004import com.withstars.domain.Tab;
005import com.withstars.domain.Topic;
006import com.withstars.domain.User;
007import com.withstars.service.impl.ReplyServiceImpl;
008import com.withstars.service.impl.TabServiceImpl;
009import com.withstars.service.impl.TopicServiceImpl;
010import com.withstars.service.impl.UserServiceImpl;
011import org.springframework.beans.factory.annotation.Autowired;
012import org.springframework.stereotype.Controller;
013import org.springframework.web.bind.annotation.PathVariable;
014import org.springframework.web.bind.annotation.RequestMapping;
015import org.springframework.web.bind.annotation.RequestMethod;
016import org.springframework.web.servlet.ModelAndView;
017import org.springframework.web.servlet.mvc.support.RedirectAttributes;
018 
019import javax.servlet.http.HttpServletRequest;
020import javax.servlet.http.HttpSession;
021import java.util.Date;
022import java.util.List;
023 
024 
025import org.apache.commons.logging.Log;
026import org.apache.commons.logging.LogFactory;
027 
028/**
029 * 主题相关控制类
030 */
031@Controller
032public class TopicController {
033 
034    @Autowired
035    public TopicServiceImpl topicService;
036    @Autowired
037    public ReplyServiceImpl replyService;
038    @Autowired
039    public UserServiceImpl userService;
040    @Autowired
041    public TabServiceImpl tabService;
042 
043    //log4j对象
044    private final Log log = LogFactory.getLog(getClass());
045 
046    /**
047     * 渲染首页
048     * @param session
049     * @return
050     */
051    @RequestMapping("/")
052    public ModelAndView toMain(HttpSession session){
053        ModelAndView indexPage=new ModelAndView("cate");
054        //全部主题
055        List<Topic> topics=topicService.listTopicsAndUsers();
056 
057        //获取统计信息
058        int topicsNum=topicService.getTopicsNum();
059        int usersNum=userService.getUserCount();
060        //获取用户信息
061        Integer uid=(Integer) session.getAttribute("userId");
062        User user=userService.getUserById(uid);
063        //最热主题
064        List<Topic> hotestTopics=topicService.listMostCommentsTopics();
065 
066        indexPage.addObject("topics",topics);
067        indexPage.addObject("hotestTopics",hotestTopics);
068        indexPage.addObject("topicsNum",topicsNum);
069        indexPage.addObject("usersNum",usersNum);
070        indexPage.addObject("user",user);
071        return  indexPage;
072    }
073 
074    /**
075     * 渲染主题详细页面
076     * @param id
077     * @param session
078     * @return
079     */
080    @RequestMapping("/t/{id}")
081    public ModelAndView toTopic(@PathVariable("id")Integer id,HttpSession session){
082        //点击量加一
083        boolean ifSucc=topicService.clickAddOne(id);
084        //获取主题信息
085        Topic topic=topicService.selectById(id);
086        //获取主题全部评论
087        List<Reply> replies=replyService.getRepliesOfTopic(id);
088        //获取评论数
089        int repliesNum=replyService.repliesNum(id);
090        //获取统计信息
091        int topicsNum=topicService.getTopicsNum();
092        int usersNum=userService.getUserCount();
093        //获取用户信息
094        Integer uid=(Integer) session.getAttribute("userId");
095        User user=userService.getUserById(uid);
096        //最热主题
097        List<Topic> hotestTopics=topicService.listMostCommentsTopics();
098 
099        //渲染视图
100        ModelAndView topicPage=new ModelAndView("detail");
101        topicPage.addObject("topic",topic);
102        topicPage.addObject("replies",replies);
103        topicPage.addObject("repliesNum",repliesNum);
104        topicPage.addObject("topicsNum",topicsNum);
105        topicPage.addObject("usersNum",usersNum);
106        topicPage.addObject("user",user);
107        topicPage.addObject("hotestTopics",hotestTopics);
108        return topicPage;
109    }
110 
111    /**
112     * 渲染指定板块页面
113     */
114    @RequestMapping("/tab/{tabNameEn}")
115    public ModelAndView toTabPage(@PathVariable("tabNameEn")String tabNameEn,HttpSession session){
116        Tab tab=tabService.getByTabNameEn(tabNameEn);
117        Integer tabId=tab.getId();
118 
119        ModelAndView indexPage=new ModelAndView("cate");
120        //全部主题
121        List<Topic> topics=topicService.listTopicsAndUsersOfTab(tabId);
122 
123        //获取统计信息
124        int topicsNum=topicService.getTopicsNum();
125        int usersNum=userService.getUserCount();
126 
127        //获取用户信息
128        Integer uid=(Integer) session.getAttribute("userId");
129        User user=userService.getUserById(uid);
130        //最热主题
131        List<Topic> hotestTopics=topicService.listMostCommentsTopics();
132 
133        indexPage.addObject("topics",topics);
134        indexPage.addObject("topicsNum",topicsNum);
135        indexPage.addObject("usersNum",usersNum);
136        indexPage.addObject("tab",tab);
137        indexPage.addObject("user",user);
138        indexPage.addObject("hotestTopics",hotestTopics);
139        return  indexPage;
140    }
141 
142    /**
143     * 发表主题
144     * @param request
145     * @param session
146     * @return
147     */
148    @RequestMapping(value = "/topic/add", method = RequestMethod.POST)
149    public ModelAndView addTopic(HttpServletRequest request,HttpSession session){
150        ModelAndView indexPage;
151        //未登陆
152        if(session.getAttribute("userId")==null){
153            indexPage=new ModelAndView("redirect:/signin");
154            return  indexPage;
155        }
156        //处理参数
157        Integer userId=(Integer) session.getAttribute("userId");
158        String title=request.getParameter("title");
159        String content=request.getParameter("content");
160        Byte tabId=Byte.parseByte(request.getParameter("tab"));
161        //新建Topic
162        Topic topic=new Topic();
163        topic.setUserId(userId);
164        topic.setTitle(title);
165        topic.setContent(content);
166        topic.setTabId(tabId);
167        topic.setCreateTime(new Date());
168        topic.setUpdateTime(new Date());
169        //添加topic
170        boolean ifSucc=topicService.addTopic(topic);
171        boolean ifSuccAddCredit=userService.addCredit(1,userId);
172        if (ifSucc){
173            if (log.isInfoEnabled()){
174                log.info("添加主题成功!");
175            }
176        }
177        indexPage=new ModelAndView("redirect:/");
178 
179        return  indexPage;
180    }
181 
182}
最近下载更多
玖零定制问题修复  LV34 2024年3月3日
ewan007  LV30 2024年2月25日
2897744513  LV2 2023年12月31日
lilong007  LV23 2023年12月30日
wang123999  LV19 2023年12月5日
liuyuheng  LV17 2023年11月15日
全栈小白  LV35 2023年11月6日
Seaskye  LV14 2023年11月4日
Dramaaaa  LV3 2023年11月4日
zxc131313  LV12 2023年11月2日
最近浏览更多
月牙君  LV1 前天
PLVAE_514  LV2 3月7日
ChanLain  LV2 3月3日
andy_伟  LV6 2月25日
asdfg01234  LV10 1月10日
meng123wei 1月6日
暂无贡献等级
weishenme1993  LV9 1月1日
srmess  LV4 2024年12月30日
ninuxf  LV12 2024年12月28日
Junnnn 2024年12月28日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友