SpringMVC 简易模拟
-
为了安全,我们将涉及数据的网页放入WEB-INF文件夹下.
<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>
-
设置模拟控制器
@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控制具体逻辑.
-
ProductInputController类直接转发,实现内部转发至WEB-INF下的jsp
public class ProductInputController implements Controller { @Override public String handleRequest(HttpServletRequest request, HttpServletResponse response) { return "/WEB-INF/index.html"; } }
-
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"; } }
-
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; } }