Vue-05-笔记

Posted 寻7

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Vue-05-笔记相关的知识,希望对你有一定的参考价值。

Vue综合案例 学生管理系统

1、效果环境的介绍

在这里插入图片描述
在这里插入图片描述

2、登录功能的实现

  • 环境搭建

    • 从资料中解压《学生管理系统原始项目》,并导入。
  • 代码实现

    • html代码

      onSubmit(formName) {
                      // 为表单绑定验证功能
                      this.$refs[formName].validate((valid) => {
                          if (valid) {
                              //请求服务器完成登录功能
                              axios.post("userServlet","username=" + this.form.username + "&password=" + this.form.password)
                                  .then(resp => {
                                      if(resp.data == true) {
                                          //登录成功,跳转到首页
                                          location.href = "index.html";
                                      }else {
                                          //登录失败,跳转到登录页面
                                          alert("登录失败,请检查用户名和密码");
                                          location.href = "login.html";
                                      }
                                  })
                          } else {
                              return false;
                          }
                      });
                  }
      
    • java代码

      • UserServlet.java
      package com.itheima.controller;
      
      import com.itheima.bean.User;
      import com.itheima.service.UserService;
      import com.itheima.service.impl.UserServiceImpl;
      
      import javax.servlet.ServletException;
      import javax.servlet.annotation.WebServlet;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      import java.io.IOException;
      import java.util.List;
      
      @WebServlet("/userServlet")
      public class UserServlet extends HttpServlet {
          private UserService service = new UserServiceImpl();
          @Override
          protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
              //设置请求和响应编码
              req.setCharacterEncoding("UTF-8");
              resp.setContentType("text/html;charset=UTF-8");
      
              //1.获取请求参数
              String username = req.getParameter("username");
              String password = req.getParameter("password");
      
              //2.封装User对象
              User user = new User(username,password);
      
              //3.调用业务层的登录方法
              List<User> list = service.login(user);
      
              //4.判断是否查询出结果
              if(list.size() != 0) {
                  //将用户名存入会话域当中
                  req.getSession().setAttribute("username",username);
                  //响应给客户端true
                  resp.getWriter().write("true");
              }else {
                  //响应给客户端false
                  resp.getWriter().write("false");
              }
          }
      
          @Override
          protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
              doGet(req,resp);
          }
      }
      
      • UserService.java
      package com.itheima.service;
      
      import com.itheima.bean.User;
      
      import java.util.List;
      /*
          业务层约束接口
       */
      public interface UserService {
          /*
              登录方法
           */
          public abstract List<User> login(User user);
      }
      
      • UserServiceImpl.java
      package com.itheima.service.impl;
      
      import com.itheima.bean.User;
      import com.itheima.mapper.UserMapper;
      import com.itheima.service.UserService;
      import org.apache.ibatis.io.Resources;
      import org.apache.ibatis.session.SqlSession;
      import org.apache.ibatis.session.SqlSessionFactory;
      import org.apache.ibatis.session.SqlSessionFactoryBuilder;
      
      import java.io.IOException;
      import java.io.InputStream;
      import java.util.List;
      
      public class UserServiceImpl implements UserService {
          @Override
          public List<User> login(User user) {
              InputStream is = null;
              SqlSession sqlSession = null;
              List<User> list = null;
              try{
                  //1.加载核心配置文件
                  is = Resources.getResourceAsStream("MyBatisConfig.xml");
      
                  //2.获取SqlSession工厂对象
                  SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
      
                  //3.通过SqlSession工厂对象获取SqlSession对象
                  sqlSession = sqlSessionFactory.openSession(true);
      
                  //4.获取UserMapper接口的实现类对象
                  UserMapper mapper = sqlSession.getMapper(UserMapper.class);
      
                  //5.调用实现类对象的登录方法
                  list = mapper.login(user);
      
              }catch (Exception e) {
                  e.printStackTrace();
              } finally {
                  //6.释放资源
                  if(sqlSession != null) {
                      sqlSession.close();
                  }
                  if(is != null) {
                      try {
                          is.close();
                      } catch (IOException e) {
                          e.printStackTrace();
                      }
                  }
              }
              //7.返回结果到控制层
              return list;
          }
      }
      
      • UserMapper.java
      package com.itheima.mapper;
      
      import com.itheima.bean.User;
      import org.apache.ibatis.annotations.Select;
      
      import java.util.List;
      
      public interface UserMapper {
          /*
              登录方法
           */
          @Select("SELECT * FROM user WHERE username=#{username} AND password=#{password}")
          public abstract List<User> login(User user);
      }
      

3、分页查询功能的实现

  • 代码实现

    • html代码

      <script>
          new Vue({
              el:"#div",
              data:{
                  dialogTableVisible4add: false,  //添加窗口显示状态
                  dialogTableVisible4edit: false, //编辑窗口显示状态
                  formData:{},//添加表单的数据
                  editFormData: {},//编辑表单的数据
                  tableData:[],//表格数据
                  pagination: {
                      currentPage: 1, //当前页
                      pageSize: 5,    //每页显示条数
                      total: 0        //总条数
                  },
                  rules: {
                      number: [
                          {required: true, message: '请输入学号', trigger: 'blur'},
                          {min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur'}
                      ],
                      name: [
                          {required: true, message: '请输入姓名', trigger: 'blur'},
                          {min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur'}
                      ],
                      birthday: [
                          {required: true, message: '请选择日期', trigger: 'change'}
                      ],
                      address: [
                          {required: true, message: '请输入地址', trigger: 'blur'},
                          {min: 2, max: 200, message: '长度在 2 到 200 个字符', trigger: 'blur'}
                      ],
                  }
              },
              methods:{
                  //分页查询功能
                  selectByPage(){
                      axios.post("studentServlet","method=selectByPage&currentPage=" + this.pagination.currentPage + "&pageSize=" + this.pagination.pageSize)
                          .then(resp => {
                              //将查询出的数据赋值tableData
                              this.tableData = resp.data.list;
                              //设置分页参数
                              //当前页
                              this.pagination.currentPage = resp.data.pageNum;
                              //总条数
                              this.pagination.total = resp.data.total;
                          })
                  },
                  //改变每页条数时执行的函数
                  handleSizeChange(pageSize) {
                      //修改分页查询的参数
                      this.pagination.pageSize = pageSize;
                      //重新执行查询
                      this.selectByPage();
                  },
                  //改变页码时执行的函数
                  handleCurrentChange(pageNum) {
                      //修改分页查询的参数
                      this.pagination.currentPage = pageNum;
                      //重新执行查询
                      this.selectByPage();
                  },
                  showAddStu() {
                      //弹出窗口
                      this.dialogTableVisible4add = true;
                  },
                  resetForm(addForm) {
                      //双向绑定,输入的数据都赋值给了formData, 清空formData数据
                      this.formData = {};
                      //清除表单的校验数据
                      this.$refs[addForm].resetFields();
                  },
                  showEditStu(row) {
                      //1. 弹出窗口
                      this.dialogTableVisible4edit = true;
      
                      //2. 显示表单数据
                      this.editFormData = {
                          number:row.number,
                          name:row.name,
                          birthday:row.birthday,
                          address:row.address,
                      }
                  }   
              },
              mounted(){
                  //调用分页查询功能
                  this.selectByPage();
              }
          });
      </script>
      
    • java代码

      • 1、创建StudentServlet.java
      package com.itheima.controller;
      
      import com.fasterxml.jackson.databind.ObjectMapper;
      import com.github.pagehelper.Page;
      import com.github.pagehelper.PageInfo;
      import com.itheima.bean.Student;
      import com.itheima.service.StudentService;
      import com.itheima.service.impl.StudentServiceImpl;
      import org.apache.commons.beanutils.BeanUtils;
      import org.apache.commons.beanutils.ConvertUtils;
      import org.apache.commons.beanutils.Converter;
      
      import javax.servlet.ServletException;
      import javax.servlet.annotation.WebServlet;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      import java.io.IOException;
      import java.text.ParseException;
      import java.text.SimpleDateFormat;
      import java.util.Date;
      import java.util.Map;
      
      @WebServlet("/studentServlet")
      public class StudentServlet extends HttpServlet {
          private StudentService service = new StudentServiceImpl();
          @Override
          protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
              //设置请求和响应编码
              req.setCharacterEncoding("UTF-8");
              resp.setContentType("text/html;charset=UTF-8");
      
              //1.获取方法名
              String method = req.getParameter("method");
              if(method.equals("selectByPage")) {
                  //分页查询功能
                  selectByPage(req,resp);
              }
          }
      
          /*
              分页查询功能
           */
          private void selectByPage(HttpServletRequest req, HttpServletResponse resp) {
              //获取请求参数
              String currentPage = req.getParameter("currentPage");
              String pageSize = req.getParameter("pageSize");
      
              //调用业务层的查询方法
              Page page = service.selectByPage(Integer.parseInt(currentPage), Integer.parseInt(pageSize));
      
              //封装PageInfo
              PageInfo info = new PageInfo(page);
      
              //将info转成json,响应给客户端
              try {
                  String json = new ObjectMapper().writeValueAsString(info);
                  resp.getWriter().write(json);
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      
          @Override
          protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
              doGet(req,resp);
          }
      }
      
      • 2、创建StudentService.java
      package com.itheima.service;
      
      import com.github.pagehelper.Page;
      import com.itheima.bean.Student;
      
      /*
          学生业务层接口
       */
      public interface StudentService {
          /*
              分页查询方法
           */
          public abstract Page selectByPage(Integer currentPage, Integer pageSize);
      }
      
      • 3、创建StudentServiceImpl.java
      package com.itheima.service.impl;
      
      import com.github.pagehelper.Page;
      import com.github.pagehelper.PageHelper;
      import com.itheima.bean.Student;
      import以上是关于Vue-05-笔记的主要内容,如果未能解决你的问题,请参考以下文章

      [原创]java WEB学习笔记61:Struts2学习之路--通用标签 property,uri,param,set,push,if-else,itertor,sort,date,a标签等(代码片段

      sh bash片段 - 这些片段大多只是我自己的笔记;我找到了一些,有些我已经找到了

      需要一种有效的方法来避免使用 Laravel 5 重复代码片段

      Oracle-常用数据库对象笔记(片段)

      如何使用导航组件处理片段内的向上按钮

      JSP笔记