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

news/2025/2/27 4:10:35

使用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>
     
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

index.jsp

<body>
    <a href="product-input.action">product input</a>
</body>
     
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

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>
     
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

details.jsp

<body>
    ProductName:${requestScope.product.productId}<br/>
    ProductName:${requestScope.product.productName}<br/>
    ProductName:${requestScope.product.productDesc}<br/>
    ProductName:${requestScope.product.productPrice}<br/>
  </body>
     
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

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}-&amp;gt;${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 {}
}
     
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

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 + "]";
    }
}
     
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
(function () {('pre.prettyprint code').each(function () { var lines = (this).text().split(\n).length;var numbering = $('
  • ').addClass('pre-numbering').hide(); (this).addClass(hasnumbering).parent().append( numbering); for (i = 1; i

http://www.niftyadmin.cn/n/702494.html

相关文章

热评云厂商:坚果云0.65亿元,继续推进云存储业务,提振在即

全球云观察《云白皮书&#xff08;2020-2021&#xff09;》热评云厂商60家之五十五 上海亦存网络科技有限公司是坚果云的实际运营公司&#xff0c;希望能够2020年左右登陆创业板&#xff0c;然而到目前为止还没有正式IPO&#xff0c;同时公司的性质还没有调整为股份制公司&…

2.Struts2_概述

转载请注明&#xff1a;http://blog.csdn.net/uniquewonderq 1.struts2背景 由出色稳定的框架struts1和WebWork框架整合而来。 &#xff08;struts1是Apache软件基金会赞助的一个开源项目&#xff0c;它通过采育好难过javaservlet/jsp技术&#xff0c;实现了基于java ee web应…

热评云厂商:银联云开启金融新科技,构建富有代表性的行业云

全球云观察《云白皮书&#xff08;2020-2021&#xff09;》热评云厂商60家之五十七 有时候&#xff0c;阿明在思考&#xff0c;一个时代总会诞生一个时代新的云。在面对行业数字化进程加速的今天&#xff0c;任何一个行业都可能诞生富有代表性的行业云&#xff0c;金融领域自然…

3.Struts2_搭建环境及实例

刚刚接触struts2&#xff0c;有点懵懵懂懂&#xff0c;还是习惯于先写代码&#xff0c;然后慢慢来理解其中的思想。 这篇文章主要内容是strusts的环境搭建及通过一个简单的例子来理解到底是怎么使用struts来简化编程的。 1.项目结构如下如&#xff0c;包括必须的包 2.web.xm…

云数据库,谁才是领导者?

2021年新冠疫情进入第二年&#xff0c;对全球的社会、经济而言是不平凡之年&#xff0c;这句话也可用于概括云数据库的发展。随着中国厂商逐步进入全球云数据库市场重要舞台&#xff0c;我们也看到一名世界级的中国高手正式亮相。 2021年Gartner云数据库管理系统魔力象限公布后…

4.Struts2_详解hellostruts

第四节是在第三节搭配环境后做的小练习。 1.项目结构如下如&#xff0c;包括必须的包 &#xff08;导入struts2.jar包&#xff0c;导入到lib目录下&#xff09; 2..web.xml <?xml version"1.0" encoding"UTF-8"?> <web-app xmlns:xsi"htt…

3.1.struts.xml中package的namespace属性

struts.xml文件中package标签除了有name、extends属性外&#xff0c;还有一个十分重要的属性&#xff1a;namespace。如下&#xff1a; <struts> <package name”struts2” extends”struts-default” namespace”/abc”> <!– 用户登录Action –> …

每周全球科技十大看点(2022.1.10-1.16)

看点摘要&#xff1a; 美光科技发布了2022财年第一季度财报&#xff0c;营收同比增长33%谷歌不仅大力拓展公有云&#xff0c;同时重金100亿美元发展云安全2021年Gartner云数据库管理系统魔力象限公布&#xff0c;国内企业仅有两家入选法国CNIL宣布对谷歌和脸书处以1.5亿欧元和…