Struts2_使用 Filter 作为控制器的 MVC 应用

实现 MVC(Model、View、Controller) 模式的应用程序由 3 大部分构成:
模型:封装应用程序的数据和业务逻辑POJO(Plain Old Java Object)
视图:实现应用程序的信息显示功能(JSP)
控制器:接收来自用户的输入,调用模型层,响应对应的视图组件(Servlet,Filter)
新建一个web动态工程,然后在WebContent文件夹下新建index.jsp页面
index.jsp:

<body>
    <a href="product-input.action">Product Input</a>
</body>

然后在WEB-INF下新建pages文件夹,里面新建
input.jsp页面:

<body>

    <form action="product-save.action" method="post">

        ProductName: <input type="text" name="productName"/>
        <br><br>

        ProductDesc: <input type="text" name="productDesc"/>
        <br><br>

        ProductPrice: <input type="text" name="productPrice" />
        <br><br>

        <input type="submit" value="Submit"/>
        <br><br>

    </form>

</body>

details.jsp页面:

<body>

    ProductId: ${requestScope.product.productId }
    <br><br>

    ProductName: ${requestScope.product.productName }
    <br><br>

    ProductDesc: ${requestScope.product.productDesc }
    <br><br>

    ProductPrice: ${requestScope.product.productPrice }
    <br><br>

</body>

再在src新建包,包名:com.atguigu.struts2.helloworld

创建 Product 类:

package com.atguigu.struts2.helloworld;

public class Product {

    private Integer productId;
    private String productName;
    private String productDesc;

    private double productPrice;

    public Integer getProductId() {
        return productId;
    }

    public void setProductId(Integer productId) {
        this.productId = productId;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public String getProductDesc() {
        return productDesc;
    }

    public void setProductDesc(String productDesc) {
        this.productDesc = productDesc;
    }

    public double getProductPrice() {
        return productPrice;
    }

    public void setProductPrice(double productPrice) {
        this.productPrice = productPrice;
    }

    public Product(Integer productId, String productName, String productDesc,
            double productPrice) {
        super();
        this.productId = productId;
        this.productName = productName;
        this.productDesc = productDesc;
        this.productPrice = productPrice;
    }

    public Product() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public String toString() {
        return "Product [productId=" + productId + ", productName="
                + productName + ", productDesc=" + productDesc
                + ", productPrice=" + productPrice + "]";
    }


}

在包里创建一个Filter,Class nameFileterDispatcher,然后next编辑Filter mappings/FileterDispatcher 改为*.action,然后ok,finish:

package com.atguigu.struts2.helloworld;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

/**
 * Servlet Filter implementation class FilterDispatcher
 */
public class FilterDispatcher implements Filter {

    public void destroy() {}

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        HttpServletRequest req = (HttpServletRequest) request;

        //1. 获取 servletPath
        String servletPath = req.getServletPath();
        System.out.println(servletPath);

        String path = null;

        //2. 判断 servletPath, 若其等于 "/product-input.action", 则转发到
        ///WEB-INF/pages/input.jsp
        if("/product-input.action".equals(servletPath)){
            path = "/WEB-INF/pages/input.jsp";
        }

        //3. 若其等于 "/product-save.action", 则
        if("/product-save.action".equals(servletPath)){
            //1). 获取请求参数
            String productName = request.getParameter("productName");
            String productDesc = request.getParameter("productDesc");
            String productPrice = request.getParameter("productPrice");

            //2). 把请求信息封装为一个 Product 对象
            Product product = new Product(null, productName, productDesc, Double.parseDouble(productPrice));

            //3). 执行保存操作
            System.out.println("Save Product: " + product);
            product.setProductId(1001);

            //4). 把 Product 对象保存到 request 中. ${param.productName} -> ${requestScope.product.productName}
            request.setAttribute("product", product);

            path = "/WEB-INF/pages/details.jsp";
        }

        if(path != null){
            request.getRequestDispatcher(path).forward(request, response);
            return;
        }

        chain.doFilter(request, response);
    }

    public void init(FilterConfig fConfig) throws ServletException {}

}

使用 Filter 作为控制器的好处 使用一个过滤器来作为控制器, 可以方便地在应用程序里对所有资源(包括静态资源)进行控制访问.

<url-pattern>*.action</url-pattern>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值