Spring MVC模式樣本(採用解耦控制器),mvc

來源:互聯網
上載者:User

Spring MVC模式樣本(採用解耦控制器),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 {@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) {// 構建一個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;@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);}/** * 這個方法用來處理所有輸入請求 * @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(); // 擷取請求URIint 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);}}}

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.