Servlet - MVC

版权声明:[email protected] https://blog.csdn.net/zhaoxuyang1997/article/details/82927176

1 项目结构图

项目结构图

2 ControllerServlet.java

package com.zxy97.spring.controller;

import com.zxy97.spring.form.ProductForm;
import com.zxy97.spring.model.Product;
import com.zxy97.spring.action.SaveProductAction;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import javax.servlet.RequestDispatcher;
import javax.servlet.annotation.WebServlet;

/**
 * @description MVC
 * @date 2018-10-02
 * @author zhaoxuyang
 */
@WebServlet(name = "ControllerServlet", urlPatterns = {"/input-product", "/save-product"})
public class ControllerServlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String uri = request.getRequestURI();
        int lastIndex = uri.lastIndexOf("/");
        String action = uri.substring(lastIndex + 1);
        String dispatchUrl = null;
        if (null != action) {
            switch (action) {

                case "input-product":
                    dispatchUrl = "/WEB-INF/jsp/ProductForm.jsp";
                    break;

                case "save-product":
                    ProductForm productForm = new ProductForm();
                    productForm.setName(request.getParameter("name"));
                    productForm.setDescription(request.getParameter("description"));
                    productForm.setPrice(request.getParameter("price"));

                    Product product = new Product();
                    product.setName(productForm.getName());
                    product.setDescription(productForm.getDescription());
                    try {
                        product.setPrice(new BigDecimal(productForm.getPrice()));
                    } catch (Exception e) {
                        dispatchUrl = "/WEB-INF/jsp/ProductDetails.jsp";
                        break;
                    }

                    SaveProductAction saveProductAction = new SaveProductAction();
                    saveProductAction.save(product);
                    request.setAttribute("product", product);
                    dispatchUrl = "/WEB-INF/jsp/ProductForm.jsp";
                    break;

                case "":
                    dispatchUrl = "/WEB-INF/jsp/ProductForm.jsp";
                    break;
            }
        }

        if (dispatchUrl != null) {
            RequestDispatcher rd = request.getRequestDispatcher(dispatchUrl);
            rd.forward(request, response);
        }
    }


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }

}

3 SaveProductAction.java

package com.zxy97.spring.action;

import com.zxy97.spring.model.Product;

public class SaveProductAction {
    public void save(Product product){ 
        System.out.println("将product保存到数据库中:\n" + product.toString());//product保存到数据库中
    }
}

4 ProductForm.java

package com.zxy97.spring.form;

/**
 * @描述 与用户交互的模型
 * @作用 存储用户输入,反馈信息给用户
 */
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;
    }
}

5 Product.java

package com.zxy97.spring.model;

import java.io.Serializable;
import java.math.BigDecimal;

/**
 * @描述 与系统交互的模型
 */
public class Product implements Serializable{
    private String name;
    private String description;
    private BigDecimal price;

    @Override
    public String toString() {
        return "Product{" + "name=" + name + ", description=" + description + ", price=" + price + '}';
    }
    public String getName() {
        return name;
    }
    public String getDescription() {
        return description;
    }
    public BigDecimal getPrice() {
        return price;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public void setPrice(BigDecimal price) {
        this.price = price;
    }
}

6 ProductForm.jsp

<%-- 
    Document   : ProductForm
    Created on : 2018-10-2, 16:09:09
    Author     : zhaoxuyang
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <form method="post" action="save-product">
            <h4>添加</h4>
            <label>
                <span>名称:</span>
                <input type="text" name="name" placeholder="填写名称" />
            </label>
            <label>
                <span>描述:</span>
                <input type="text" name="description" placeholder="填写描述" />
            </label>
            <label>
                <span>单价:</span>
                <input type="number" name="price" placeholder="填写单价" step="any" />
            </label>
            <label>
                <span>&nbsp;</span>
                <input type="submit" value="提交" />
            </label>
        </form>
    </body>
</html>

7 ProductDetails.jsp

<%-- 
    Document   : ProductDetails
    Created on : 2018-10-2, 16:08:48
    Author     : zhaoxuyang
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>保存</title>
    </head>
    <body>
        <div>
            <h4>已保存</h4>
            <p>
                <h5>信息:</h5>
                名称:${product.name}
                描述:${product.description}
                价格:${product.price}
            </p>
        </div>
    </body>
</html>

猜你喜欢

转载自blog.csdn.net/zhaoxuyang1997/article/details/82927176
今日推荐