首页>代码>基于ssm的图书馆预约占座管理系统>/LibrarySeats/src/dingzhen/controller/BbsController.java
package dingzhen.controller;
/*
 * 交流控制器
 */
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import dingzhen.entity.Bbs;
import dingzhen.entity.Topic;
import dingzhen.entity.User;
import dingzhen.service.BbsService;
import dingzhen.service.TopicService;
import dingzhen.util.WriterUtil;

@Controller
@RequestMapping("bbs")
public class BbsController {

	private int page;
	private int rows;
	@Autowired
	private BbsService<Bbs> bbsService;
	private Bbs bbs;
	@Autowired
	private TopicService<Topic> topicService;
	private Topic topic;
	
	@RequestMapping("bbsIndex")
	public String index(HttpServletRequest request) {
		User currentUser = (User)request.getSession().getAttribute("currentUser");
		if(currentUser.getRoleId()==1){
			return "bbs/bbsIndexForAdmin";
		} else {
			return "bbs/bbsIndex";
		}
	}
	
	
	@RequestMapping("bbsList")
	public void bbsList(HttpServletRequest request,HttpServletResponse response){
		try {
			page = Integer.parseInt(request.getParameter("page"));
			rows = Integer.parseInt(request.getParameter("rows"));
			bbs = new Bbs();
			bbs.setPage((page-1)*rows);
			bbs.setRows(rows);
			bbs.setAuthor(request.getParameter("author"));
			bbs.setTitle(request.getParameter("title"));
			List<Bbs> list = bbsService.findBbs(bbs);
			int total = bbsService.countBbs(bbs);
			JSONObject jsonObj = new JSONObject();//new一个JSON
			jsonObj.put("total",total );//total代表一共有多少数据
			jsonObj.put("rows", list);//row是代表显示的页的数据
	        WriterUtil.write(response,jsonObj.toString()); //将上述Json输出,前台ajax接收
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	// 发表新帖
	@RequestMapping("newBbs")
	public void newTopic(HttpServletRequest request,HttpServletResponse response,Bbs bbs){
		JSONObject result = new JSONObject();
		String content = bbs.getContent();
		User currentUser = (User)request.getSession().getAttribute("currentUser");
		String author = currentUser.getUserName();
		String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
		try {
			bbs.setAuthor(author);
			bbs.setTime(time);
			bbsService.addBbs(bbs);
			
			int bbsid = bbs.getId();
			topic = new Topic();
			topic.setAuthor(author);
			topic.setBbsid(bbsid);
			topic.setContent(content);
			topic.setTime(time);
			topicService.addTopic(topic);
			
			result.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			result.put("errorMsg", "对不起!发表新帖失败");
		}
		WriterUtil.write(response, result.toString());
	}
	
	
	// 修改标题
	@RequestMapping("updateBbstitle")
	public void updateBbstitle(HttpServletResponse response,HttpServletRequest request,Bbs bbs){
		JSONObject result = new JSONObject();
		String id = request.getParameter("id");
		try {
			bbs.setId(Integer.parseInt(id));
			bbsService.updateBbstitle(bbs);
			result.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			result.put("errorMsg", "对不起!修改标题失败");
		}
		WriterUtil.write(response, result.toString());
	}
	
	
	

	@RequestMapping("deleteBbs")
	public void deleteBbs(HttpServletRequest request,HttpServletResponse response){
		JSONObject result=new JSONObject();
		try {
			String[] ids=request.getParameter("ids").split(",");
			for (String id : ids) {
				bbsService.deleteBbs(Integer.parseInt(id));
			}
			result.put("success", true);
			result.put("delNums", ids.length);
		} catch (Exception e) {
			e.printStackTrace();
			result.put("errorMsg", "对不起,删除失败");
		}
		WriterUtil.write(response, result.toString());
	}
	
	
	
	
	
	
}
最近下载更多
柳咪华沙  LV7 2024年6月28日
heweimin  LV13 2024年4月15日
自由吃柚子  LV2 2024年3月26日
江江江2326  LV1 2024年1月2日
hehanhan  LV1 2023年11月8日
lilong007  LV23 2022年10月21日
171337601  LV9 2022年4月18日
123qwer123  LV1 2022年4月2日
18784679855  LV8 2022年3月5日
ypf12345  LV2 2022年3月2日
最近浏览更多
PLVAE_514  LV2 3月10日
987654321666 3月7日
暂无贡献等级
柳咪华沙  LV7 2024年6月28日
TY0165  LV20 2024年6月22日
bingo111 2024年5月25日
暂无贡献等级
微信网友_7000012089643008  LV4 2024年5月19日
yangyuer  LV1 2024年4月28日
heweimin  LV13 2024年4月15日
Charismatic 2024年4月14日
暂无贡献等级
WBelong  LV8 2024年3月29日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友