struts2:使用Filter作为控制器的MVC

使用Fliter作为控制器,可以方便的在应用程序里对所有资源包括静态资源进行控制访问

以下是一个用Filter作为控制器的例子
程序结构
这里写图片描述

web.xml的配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <filter>
    <filter-name>product</filter-name>
    <filter-class>com.struts.first.FilterDispatcher</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>product</filter-name>
    <url-pattern>*.action</url-pattern>
  </filter-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

index.jsp

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

input.jsp

<body>
    <form action="product-save.action" method="post">
        ProductName:<input type="text" name="productName"/>
        <br/>
        ProductDesc:<input type="text" name="productDesc"/>
        <br/>
        ProductPrice:<input type="text" name="productPrice"/>
        <br/>
        <input type="submit" value="submit"/>
    </form>
</body>

details.jsp

<body>
    ProductName:${requestScope.product.productId}<br/>
    ProductName:${requestScope.product.productName}<br/>
    ProductName:${requestScope.product.productDesc}<br/>
    ProductName:${requestScope.product.productPrice}<br/>
  </body>

FilterDispatcher.java

public class FilterDispatcher implements Filter {
    @Override
    public void destroy() {}
    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req=(HttpServletRequest)request;
        String path=null;
        //1.获取servletPath
        String servletPath=req.getServletPath();
        System.out.println(servletPath);
        //2.判断servletPath,若其等于"/product-input.action"则转发到
        //  /WEB-INF/pages/input.jsp
        if("/product-input.action".equals(servletPath)){
            path="/WEB-INF/pages/input.jsp";
        }
        //3.若其等于"/product-input.action"则转发到/WEB-INF/pages/details.jsp
        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.toString());
            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;//重要。转发完就代表已经给过响应了,该方法就要结束,就不能在执行后面的doFilter了
        }
        chain.doFilter(request, response);
    }
    @Override
    public void init(FilterConfig config) throws ServletException {}
}

Product.java

public class Product {  
    private Integer productId;
    private String productName;
    private String productDesc;
    private Double productPrice;
    public Product(){}
    public Product(Integer productId, String productName, String productDesc,
            double productPrice) {
        super();
        this.productId = productId;
        this.productName = productName;
        this.productDesc = productDesc;
        this.productPrice = 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;
    }
    @Override
    public String toString() {
        return "Product [productName=" + productName + ", productDesc="
                + productDesc + ", productPrice=" + productPrice + "]";
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值