?J2EE Pattern (Presentation Tier Patterns) ?
Intercepting Filter
Front Controller
Context Object
Application Controller
View Helper
Composite View
Service to Worker
Dispatcher View
- 分析Intercepting Filter和Front Controller
Intercepting Filter这一部分没有什么好说的,因为我们web项目运行在servlet容器时,我们的就可以天然的运用过滤器了。
书中提到Servlet 2.3之后,我们用过滤器时可以提供一个基类TemplateFilter,它继承javax.servlet.Filter ,通过它封装基本的Filter代码。当其之类在调用它的时候就可以直接完成过滤前的操作和过滤后的操作了。(这是让仿佛让我想起AOP)
?1?? public abstract class TemplateFilter implements javax.servlet.Filter {
2???? private FilterConfig filterConfig;3???? public void init(FilterConfig filterConfig) throws ServletException {
4?????? this.filterConfig = filterConfig;
5???? }7???? protected FilterConfig getFilterConfig()??? {
8?????? return filterConfig;
9???? }11??? public void doFilter(ServletRequest request,
12??????? ServletResponse response, FilterChain chain)
13??????? throws IOException, ServletException {
14
15?????? // Preprocessing for each filter
16?????? doPreProcessing(request, response);
17
18?????? // Pass control to the next filter in the chain or
19?????? // to the target resource. This method invocation is what logically
20?????? // demarcates preprocessing from postprocessing.
21?????? chain.doFilter(request, response);
22
23?????? // Post-processing for each filter
24?????? doPostProcessing(request, response);
25???? }26???? public abstract void doPreProcessing(ServletRequest request,
27???????? ServletResponse response) { }
28
29???? public abstract void doPostProcessing(ServletRequest request,
30???????? ServletResponse response) { }32???? public void destroy() { }
33? }
子类调用
?1?? public class LoggingFilter extends TemplateFilter {
2???? public void doPreProcessing(ServletRequest req, ServletResponse res) {
3?????? //do some preprocessing here, such as logging some information about
4?????? // the request before it's been handled.
5???? }
6
7???? public void doPostProcessing(ServletRequest req, ServletResponse res){
8?????? // do some post-processing here, such as logging some information
9?????? // about the request and response after the request has been handled
10????? // and the response generated.
11???? }
12? }
Front Controller
struts in action 中描述其运用此模式:
we see that the ActionServlet receives user gestures and state
changes. Another way to say it is that the ActionServlet provides a centralized
access point for request handling. This called the Front Controller pattern, and it is
a key feature of the Model 2 approach. This pattern allows the centralization of
code relating to system services, security services, content retrieval, view manage-
ment, and navigation, so that application code is not endlessly duplicated or com-
mingled with view content
我们可以看到ActionServlet类接收的是用户动作和其状态的改变。也就是说ActionServlet这个类提供的是一个集中切入点处理request请求。这就是前端控制模式,它也是作为Model2模型的一种重要方法。前端控制器这种模式允许集中管理相关的系统服务、安全服务、内容检索、视图管理、导航,使应用代码不再重复或者在视图层的代码不再混淆。????????? core J2EE pattern?
????????????????? 因此我们可以想象struts通过封装ActionServlet类来进行Servlet的分发,对前端进行控制起到FrontController的作用。Action起着ApplicationController的作用。ActionMapping作为分发的作用。
随后通过powerdesigner逆向查看,得出结果....
待续...
Core J2EE Patterns 2nd Editioin解读2
最新推荐文章于 2021-10-10 10:52:50 发布