项目介绍
这是一个基于SpringMVC+Spring+MyBatis的SSM问卷调查系统。
管理端权限
管理员信息CURD
制作调查问卷
发布调查问卷
统计调查结果
统计报表展示
用户端权限
填写问卷
环境需要
1.运行环境:最好是java jdk 1.8,我们在这个平台上运行的。其他版本理论上也可以。
2.IDE环境:IDEA,Eclipse,Myeclipse都可以。推荐IDEA;
3.tomcat环境:Tomcat 7.x,8.x,9.x版本均可
4.硬件环境:windows 7/8/10 1G内存以上;或者 Mac OS;
5.是否Maven项目: 是;查看源码目录中是否包含pom.xml;若包含,则为maven项目,否则为非maven项目
6.数据库:MySql 5.7等版本均可;
技术栈
1. 后端:Spring Springmvc Mybatis
2. 前端:JSP+css+javascript+jQuery+layui
使用说明
1. 使用Navicat或者其它工具,在mysql中创建对应名称的数据库,并导入项目的sql文件;
2.使用IDEA/Eclipse/MyEclipse导入项目,Eclipse/MyEclipse导入时,若为maven项目请选择maven;
若为maven项目,导入成功后请执行maven clean;maven install命令,配置tomcat,
3. 将项目中db.properties配置文件中的数据库配置改为自己的配置,然后运行;
4.运行项目,在浏览器中输入http://localhost:8080/survey
管理员用户名密码:admin/123456
登录控制层:
@Controller
public class LoginController {
@Autowired
private AdminService adminService;
@GetMapping("/login")
public String v_login(){
return "login";
}
@PostMapping("/login")
@ResponseBody
public Map<String, Object> login(@RequestBody Map<String,Object> map, HttpServletRequest request){
String account = map.get("account")+"";
String password = map.get("password")+"";
if(Strings.isNullOrEmpty(account) || Strings.isNullOrEmpty(password)){
return MapControl.getInstance().error("用户名或密码不能为空").getMap();
}
Admin admin = adminService.login(account, MD5Utils.getMD5(password));
if(admin != null){
SessionUtils.setAdmin(request,admin);
return MapControl.getInstance().success().getMap();
}else{
return MapControl.getInstance().error("用户名或密码错误").getMap();
}
}
@GetMapping("/pwd")
public String pwd(){
return "pwd";
}
@PostMapping("/pwd")
@ResponseBody
public Map<String,Object> pwd(Integer id,String type,String sourcePwd,String newPwd){
Admin user = adminService.detail(id);
if(user.getPassword().equals(MD5Utils.getMD5(sourcePwd))){
Admin entity = new Admin();
entity.setId(id);
entity.setPassword(MD5Utils.getMD5(newPwd));
int update = adminService.update(entity);
if(update>0){
return MapControl.getInstance().success().getMap();
}else{
return MapControl.getInstance().error().getMap();
}
}else{
return MapControl.getInstance().error("原密码错误").getMap();
}
}
}
问卷管理控制层:
@Controller
@RequestMapping("/survey")
public class SurveyController {
@Autowired
private SurveyService surveyService;
@Autowired
private QuestionService questionService;
@GetMapping("/create")
public String v_create(){
return "survey/add";
}
@PostMapping("/create")
@ResponseBody
public Map<String,Object> create(@RequestBody Survey survey, HttpServletRequest request){
Admin currAdmin = SessionUtils.getAdmin(request);
survey.setCreator(currAdmin.getId());
survey.setState(Survey.state_create);
survey.setAnon(survey.getAnon()!=null?0:1);
int result = surveyService.create(survey);
if(result<=0){
//失败的情况下
return MapControl.getInstance().error().getMap();
}
return MapControl.getInstance().success().getMap();
}
@PostMapping("/delete")
@ResponseBody
public Map<String,Object> delete(String ids){
int result = surveyService.deleteBatch(ids);
if(result<=0){
//失败的情况下
return MapControl.getInstance().error().getMap();
}
return MapControl.getInstance().success().getMap();
}
@PostMapping("/update")
@ResponseBody
public Map<String, Object> update(@RequestBody Survey survey){
survey.setAnon(survey.getAnon()!=null?0:1);
int result = surveyService.update(survey);
if(result<=0){
//失败的情况下
return MapControl.getInstance().error().getMap();
}
return MapControl.getInstance().success().getMap();
}
@GetMapping("/list")
public String list(){
return "survey/list";
}
@PostMapping("/query")
@ResponseBody
public Map<String,Object> query(@RequestBody Survey survey, ModelMap modelMap){
List<Survey> list = surveyService.query(survey);
//创建者信息写入survey对象
for (Survey entity : list) {
entity.setAdmin(SystemInit.adminMap.get(entity.getCreator()));
}
Integer count = surveyService.count(survey);
return MapControl.getInstance().page(list,count).getMap();
}
@GetMapping("/detail")
public String detail(Integer id,ModelMap modelMap){
Survey survey = surveyService.detail(id);
modelMap.addAttribute("survey",survey);
return "survey/update";
}
@GetMapping("/question")
public String question(Integer id,ModelMap modelMap){
Survey survey = surveyService.detail(id);
modelMap.addAttribute("survey",survey);
return "survey/question";
}
@GetMapping("/preview/{id}")
public String preview(@PathVariable("id") Integer id,ModelMap modelMap){
Survey survey = surveyService.detail(id);
Question question = new Question();
question.setSurveyId(survey.getId());
//查询一个问卷中的所有问题及选项
List<Question> questions = questionService.query(question);
//将问题设置为survey的属性
survey.setQuestions(questions);
modelMap.addAttribute("survey",survey);
return "survey/preview";
}
@RequestMapping(value = "/upload",method = RequestMethod.POST)
public String upload(Integer id, @RequestParam("file") MultipartFile multipartFile, HttpServletRequest request){
//上传的位置
String path = request.getSession().getServletContext().getRealPath("/upload/");
//判断该路径是否存在
File file = new File(path);
if (!file.exists()) {
file.mkdirs();
}
//上传文件项
String filename = multipartFile.getOriginalFilename();
String uuid = UUID.randomUUID().toString().replace("-", "");
String saveName = uuid + "_" + filename.substring(filename.lastIndexOf(File.separator) + 1);
try {
multipartFile.transferTo(new File(path, saveName));
Survey survey = new Survey();
survey.setId(id);
survey.setBgimg(saveName);
surveyService.update(survey);
} catch (IOException e) {
e.printStackTrace();
}
return "redirect:preview/"+id;
}
@PostMapping("/publish")
@ResponseBody
public Map<String,Object> publish(Integer id,HttpServletRequest request){
Survey param = surveyService.detail(id);
if(!Survey.state_exec.equals(param.getState())){
return MapControl.getInstance().error("操作失败,当前问卷未在执行中!").getMap();
}
String uuid = "/dy/"+UUID.randomUUID().toString();
Survey survey = new Survey();
survey.setId(id);
//http://localhost:8080/survey/ieieas-asdf-asdf-3-asd-f-asdf
String url = "http://"+request.getServerName()+ ":" + request.getServerPort() + request.getContextPath()+uuid;
survey.setUrl(url);
surveyService.update(survey);
return MapControl.getInstance().success().getMap();
}
@PostMapping("/submit")
@ResponseBody
public Map<String,Object> submit(@RequestBody List<Map<String,Object>> list){
List<AnswerOpt> optList = new ArrayList();
List<AnswerTxt> txtList = new ArrayList();
String uuid = UUID.randomUUID().toString();
for (Map<String, Object> stringObjectMap : list) {
if(object2String(stringObjectMap.get("type")).equals("1") || object2String(stringObjectMap.get("type")).equals("2")){
List<Object> opts = (List<Object>) stringObjectMap.get("opts");
for (Object opt : opts) {
AnswerOpt answerOpt = new AnswerOpt();
answerOpt.setQuestionId(object2Integer(stringObjectMap.get("questionId")));
answerOpt.setSurveyId(object2Integer(stringObjectMap.get("surveyId")));
answerOpt.setType(object2String(stringObjectMap.get("type")));
answerOpt.setOptId(object2Integer(opt));
answerOpt.setCreateTime(new Date());
answerOpt.setVoter(uuid);
optList.add(answerOpt);
}
}
if(object2String(stringObjectMap.get("type")).equals("3") || object2String(stringObjectMap.get("type")).equals("4")){
AnswerTxt answerTxt = new AnswerTxt();
answerTxt.setQuestionId(object2Integer(stringObjectMap.get("questionId")));
answerTxt.setSurveyId(object2Integer(stringObjectMap.get("surveyId")));
answerTxt.setResult(object2String(stringObjectMap.get("result")));
answerTxt.setCreateTime(new Date());
answerTxt.setVoter(uuid);
txtList.add(answerTxt);
}
}
surveyService.submit(optList,txtList);
return MapControl.getInstance().success().getMap();
}
@GetMapping("/query_detail/{id}")
public String query_detail(@PathVariable("id") Integer id,ModelMap modelMap){
Survey survey = surveyService.detail(id);
Question question = new Question();
question.setSurveyId(survey.getId());
//查询一个问卷中的所有问题及选项
List<Question> questions = questionService.query(question);
//将问题设置为survey的属性
survey.setQuestions(questions);
//总投票人数
AnswerOpt answerOpt = new AnswerOpt();
answerOpt.setSurveyId(id);
List<AnswerOpt> answerOpts = surveyService.queryAnswerOpt(answerOpt);
Set<String> set = new HashSet<String>();
for (AnswerOpt opt : answerOpts) {
set.add(opt.getVoter());
}
for (Question question1 : questions) {
for (QuestionOpt questionOpt : question1.getOptions()) {
int num = 0;
for (AnswerOpt opt : answerOpts) {
if(questionOpt.getId() == opt.getOptId()){
num++;
}
}
questionOpt.setNum(num);
}
}
modelMap.addAttribute("survey",survey);
modelMap.addAttribute("total",set.size());
return "survey/query_detail";
}
public String object2String(Object object){
if(object !=null){
return object+"";
}else{
return null;
}
}
public Integer object2Integer(Object object){
if(object !=null){
return Integer.parseInt(object+"");
}else{
return null;
}
}
}
后台管理员管理控制层:
@Controller
@RequestMapping("/admin")
public class AdminController {
@Autowired
private AdminService adminService;
@GetMapping("/create")
public String v_create(){
return "admin/add";
}
@PostMapping("/create")
@ResponseBody
public Map<String,Object> create(@RequestBody Admin admin){
admin.setPassword(MD5Utils.getMD5(admin.getPassword()));
int result = adminService.create(admin);
if(result<=0){
//失败的情况下
return MapControl.getInstance().error().getMap();
}
return MapControl.getInstance().success().getMap();
}
@PostMapping("/delete")
@ResponseBody
public Map<String,Object> delete(String ids){
int result = adminService.deleteBatch(ids);
if(result<=0){
//失败的情况下
return MapControl.getInstance().error().getMap();
}
return MapControl.getInstance().success().getMap();
}
@PostMapping("/update")
@ResponseBody
public Map<String, Object> update(@RequestBody Admin admin){
int result = adminService.update(admin);
if(result<=0){
//失败的情况下
return MapControl.getInstance().error().getMap();
}
return MapControl.getInstance().success().getMap();
}
@GetMapping("/list")
public String list(){
return "admin/list";
}
@PostMapping("/query")
@ResponseBody
public Map<String,Object> query(@RequestBody Admin admin, ModelMap modelMap){
System.out.println(admin.getPage());
System.out.println(admin.getLimit());
System.out.println(admin.getAccount());
List<Admin> list = adminService.query(admin);
Integer count = adminService.count(admin);
return MapControl.getInstance().page(list,count).getMap();
}
@GetMapping("/detail")
public String detail(Integer id,ModelMap modelMap){
Admin admin = adminService.detail(id);
modelMap.addAttribute("admin",admin);
return "admin/update";
}
}