首页>代码>ssm+maven简单个人论坛系统>/Forum/src/main/java/com/itwyc/forum/controller/AllController.java
package com.itwyc.forum.controller;

import java.util.ArrayList;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.itwyc.forum.pojo.Announcement;
import com.itwyc.forum.pojo.Answer;
import com.itwyc.forum.pojo.Questions;
import com.itwyc.forum.pojo.Users;
import com.itwyc.forum.service.AdminService;
import com.itwyc.forum.service.AnnouncementService;
import com.itwyc.forum.service.AnswerService;
import com.itwyc.forum.service.QuestionsService;
import com.itwyc.forum.service.UsersService;

@RequestMapping(value = "/AllController")
@Controller
public class AllController {

	String name;
	ModelAndView mav = null;

	// 注入UsersService
	private UsersService usersService;

	@Autowired
	public void setUsersService(UsersService usersService) {
		this.usersService = usersService;
	}

	// 注入AdminService
	private AdminService adminService;

	@Autowired
	public void setAdminService(AdminService adminService) {
		this.adminService = adminService;
	}

	// 注入QuestionsService
	private QuestionsService questionsService;

	@Autowired
	public void setQuestionsService(QuestionsService questionsService) {
		this.questionsService = questionsService;
	}

	// 注入AnnuncementService
	private AnnouncementService announcementService;

	@Autowired
	public void setAnnouncementService(AnnouncementService announcementService) {
		this.announcementService = announcementService;
	}
	
	//注入AnswerService
	private AnswerService answerService;
	
	@Autowired
	public void setAnswerService(AnswerService answerService) {
		this.answerService = answerService;
	}
	
	

	//遍历公告
	public ModelAndView showAnn() {
		ArrayList<Announcement> announcement = announcementService.getAll();
		mav.addObject("announcement", announcement);
		return mav;
	}
	
	// 首页
	@RequestMapping("/index.html")
	public ModelAndView show() {
		mav = new ModelAndView("index");
		getAll();
		showAnn();
		return mav;
	}
	
	//登录后的首页
	@RequestMapping("/default")
	public ModelAndView backIndex() {
		mav = new ModelAndView("default");
		getAll();
		showAnn();
		return mav;
	}
	
	
	// 登录
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public ModelAndView login(HttpSession session, @RequestParam("username") String username,
			@RequestParam("password") String password) {
		if (usersService.login(username, password) != null) {
			mav = new ModelAndView("default");
			name = username;
			getAll();
			showAnn();
		} else {
			mav = new ModelAndView("error");
		}
		return mav;
	}

	// 注册
	@RequestMapping(value = "/register", method = RequestMethod.POST)
	public ModelAndView register(Users users, @RequestParam("username") String username, HttpSession session) {
		if (usersService.getOne(username) != null) { // 如果数据库中已经存在相同的用户名的话就报错
			mav = new ModelAndView("error");
		} else {
			usersService.insertUser(users);
			session.setAttribute("realname", username);
			mav = new ModelAndView("SUCCESS");
		}
		return mav;
	}

	// 管理员登录
	@RequestMapping(value = "/admin_login", method = RequestMethod.POST)
	public ModelAndView admin_login(@RequestParam("username") String username,
			@RequestParam("password") String password) {
		if (adminService.admin_login(username, password) != null) {
			mav = new ModelAndView("admin_index");
		} else {
			mav = new ModelAndView("error");
		}
		return mav;
	}

	// 遍历所有的问题
	public ModelAndView getAll() {
		ArrayList<Questions> questions = questionsService.getAll();
		mav.addObject("questions", questions);
		return mav;
	}

	// 个人中心
	@RequestMapping("/self")
	public ModelAndView self(HttpSession session) {
		ArrayList<Users> user = usersService.getUser(name);
		mav = new ModelAndView("self");
		mav.addObject("user", user);
		return mav;
	}

	// 更改资料
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String update(@RequestParam("newRealname") String newRealname, @RequestParam("newPhone") String newPhone) {
		if (newRealname != null && newPhone != null) {
			usersService.update(newRealname, newPhone, name);
		}
		return "update";
	}

	// 更改密码
	@RequestMapping("/updatePassword")
	public String updatePassword(@RequestParam("newPassword") String newPassword) {
		if (newPassword != null) {
			usersService.updatePassword(newPassword, name);
		}
		return "update";
	}

	//在后台提交公告
	@RequestMapping("announcement")
	public String insertAnnouncement(@RequestParam("title") String title,@RequestParam("content") String content) {
		announcementService.insertAnnouncement(title,content);
		return "rightframe";
	}
	
	//遍历所有的公告
	@RequestMapping("/showAnnouncement")
	public ModelAndView showAnnouncement() {
		ArrayList<Announcement> announcement = announcementService.getAll();
		mav = new ModelAndView("delAnnouncement");
		mav.addObject("announcement", announcement);
		return mav;
	}
	
	//后台删除公告
	@RequestMapping("/delAnnouncement")
	public String delAnnouncement(@RequestParam("id") int id) {
		announcementService.delAnnouncement(id);
		return "rightframe";
	}
	
	//公告详情
	@RequestMapping("/announcement_detail")
	public ModelAndView getOne(@RequestParam("id") int id) {
		ArrayList<Announcement> ann = announcementService.getOne(id);
		mav = new ModelAndView("announcement_detail");
		mav.addObject("ann", ann);
		return mav;
	}
	
	//提出问题
	@RequestMapping("questions")
	public ModelAndView insertQuestion(@RequestParam("topic") String topic) {
		questionsService.insertQuestions(topic);
		mav = new ModelAndView("result");
		return mav;
	}
	
	//遍历所有的问题呈现再页面
	@RequestMapping("question_all")
	public ModelAndView questionAll() {
		mav = new ModelAndView("question_all");
		getAll();
		return mav;
	}
	
	//将答案插入数据库
	@RequestMapping("/answer")
	public ModelAndView insertAnswer(@RequestParam("id") int id,@RequestParam("answer") String answer) {
		Questions question = questionsService.getOne(id);
		answerService.insertAnswer(question.getTopic(), answer);
		questionAll();
		return mav;
	}
	
	//查看所有的问题答案
	@RequestMapping("/showAnswer")
	public ModelAndView showAnswer(HttpSession session,@RequestParam("topic") String topic) {
		ArrayList<Answer> answer = answerService.showAnswer(topic);
		mav = new ModelAndView("answers");
		session.setAttribute("topic", topic);
		mav.addObject("answer", answer);
		return mav;
		
	}
	
	//遍历所有的用户
	@RequestMapping("/member")
	public ModelAndView showMember() {
		ArrayList<Users> member = usersService.users();
		mav = new ModelAndView("member");
		mav.addObject("member", member);
		return mav;
	}
	
	//删除会员
	@RequestMapping("/delMember")
	public ModelAndView delMember(@RequestParam("id") int id) {
		mav = new ModelAndView("rightframe");
		usersService.delMember(id);
		return mav;
	}
	
	//后台问题答案的所有遍历
	@RequestMapping("/answers")
	public ModelAndView answers() {
		mav = new ModelAndView("question_answer");
		ArrayList<Answer> answers = answerService.getAllAnswer();
		mav.addObject("answers", answers);
		return mav;
	}
	
	//删除问题和答案
	@RequestMapping("/delQuestionAndAnswer")
	public ModelAndView delQuestionAndAnswer(@RequestParam("id") int id) {
		mav = new ModelAndView("rightframe");
		answerService.delQuestionAndAnswer(id);
		return mav;
	}
	
	
	
}
最近下载更多
864216741  LV2 8月25日
的还是看哈  LV3 2023年11月6日
molu123456 2023年10月16日
暂无贡献等级
tatakai  LV2 2023年3月22日
ProsperKing  LV4 2023年2月20日
hqy966  LV5 2022年7月5日
wanglinddad  LV55 2022年4月3日
a318888331  LV13 2022年3月24日
379131419  LV11 2021年12月14日
18219194576  LV7 2021年11月24日
最近浏览更多
864216741  LV2 8月25日
TY0165  LV20 6月24日
chenyang YI 6月6日
暂无贡献等级
zuorishu 5月16日
暂无贡献等级
2017143155  LV12 5月1日
tianran9 4月27日
暂无贡献等级
xiaopengzep  LV1 3月24日
XiaoSong888  LV3 1月10日
chfismine 1月8日
暂无贡献等级
潘潘123456  LV2 2023年12月24日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友