Spring MVC pattern example (using Decoupling Controller + validator), mvc
Product
Package com. mstf. bean; import java. io. serializable;/*** Product class, which encapsulates some information, including three attributes * @ author wangzheng **/public class Product implements Serializable {private static final long serialVersionUID = 1L; private String name; private String description; private float price; public String getName () {return name;} public void setName (String name) {this. name = name;} public String getDescription () {return description;} public void setDescription (String description) {this. description = description;} public float getPrice () {return price;} public void setPrice (float price) {this. price = price ;}}
ProductForm
Package com. mstf. bean. form;/*** ProductForm is a form class *: When data verification fails, used to save and Display User input in the original form * @ author wangzheng */public class ProductForm {private String name; private String description; private String price; public String getName () {return name;} public void setName (String name) {this. name = name;} public String getDescription () {return description;} public void setDescription (String description) {this. description = description;} public String getPrice () {return price;} public void setPrice (String price) {this. price = price ;}}
Controller
package com.mstf.controller;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public interface Controller {String handleRequest(HttpServletRequest req,HttpServletResponse resp);}
InputProductController
package com.mstf.controller;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class InputProductController implements Controller {@Overridepublic String handleRequest(HttpServletRequest req,HttpServletResponse resp) {return "/WEB-INF/jsp/ProductForm.jsp";}}
SaveProductController
Package com. mstf. controller; import java. util. list; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import com. mstf. bean. product; import com. mstf. bean. form. productForm; import com. mstf. validator. productValidator; public class SaveProductController implements Controller {@ Overridepublic String handleRequest (HttpServletRequest req, HttpServletResponse resp) {// construct a ProductForm object ProductForm productForm = new ProductForm (); // write the form object productForm. setName (req. getParameter ("name"); productForm. setDescription (req. getParameter ("description"); productForm. setPrice (req. getParameter ("price"); // introduces the validator ProductValidator productValidator = new ProductValidator (); List <String> errors = productValidator. validate (productForm); if (errors. isEmpty () {// create model Product product = new Product (); product. setName (productForm. getName (); product. setDescription (productForm. getDescription (); product. setPrice (Float. parseFloat (productForm. getPrice (); req. setAttribute ("product", product); return "/WEB-INF/jsp/ProductDetails. jsp ";} else {req. setAttribute ("errors", errors); req. setAttribute ("form", productForm); return "/WEB-INF/jsp/ProductForm. jsp ";}}}
DispatcherServlet
Package com. mstf. servlet; import java. io. IOException; import javax. servlet. requestDispatcher; import javax. servlet. servletException; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import com. mstf. controller. inputProductController; import com. mstf. controller. saveProductController; public class DispatcherServlet extends HttpS Ervlet {private static final long serialVersionUID = 1L; @ Overridepublic void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {process (req, resp );} @ Overridepublic void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {process (req, resp );} /*** this method is used to process all input requests * @ param req * @ param resp * @ throws IOException *@ Throws ServletException */private void process (HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {req. setCharacterEncoding ("UTF-8"); // transcode resp. setCharacterEncoding ("UTF-8"); String uri = req. getRequestURI (); // obtain the URIint lastIndex = uri. lastIndexOf ("/"); String action = uri. substring (lastIndex + 1); // obtain the action name String dispatchUrl = null; // execution method if (action. equals ("product_inpu T. action ") {InputProductController controller = new InputProductController (); dispatchUrl = controller. handleRequest (req, resp);} else if (action. equals ("product_save.action") {SaveProductController controller = new SaveProductController (); dispatchUrl = controller. handleRequest (req, resp);} if (dispatchUrl! = Null) {RequestDispatcher rd = req. getRequestDispatcher (dispatchUrl); rd. forward (req, resp );}}}
ProductValidator
Package com. mstf. validator; import java. util. arrayList; import java. util. list; import com. mstf. bean. form. productForm;/*** validator * @ author wangzheng **/public class ProductValidator {public List <String> validate (ProductForm productForm) {List <String> errors = new ArrayList <String> (); String name = productForm. getName (); if (name = null | name. trim (). isEmpty () {errors. add ("A name is required! ");} String price = productForm. getPrice (); if (price = null | price. trim (). isEmpty () {errors. add ("the price is required! ");} Else {try {Float. parseFloat (price);} catch (NumberFormatException e) {errors. add (" the entered price is invalid! ") ;}} Return errors ;}}
ProductDetails. jsp
<% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %> <! DOCTYPE html>
ProductForm. jsp
<% @ Page language = "java" contentType = "text/html; charset = UTF-8 "pageEncoding =" UTF-8 "%> <% @ taglib uri =" http://java.sun.com/jsp/jstl/core "prefix =" c "%> <! DOCTYPE html>