Spring MVC模式示例(采用解耦控制器)
Product
package com.mstf.bean; import java.io.Serializable; /** * Product类,封装了一些信息,包含三个属性 * @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是表单类 * 作用:当数据校验失败时,用于保存和展示用户在原始表单的输入 * @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 { @Override public 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 { @Override public String handleRequest(HttpServletRequest req,HttpServletResponse resp) { // 构建一个ProductForm表单对象 ProductForm productForm = new ProductForm(); // 写入表单对象 productForm.setName(req.getParameter("name")); productForm.setDescription(req.getParameter("description")); productForm.setPrice(req.getParameter("price")); // 创建模型 Product product = new Product(); product.setName(productForm.getName()); product.setDescription(productForm.getDescription()); try { product.setPrice(Float.parseFloat(productForm.getPrice())); } catch (NumberFormatException e) { e.printStackTrace(); } // 添加 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 HttpServlet { private static final long serialVersionUID = 1L; @Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { process(req, resp); } @Override public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { process(req, resp); } /** * 这个方法用来处理所有输入请求 * @param req * @param resp * @throws IOException * @throws ServletException */ private void process(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException { req.setCharacterEncoding("UTF-8"); //转码 resp.setCharacterEncoding("UTF-8"); String uri=req.getRequestURI(); // 获取请求URI int lastIndex=uri.lastIndexOf("/"); String action=uri.substring(lastIndex+1); // 获取action名称 String dispatchUrl=null; // 执行方法 if(action.equals("product_input.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); } } }
我们有两个方法来进行软件设计:一个是让其足够的简单以至于让BUG无法藏身;另一个就是让其足够的复杂,让人找不到BUG。前者更难一些。