Spring MVC pattern example (using a Decoupling Controller), 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 javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import com. mstf. bean. product; import com. mstf. bean. form. productForm; 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"); // create model Product product = new Product (); product. setName (productForm. getName (); product. setDescription (productForm. getDescription (); try {product. setPrice (Float. parseFloat (productForm. getPrice ();} catch (NumberFormatException e) {e. printStackTrace ();} // Add req. setAttribute ("product", product); return "/WEB-INF/jsp/ProductDetails. 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 );}}}