#千锋逆战# springMVC简易模拟

SpringMVC 简易模拟

  1. 为了安全,我们将涉及数据的网页放入WEB-INF文件夹下.

    • 图1.jpg
  • <body>
    <ul>
        <c:forEach var="${errors}" items="item">
            <li>${item}</li>
        </c:forEach>
    </ul>
        <form method="post" action="ProductDetail">
            name:<input name="name"> <br/>
            age:<input name="age"> <br/>
            sex:<input name="sex"> <br/>
            classRoom:<input name="classRoom"> <br/>
            <input type="submit">
        </form>
    </body>
    
  1. 设置模拟控制器

    • @WebServlet(urlPatterns = {"/ProductInput", "/ProductDetail"})
      public class DispatcherServlet extends HttpServlet {
          protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      
              String requestURI = request.getRequestURI();
      
              String action = requestURI.substring(requestURI.lastIndexOf("/") + 1);
      
              Controller controller = null;
      
              if("ProductInput".equalsIgnoreCase(action)){
                  controller = new ProductInputController();
              }else if("ProductDetail".equalsIgnoreCase(action)){
                  controller = new ProductDetailController();
              }
      
              String url = controller.handleRequest(request, response);
      
              request.getRequestDispatcher(url).forward(request, response);
          }
      
          protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
              doPost(request, response);
          }
      }
      
    • ProductInput请求和ProductDetail请求都会通过控制器,然后由控制器转发地址.ProductInputController和ProductDetailController控制具体逻辑.

  2. ProductInputController类直接转发,实现内部转发至WEB-INF下的jsp

    • public class ProductInputController implements Controller {
          @Override
          public String handleRequest(HttpServletRequest request, HttpServletResponse response) {
              return "/WEB-INF/index.html";
          }
      }
      
  3. ProductDetailController类接受数据并进行校验,通过校验结果返回转发地址.

    • public class ProductDetailController implements Controller {
          @Override
          public String handleRequest(HttpServletRequest request, HttpServletResponse response) {
      
              String name = request.getParameter("name");
              int age = Integer.parseInt(request.getParameter("age"));
              String sex = request.getParameter("sex");
              String classRoom = request.getParameter("classRoom");
      
              Student student = new Student();
              student.setAge(age);
              student.setClassRoom(classRoom);
              student.setName(name);
              student.setSex(sex);
      
      
              ProductValidate pv = new ProductValidate();
              List<String> errors = pv.validate(student);
      
              if(errors != null && !errors.isEmpty()){
                  request.setAttribute("errors", errors);
                  return "/WEB-INF/index.jsp";
              }
      
      
              System.out.println(name);
              System.out.println(age);
              System.out.println(sex);
              System.out.println(classRoom);
      
              return "/WEB-INF/product.jsp";
          }
      }
      
  4. ProductValidate类,具体实施校验方法

    • public class ProductValidate {
      
          /**
           * 校验给定的Student对象
           *
           * @param student 要校验的对象
           * @return 表单校验不成功,则将错误信息存储
           */
          public List<String> validate(Student student) {
              List<String> errors = null;
      
              String name = student.getName();
              int age = student.getAge();
              String classRoom = student.getClassRoom();
              String sex = student.getSex();
      
      
              errors = new ArrayList<>();
      
              if (name == null || name.length() == 0) {
                  errors.add("product name must not be empty.");
              }
      
              if (classRoom == null || classRoom.length() == 0) {
                  errors.add("product classRoom must not be empty.");
              }
      
      
              if (sex == null || sex.length() == 0) {
                  errors.add("sex must not be empty.");
              }
      
              if (age < 0) {
                  errors.add("the age must be a positive number.");
              }
      
              return errors;
          }
      }
      
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容