发表于: 2017-12-18 20:09:30

1 585


今天完成的事情:

把任务一到任务五的结合做完了。

贴一下项目结构:

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object var3) throws Exception {
Cookie[] cookies = request.getCookies();
   if(cookies!=null) {
for (Cookie cookie: cookies) {
if(cookie.getName().equals("name")){
int id = Integer.parseInt(util.decrypt(cookie.getValue()));
               if(userService.findByID(id)!=null){
return true;
               }
}
}

}else {
response.sendRedirect("/user");
       return false;
   }
response.sendRedirect("/user");
   return false;
}


CRUD

//新增的同时,提交了创建时间,同时还提交了更新时间。

@RequestMapping(value = "/newStudent",method = RequestMethod.POST)
public String insert(String name, Long QQ, String studyType, Date date, String graduation, String studentID, String dailyLink, String desire, String bro){
Calendar c =Calendar.getInstance();
   c.setTime(date);
   System.out.println("name----->"+name);
   System.out.println("date----->"+date);
   Long newDate = c.getTimeInMillis();
   System.out.println(newDate);
   Long newUpdate_at = System.currentTimeMillis();
   Long newCreate_at = System.currentTimeMillis();
   studentService.insert(name,QQ,studyType,newDate,graduation,studentID,dailyLink,desire,bro,newUpdate_at,newCreate_at);
   //重定向到所有学生页
   return "redirect:allStudent";
}
//查看所有学生
@RequestMapping(value = "/allStudent",method = RequestMethod.GET)
public String list(Model model){
List<Student> studentList = studentService.list();
   model.addAttribute("studentList",studentList);
   return "studentList";
}
//编辑学生
@RequestMapping(value = "/student",method = RequestMethod.GET)
public String update(int id,Model model){
Student student = studentService.getStudent(id);
   model.addAttribute("student",student);
   System.out.println(student.getDate());
   System.out.println("********************");
   return "editStudent";
}
@RequestMapping(value = "/student",method = RequestMethod.POST)
public String update(int id ,String name,Long QQ, String studyType, Date date, String graduation, String studentID, String dailyLink, String desire, String bro){
Calendar c =Calendar.getInstance();
   c.setTime(date);
   System.out.println("name----->"+name);
   System.out.println("date----->"+date);
   Long newDate = c.getTimeInMillis();
   System.out.println(newDate);
   Long newUpdate_at = System.currentTimeMillis();
   studentService.update(id,name,QQ,studyType,newDate,graduation,studentID,dailyLink,desire,bro,newUpdate_at);
   return "redirect:allStudent";
}
//删除学生
@RequestMapping(value = "/oldStudent",method = RequestMethod.GET)
public String delete(int id){
studentService.delete(id);
   return "redirect:allStudent";
}
//查看一个学生
@RequestMapping("singleStudent")
public String getStudent(int id,Model model){
Student student = studentService.getStudent(id);
   model.addAttribute("student",student);
   return "singleStudent";
}

//登录流程。登录注销

@RequestMapping(value = "/user",method = RequestMethod.POST)

public String forLogin(String name, String password, HttpServletRequest request, HttpServletResponse response, Model model, HttpSession session) throws Exception{
logger.info("把用户输入密码执行MD5加密后进入用户身份判断");
   System.out.println("登录是输入密码"+password);
   password = MD5UtilAndSalt.getMD5(password);
   System.out.println("登录时加密后密码为:"+password);
   User user =  userService.findByNameAndPassword(name,password);
   System.out.println("user"+user);
   if(user!=null){
String des =user.getId()+"="+System.currentTimeMillis();
       System.out.println("userID---->"+user.getId());
       String token = util.encryption(des);
       Cookie cookie  = new Cookie("name",token);
       cookie.setPath("/");
       cookie.setMaxAge(24*60*60);
       model.addAttribute("user",user);
       response.addCookie(cookie);
       return "hello";
   }else {
logger.info("帐密错误");
       return "web";
   }
}
//注销登录
@RequestMapping(value = "/loginOut",method = RequestMethod.GET)
public String loginOut(HttpServletRequest request, HttpServletResponse response, ModelAndView modelAndView){
Cookie[] cookies =  request.getCookies();
   if(cookies !=null){
for (Cookie cookie:cookies) {
if(cookie.getName().equals("name")){
cookie.setValue(null);
               cookie.setMaxAge(0);
               cookie.setPath("/");
               response.addCookie(cookie);
               System.out.println("删除cookie");
               return"web";
           }
       }
   }
return null;
}

@RequestMapping(value = "/newUser", method = RequestMethod.GET)
public String register(){
System.out.println("进入注册界面");
   logger.info("注册界面");
   return "register";
}
@RequestMapping(value = "/newUser", method = RequestMethod.POST)
public String forRegister(String name,String password){
logger.info("传入的姓名是:"+name);
   logger.info("传入的密码为:"+password);
   System.out.println(name);
   List<User> list  = userService.nameList();
   System.out.println("list"+list);
       //前台表单如果不输入数据,直接提交,值是"",而不是NULL
   if(name==null||name.trim().equals("")){
logger.info("姓名不能为空");
       return null;
       }else if(password==null||password.trim().equals("") ) {
logger.info("密码不能为空");
           return null;
       }else{
for (User user:list) {
if(name.equals(user.getName())){
System.out.println("name不能一样");
                   break;
               }else {
Calendar c = Calendar.getInstance();
                   Long create_at =c.getTimeInMillis();
                   password = MD5UtilAndSalt.getMD5(password);
                   logger.info("加密后的密码为:"+password);
                   userService.addUser(name, password,create_at);
                   return "web";
               }
           }
       }
System.out.println("&&&&&&&&&");
       return null;
}

//拦截器

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object var3) throws Exception {

Cookie[] cookies = request.getCookies();
   if(cookies!=null) {
for (Cookie cookie: cookies) {
if(cookie.getName().equals("name")){
int id = Integer.parseInt(util.decrypt(cookie.getValue()));
               if(userService.findByID(id)!=null){
return true;
               }
           }
       }

   }else {
response.sendRedirect("/user");
       return false;
   }
response.sendRedirect("/user");
   return false;
}

嗯,整合了一下之前做的很多东西。感觉还不错。一个基本流程吧。

明天计划的事情:

服务器蹦了,明天搞小课堂,把服务器搞好吧。
遇到的问题:

每太大问题。多个页面整合,要注意各个界面之间的关系。理清各个层次这样更容易理解。

收获:熟悉了一下基本流程。


返回列表 返回列表
评论

    分享到