基于javaweb+mysql的ssm投票管理系统(java+jsp+ssm+javabean+mysql+tomcat)
运行环境
Java≥8、MySQL≥5.7、Tomcat≥8
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
投票管理系统,分为前端和后台管理模块
前端用户可以登录注册、查看投票信息,登录后可以进行投票,也可以查看自己的历史投票记录
后台管理模块管理员登录后可以管理用户信息、管理投票主题和子项、查看投票详情、查看投票图表统计信息等
目前投票方式支持单选只能投一次、单选一天只能投一次、多选只能投一次、多选一天只能投一次等四种方式,投票主题可以设置开始和结束时间,结束后不能再进行投票
前台
后台
技术框架
JavaBean MVC JSP SSM(Spring SpringMVC MyBatis) MySQL EasyUI jQuery Ajax
基于javaweb+mysql的SSM投票管理系统(java+jsp+ssm+javabean+mysql+tomcat)
@ResponseBody
public String validateRegisteUser(String name){
User user = userService.findUserByName(name);
if(user == null)return "n";
return "f";
}
@RequestMapping(value="/user_reg",method=RequestMethod.POST)
@ResponseBody
public String userRegiste(User user){
user.setCreateTime(new Date(System.currentTimeMillis()));
if(userService.add(user) > 0){
return "t";
}
return "f";
}
@RequestMapping(value="/get_cpacha",method=RequestMethod.GET)
public void getCpacha(@RequestParam(value="type", required=false,defaultValue="adminLogin") String type,
@RequestParam(value="w", required=false,defaultValue="98") int width,
@RequestParam(value="h", required=false,defaultValue="33") int height,
@RequestParam(value="cl", required=false,defaultValue="4") int codeLength,
HttpServletRequest request,
HttpServletResponse reponse){
CpachaUtil cpachaUtil = new CpachaUtil(codeLength,width,height);
String generatorVCode = cpachaUtil.generatorVCode();
request.getSession().setAttribute("loginCapcha", generatorVCode);
BufferedImage generatorRotateVCodeImage = cpachaUtil.generatorRotateVCodeImage(generatorVCode, true);
try {
ImageIO.write(generatorRotateVCodeImage, "gif", reponse.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
request.getSession().setAttribute("loginCapcha", generatorVCode);
BufferedImage generatorRotateVCodeImage = cpachaUtil.generatorRotateVCodeImage(generatorVCode, true);
try {
ImageIO.write(generatorRotateVCodeImage, "gif", reponse.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Controller
@RequestMapping("/admin/subject_item")
public class SubjectItemController {
@Autowired
private SubjectItemService subjectItemService;
@Autowired
private SubjectService subjectService;
}
}
@Controller
@RequestMapping("/admin/vote")
public class AdminVoteController {
@Autowired
private SubjectService subjectService;
@Autowired
private SubjectItemService subjectItemService;
@Autowired
private VoteService voteService;
@RequestMapping(value="/list",method=RequestMethod.GET)
public ModelAndView index(ModelAndView model){
model.setViewName("admin/vote/list");
return model;
}
@RequestMapping(value="/get_list",method=RequestMethod.GET)
@ResponseBody
public Map<String,Object> list(@RequestParam(value="username", required=false,defaultValue="") String username,
@RequestParam(value="subjectName", required=false,defaultValue="") String subjectName,
@RequestParam(value="subjectItemTitle", required=false,defaultValue="") String subjectItemTitle,
public class SystemController {
@Autowired
private ManagerService managerService;
@RequestMapping(value="/index",method=RequestMethod.GET)
public ModelAndView index(ModelAndView model){
model.setViewName("system/index");
return model;
}
@RequestMapping(value="/login",method=RequestMethod.GET)
public ModelAndView login(ModelAndView model){
model.setViewName("system/login");
return model;
}
@RequestMapping(value="/login_out",method=RequestMethod.GET)
public String loginOut(HttpServletRequest request){
request.getSession().setAttribute("manager", null);
return "redirect:login";
}
@RequestMapping(value="/login",method=RequestMethod.POST)
@ResponseBody
public Map<String,String> loginAct(@RequestParam(value="username", required=true) String username,
@RequestParam(value="password", required=true) String password,
@RequestParam(value="vcode", required=true) String vcode,
HttpServletRequest request){
Map<String,String> ret = new HashMap<String,String>();
if(StringUtils.isEmpty(username)){
ret.put("type", "error");
ret.put("msg", "用户名不能为空!");
return ret;
}
if(StringUtils.isEmpty(password)){
ret.put("type", "error");
ret.put("msg", "密码不能为空!");
return ret;
}
if(StringUtils.isEmpty(vcode)){
@Autowired
private SubjectItemService subjectItemService;
@Autowired
private VoteService voteService;
@RequestMapping(value="/vote_list",method=RequestMethod.GET)
public ModelAndView index(ModelAndView model){
Map<String,Object> queryMap = new HashMap<String, Object>();
queryMap.put("name", "%%");
queryMap.put("startIndex", 0);
queryMap.put("pageSize", 9999);
List<Subject> findByName = subjectService.findByRelation(queryMap);
model.addObject("voteList", findByName);
model.setViewName("home/vote_list");
model.addObject("singleType", "SINGLE");
model.addObject("muilteType", "MUILTE");
model.addObject("nowTime", System.currentTimeMillis());
return model;
}
@RequestMapping(value="/vote_history_list",method=RequestMethod.GET)
public ModelAndView historyList(ModelAndView model,HttpServletRequest request){
Map<String,Object> queryMap = new HashMap<String, Object>();
User user = (User)request.getSession().getAttribute("user");
if(user == null){
model.setViewName("home/login");
return model;
}
queryMap.put("name", "%%");
queryMap.put("startIndex", 0);
queryMap.put("pageSize", 9999);
List<Subject> subjectList = subjectService.findByRelation(queryMap);
queryMap.put("userId", user.getId());
List<Vote> voteList = voteService.findList(queryMap);
List<Subject> ret = new ArrayList<Subject>();
DecimalFormat df = new DecimalFormat("0.00");
for(Subject s:subjectList){
for(Vote v:voteList){
if(s.getId() == v.getSubjectId()){
s.getVotedList().add(v);
if(!ret.contains(s))
return ret;
}
User user = userService.findUserByName(username);
if(user == null){
ret.put("type", "error");
ret.put("msg", "不存在该用户!");
return ret;
}
if(!password.equals(user.getPassword())){
ret.put("type", "error");
ret.put("msg", "密码错误!");
return ret;
}
if(user.getStatus() != 1){
ret.put("type", "error");
ret.put("msg", "账户被禁用,请联系管理员!");
return ret;
}
ret.put("type", "success");
ret.put("msg", "登录成功");
//将登录用户放入session
request.getSession().setAttribute("user",user);
return ret;
}
@RequestMapping(value="/reset_pwd",method=RequestMethod.POST)
@ResponseBody
public String resetPwd(String newPwd,Long id){
User user = userService.findUserById(id);
if(user == null)return "no";
if(StringUtils.isEmpty(newPwd))return "no";
user.setPassword(newPwd);
if(userService.changePwd(user) <= 0)return "no";
return "t";
}
@RequestMapping(value="/validateLoginUser",method=RequestMethod.POST)
@ResponseBody
public String validateLoginUser(String name){
User user = userService.findUserByName(name);
if(user == null)return "n";
return "yes";
}
@RequestMapping(value="/validateRegisteUser",method=RequestMethod.POST)
@ResponseBody
public String validateRegisteUser(String name){
User user = userService.findUserByName(name);
@Controller
@RequestMapping("/admin/subject_item")
public class SubjectItemController {
@Autowired
private SubjectItemService subjectItemService;
@Autowired
private SubjectService subjectService;
@RequestMapping(value="/list",method=RequestMethod.GET)
public ModelAndView index(ModelAndView model){
Map<String,Object> queryMap = new HashMap<String, Object>();
queryMap.put("name", "%%");
queryMap.put("startIndex", 0);
queryMap.put("pageSize", 9999);
List<Subject> subjects = subjectService.findByName(queryMap);
model.addObject("subjectsJson",JSONArray.fromObject(subjects));
model.addObject("subjects",subjects);
model.setViewName("admin/subject_item/list");
return model;
}
@RequestMapping(value="/add",method=RequestMethod.POST)
@ResponseBody
public Map<String,Object> add(SubjectItem subjectItem){
Map<String,Object> ret = new HashMap<String,Object>();
subjectItem.setCreateTime(new Date(System.currentTimeMillis()));
if(subjectItemService.add(subjectItem) <= 0){
ret.put("type", "error");
@Controller
@RequestMapping("/admin/vote")
public class AdminVoteController {
@Autowired
private SubjectService subjectService;
@Autowired
private SubjectItemService subjectItemService;
@Autowired
private VoteService voteService;
@RequestMapping(value="/list",method=RequestMethod.GET)
public ModelAndView index(ModelAndView model){
model.setViewName("admin/vote/list");
return model;
}
@RequestMapping(value="/get_list",method=RequestMethod.GET)
@ResponseBody
public Map<String,Object> list(@RequestParam(value="username", required=false,defaultValue="") String username,
@RequestParam(value="subjectName", required=false,defaultValue="") String subjectName,
@RequestParam(value="subjectItemTitle", required=false,defaultValue="") String subjectItemTitle,
Page page
){
Map<String,Object> queryMap = new HashMap<String,Object>();
queryMap.put("username", "%" + username + "%");
queryMap.put("subjectName", "%" + subjectName + "%");
queryMap.put("subjectItemTitle", "%" + subjectItemTitle + "%");
queryMap.put("startIndex", page.getOffset());
){
Map<String,Object> queryMap = new HashMap<String,Object>();
queryMap.put("title", "%" + name + "%");
queryMap.put("startIndex", page.getOffset());
queryMap.put("pageSize", page.getRows());
List<SubjectItem> findByName = subjectItemService.findByName(queryMap);
Map<String,Object> ret = new HashMap<String,Object>();
ret.put("total", subjectItemService.getTotalByName(queryMap));
ret.put("rows", findByName);
return ret;
}
}
@Controller
@RequestMapping("/admin/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value="/list",method=RequestMethod.GET)
public ModelAndView index(ModelAndView model){
model.setViewName("admin/user/list");
return model;
}
@RequestMapping(value="/add",method=RequestMethod.POST)
@ResponseBody
public Map<String,Object> add(User user){
Map<String,Object> ret = new HashMap<String,Object>();
if(isExist(user.getName(),null)){
ret.put("type", "error");
ret.put("msg", "用户名已经存在!");
return ret;
}
@Controller
@RequestMapping("/admin/vote_stats")
public class VoteStatsController {
@Autowired
private SubjectService subjectService;
@Autowired
private SubjectItemService subjectItemService;
@Autowired
private VoteService voteService;
@RequestMapping(value="/list",method=RequestMethod.GET)
public ModelAndView index(ModelAndView model){
Map<String, Object> queryMap = new HashMap<String, Object>();
queryMap.put("name", "%%");
queryMap.put("startIndex", 0);
queryMap.put("pageSize", 30);
List<Subject> subjectList = subjectService.findByName(queryMap);
List<String> subjectNameList = new ArrayList<String>();
List<Integer> voteNumberList = new ArrayList<Integer>();
for(Subject subject:subjectList){
subjectNameList.add(subject.getName());
voteNumberList.add(subject.getVoteNumber());
}
model.addObject("subjectNameList", JSONArray.fromObject(subjectNameList));
model.addObject("voteNumberList", JSONArray.fromObject(voteNumberList));
model.setViewName("admin/vote_stats/list");
return model;
}
@RequestMapping(value="/get_subject_stats",method=RequestMethod.POST)
@ResponseBody
public Map<String,Object> list(
@RequestParam(value="subjectName", required=false,defaultValue="") String subjectName
){
Map<String,Object> queryMap = new HashMap<String,Object>();
Map<String,Object> ret = new HashMap<String,Object>();
queryMap.put("name", "%" + name + "%");
queryMap.put("startIndex", page.getOffset());
queryMap.put("pageSize", page.getRows());
List<Subject> findByName = subjectService.findByName(queryMap);
Map<String,Object> ret = new HashMap<String,Object>();
ret.put("total", subjectService.getTotalByName(queryMap));
ret.put("rows", findByName);
return ret;
}
}
@Controller
@RequestMapping("/admin/vote_stats")
public class VoteStatsController {
@Autowired
private SubjectService subjectService;
@Autowired
private SubjectItemService subjectItemService;
@Autowired
private VoteService voteService;
@RequestMapping(value="/list",method=RequestMethod.GET)
public ModelAndView index(ModelAndView model){
Map<String, Object> queryMap = new HashMap<String, Object>();
queryMap.put("name", "%%");
queryMap.put("startIndex", 0);
queryMap.put("pageSize", 30);
List<Subject> subjectList = subjectService.findByName(queryMap);
List<String> subjectNameList = new ArrayList<String>();
List<Integer> voteNumberList = new ArrayList<Integer>();
@Controller
@RequestMapping("/admin/subject_item")
public class SubjectItemController {
@Autowired
private SubjectItemService subjectItemService;
@Autowired
private SubjectService subjectService;
@RequestMapping(value="/list",method=RequestMethod.GET)
public ModelAndView index(ModelAndView model){
Map<String,Object> queryMap = new HashMap<String, Object>();
queryMap.put("name", "%%");
queryMap.put("startIndex", 0);
queryMap.put("pageSize", 9999);
List<Subject> subjects = subjectService.findByName(queryMap);
model.addObject("subjectsJson",JSONArray.fromObject(subjects));
model.addObject("subjects",subjects);
model.setViewName("admin/subject_item/list");
return model;
}
@RequestMapping(value="/add",method=RequestMethod.POST)
@ResponseBody
public Map<String,Object> add(SubjectItem subjectItem){
Map<String,Object> ret = new HashMap<String,Object>();
subjectItem.setCreateTime(new Date(System.currentTimeMillis()));
if(subjectItemService.add(subjectItem) <= 0){
ret.put("type", "error");
ret.put("msg", "添加失败!");
model.addObject("user", userService.findUserById(id));
model.setViewName("home/change_pwd");
return model;
}
@RequestMapping(value="/login_out",method=RequestMethod.GET)
public String loginOut(HttpServletRequest request){
request.getSession().setAttribute("user", null);
return "redirect:/";
}
@RequestMapping(value="/login",method=RequestMethod.POST)
@ResponseBody
public Map<String,String> loginAct(@RequestParam(value="username", required=true) String username,
@RequestParam(value="password", required=true) String password,
HttpServletRequest request){
Map<String,String> ret = new HashMap<String,String>();
if(StringUtils.isEmpty(username)){
ret.put("type", "error");
ret.put("msg", "用户名不能为空!");
return ret;
}
if(StringUtils.isEmpty(password)){
ret.put("type", "error");
ret.put("msg", "密码不能为空!");
return ret;
}
User user = userService.findUserByName(username);
if(user == null){
ret.put("type", "error");
ret.put("msg", "不存在该用户!");
return ret;
}
if(!password.equals(user.getPassword())){
ret.put("type", "error");
ret.put("msg", "密码错误!");
return ret;
}
if(user.getStatus() != 1){
ret.put("type", "error");
ret.put("msg", "账户被禁用,请联系管理员!");
return ret;
}
ret.put("type", "success");
ret.put("msg", "登录成功");
//将登录用户放入session
request.getSession().setAttribute("user",user);
return ret;
}
@RequestMapping(value="/reset_pwd",method=RequestMethod.POST)
ret.put("type", "voted");
ret.put("msg", "今日您已经投过票了,明天再来!");
return ret;
}
}
Vote vote = new Vote();
vote.setSubjectId(subjectId);
vote.setUserId(user.getId());
String[] split = subjectItemIds.split(",");
for(String s:split){
Long subjectItemId = Long.valueOf(s);
vote.setSubjectItemId(subjectItemId);
vote.setCreateTime(new Date(System.currentTimeMillis()));
if(voteService.add(vote) <= 0){
ret.put("type", "error");
ret.put("msg", "投票失败!");
return ret;
}
if(subjectItemService.addOneVote(subjectItemId) <= 0){
ret.put("type", "error");
ret.put("msg", "投票子项修改失败!");
return ret;
}
if(subjectService.addOneVote(subjectId) <= 0){
ret.put("type", "error");
ret.put("msg", "投票主题修改失败!");
return ret;
}
}
ret.put("type", "success");
ret.put("msg", "投票成功!");
return ret;
}
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
ret.put("type", "error");
ret.put("msg", "改主题下存在投票信息,无法删除,请先去删除投票选项!");
return ret;
}
ret.put("type", "success");
return ret;
}
@RequestMapping(value="/get_list",method=RequestMethod.GET)
@ResponseBody
public Map<String,Object> list(@RequestParam(value="name", required=false,defaultValue="") String name,
Page page
){
Map<String,Object> queryMap = new HashMap<String,Object>();
queryMap.put("name", "%" + name + "%");
queryMap.put("startIndex", page.getOffset());
queryMap.put("pageSize", page.getRows());
List<User> findByName = userService.findByName(queryMap);
Map<String,Object> ret = new HashMap<String,Object>();
ret.put("total", userService.getTotalByName(queryMap));
ret.put("rows", findByName);
return ret;
}
private boolean isExist(String name,Long id){
User user = userService.findUserByName(name);
if(user == null){
return false;
}
if(id != null && user.getId() == id.longValue())return false;
return true;
}
}
model.setViewName("home/login");
return model;
}
queryMap.put("name", "%%");
queryMap.put("startIndex", 0);
queryMap.put("pageSize", 9999);
List<Subject> subjectList = subjectService.findByRelation(queryMap);
queryMap.put("userId", user.getId());
List<Vote> voteList = voteService.findList(queryMap);
List<Subject> ret = new ArrayList<Subject>();
DecimalFormat df = new DecimalFormat("0.00");
for(Subject s:subjectList){
for(Vote v:voteList){
if(s.getId() == v.getSubjectId()){
s.getVotedList().add(v);
if(!ret.contains(s))
ret.add(s);
for(SubjectItem si:s.getSubjectItems()){
String format = df.format((float)si.getVoteNumber()/s.getVoteNumber()*100);
si.setPer(Float.valueOf(format));
if(v.getSubjectItemId() == si.getId()){
si.setFlag("true");
}
}
}
}
}
model.addObject("voteList", ret);
model.setViewName("home/vote_history_list");
model.addObject("singleType", "SINGLE");
model.addObject("muilteType", "MUILTE");
model.addObject("nowTime", System.currentTimeMillis());
return model;
}
@RequestMapping(value="/add_vote",method=RequestMethod.POST)
@ResponseBody
public Map<String, String> addVote(
@RequestParam(name="subjectId",required=true) Long subjectId,
@RequestParam(name="subjectItemIds",required=true) String subjectItemIds,
HttpServletRequest request
){
Map<String, String> ret = new HashMap<String, String>();
User user = (User)request.getSession().getAttribute("user");
if(user == null){
ret.put("type", "error");
ret.put("msg", "请先登录再投票!");
@RequestMapping(value="/change_pwd",method=RequestMethod.GET)
public ModelAndView changePassword(Long id,ModelAndView model){
model.addObject("user", userService.findUserById(id));
model.setViewName("home/change_pwd");
return model;
}
@RequestMapping(value="/login_out",method=RequestMethod.GET)
public String loginOut(HttpServletRequest request){
request.getSession().setAttribute("user", null);
return "redirect:/";
}
@RequestMapping(value="/login",method=RequestMethod.POST)
@ResponseBody
public Map<String,String> loginAct(@RequestParam(value="username", required=true) String username,
@RequestParam(value="password", required=true) String password,
HttpServletRequest request){
Map<String,String> ret = new HashMap<String,String>();
if(StringUtils.isEmpty(username)){
ret.put("type", "error");
ret.put("msg", "用户名不能为空!");
return ret;
}
if(StringUtils.isEmpty(password)){
ret.put("type", "error");
ret.put("msg", "密码不能为空!");
return ret;
}
User user = userService.findUserByName(username);
if(user == null){
ret.put("type", "error");
ret.put("msg", "不存在该用户!");
return ret;
}
if(!password.equals(user.getPassword())){
ret.put("type", "error");
ret.put("msg", "密码错误!");
return ret;
}
if(user.getStatus() != 1){
ret.put("type", "error");
ret.put("msg", "账户被禁用,请联系管理员!");
return ret;
}
ret.put("type", "success");
ret.put("msg", "登录成功");
//将登录用户放入session
request.getSession().setAttribute("user",user);
return ret;
}
@RequestMapping(value="/reset_pwd",method=RequestMethod.POST)
@Autowired
private SubjectService subjectService;
@Autowired
private SubjectItemService subjectItemService;
@Autowired
private VoteService voteService;
@RequestMapping(value="/list",method=RequestMethod.GET)
public ModelAndView index(ModelAndView model){
model.setViewName("admin/vote/list");
return model;
}
@RequestMapping(value="/get_list",method=RequestMethod.GET)
@ResponseBody
public Map<String,Object> list(@RequestParam(value="username", required=false,defaultValue="") String username,
@RequestParam(value="subjectName", required=false,defaultValue="") String subjectName,
@RequestParam(value="subjectItemTitle", required=false,defaultValue="") String subjectItemTitle,
Page page
){
Map<String,Object> queryMap = new HashMap<String,Object>();
queryMap.put("username", "%" + username + "%");
queryMap.put("subjectName", "%" + subjectName + "%");
queryMap.put("subjectItemTitle", "%" + subjectItemTitle + "%");
queryMap.put("startIndex", page.getOffset());
queryMap.put("pageSize", page.getRows());
List<Vote> findByName = voteService.findList(queryMap);
Map<String,Object> ret = new HashMap<String,Object>();
ret.put("total", voteService.getTotal(queryMap));
ret.put("rows", findByName);
return ret;
}
}