[原创]java WEB学习笔记53:Struts2学习之路---前奏:使用 Filter 作为控制器的 MVC

本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用

内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系。

本人互联网技术爱好者,互联网技术发烧友

微博:伊直都在0221

QQ:951226918

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

1.MVC 设计模式概览:

实现 MVC(Model、View、Controller) 模式的应用程序由 3 大部分构成:

   1)模型:封装应用程序的数据和业务逻辑:POJO(Plain Old Java Object)

   2)视图:实现应用程序的信息显示功能:JSP

   3)控制器:接收来自用户的输入,调用模型层,响应对应的视图组件:Servlet,Filter

 

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

Servlet VS Filter

Servlet 能做的 Filter 是否都可以完成 ? 嗯。 Filter 能做的 Servlet 都可以完成吗 ? 拦截资源却不是 Servlet 所擅长的! Filter 中有一个 FilterChain,这个 API 在 Servlet 中没有!

 

3.demo

  

 

 

 

  

代码

 

index.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 
11 
12     <a href="product-input.action">Product Input</a>
13 </body>
14 </html>

 

 

 

input.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>input page</title>
 8 </head>
 9 <body>
10         
11     <form action="product-save.action" method="post">
12         ProductName:<input type="text" name="productName"/>
13         <br><br>
14         
15         ProductDesc:<input type="text" name="productDesc"/>
16         <br><br>
17 
18         ProductPrice:<input type="text"  name="productPrice"/>
19         <br><br>
20 
21         <input type="submit"  value="submit" />
22     </form>
23 </body>
24 </html>

 

 

 

detial.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>detail page</title>
 8 </head>
 9 <body>
10     
11         ProductId:${requestScope.product.productId }
12         <br><br>
13         ProductName:${requestScope.product.productName }
14         <br><br>
15         ProductDesc:${requestScope.product.productDesc }
16         <br><br>
17         ProductPrice:${requestScope.product.productPrice }
18         <br><br>
19         
20         
21 </body>
22 </html>

 

 

product.java

 1 package com.jason.struts.helloword;
 2 
 3 public class Product {
 4     
 5     private Integer productId;
 6     private String productName;
 7     private String productDesc;
 8      
 9     private double productPrice;
10     
11     
12     
13     
14 
15     public Product() {
16         super();
17     }
18 
19     public Product(Integer productId, String productName, String productDesc,
20             double productPrice) {
21         super();
22         this.productId = productId;
23         this.productName = productName;
24         this.productDesc = productDesc;
25         this.productPrice = productPrice;
26     }
27 
28     @Override
29     public String toString() {
30         return "Product [productId=" + productId + ", productName="
31                 + productName + ", productDesc=" + productDesc
32                 + ", productPrice=" + productPrice + "]";
33     }
34 
35     public Integer getProductId() {
36         return productId;
37     }
38 
39     public void setProductId(Integer productId) {
40         this.productId = productId;
41     }
42 
43     public String getProductName() {
44         return productName;
45     }
46 
47     public void setProductName(String productName) {
48         this.productName = productName;
49     }
50 
51     public String getProductDesc() {
52         return productDesc;
53     }
54 
55     public void setProductDesc(String productDesc) {
56         this.productDesc = productDesc;
57     }
58 
59     public double getProductPrice() {
60         return productPrice;
61     }
62 
63     public void setProductPrice(double productPrice) {
64         this.productPrice = productPrice;
65     }
66     
67 
68 
69 }

 

 

 

FilterDispatcher.java

 1 package com.jason.struts.helloword;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.Filter;
 6 import javax.servlet.FilterChain;
 7 import javax.servlet.FilterConfig;
 8 import javax.servlet.ServletException;
 9 import javax.servlet.ServletRequest;
10 import javax.servlet.ServletResponse;
11 import javax.servlet.annotation.WebFilter;
12 import javax.servlet.http.HttpServletRequest;
13 import javax.servlet.http.HttpServletResponse;
14 
15 /**
16  * Servlet Filter implementation class FilterDispatcher
17  */
18 @WebFilter("*.action")
19 public class FilterDispatcher implements Filter {
20 
21  
22     
23     public void destroy() {
24         
25     }
26 
27     
28     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
29         
30         HttpServletRequest req = (HttpServletRequest)request;
31         //1.获取servletPath
32             String  servletPath = req.getServletPath();
33             System.out.println(servletPath);
34             String path = null;
35         //2.判断 servletPath,若其等于 "/product-input.action", 则转发到WEB-INF/struts/input.jsp
36         if("/struts2/product-input.action".equals(servletPath)){
37             path = "/struts2/input.jsp"; 
38 
39         }
40         //3.若servletPath,为"/product-save.action",则
41          if("/struts2/product-save.action".equals(servletPath)){
42              
43              //1) 获取请求参数
44                  String productName = request.getParameter("productName");
45                  String productDesc = request.getParameter("productDesc");
46                  String productPrice = request.getParameter("productPrice");
47                  
48                  //2) 把请求信息封装我一个Product 对象
49                  Product product = new Product(null, productName, productDesc, Double.parseDouble(productPrice));
50                  //3) 执行保存操作
51                  System.out.println("save Product" + product + "" );
52                  
53                  product.setProductId(1001);
54                  request.setAttribute("product", product);
55                  
56                  path = "/struts2/details.jsp";
57                  
58                  
59              
60              
61          }
62         //4.把Product 对象保存到request 中。
63         
64         if(path != null){
65             req.getRequestDispatcher(path).forward(request, response);
66             return;
67         }
68         
69         chain.doFilter(request, response);
70     }
71 
72     public void init(FilterConfig fConfig) throws ServletException {
73         
74     }
75 
76 
77 
78     
79 
80 }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  

 

posted @ 2016-08-01 17:35  jason_zhangz  阅读(219)  评论(0编辑  收藏  举报