Struts 2 Architecture
Struts 2 架构
Struts and webwork has joined together to develop the Struts 2 Framework. Struts 2 Framework is very extensible and elegant for the development of enterprise web application of any size. In this section we are going to explain you the architecture of Struts 2 Framework
struts 和 webwork 一起组成了struts2开发框架。struts 2 框架具有很高的扩展性适合开发任何大小的企业级应用程序。在这一章我们会为您解释struts2框架的架构。
1. Request Lifecycle in Struts 2 applications
Struts 2应用程序的请求生命周期
- FilterDispatcher determines the appropriate action: The FilterDispatcher looks at the request and then determines the appropriate Action.
FilterDispatcher 将决定哪个action将会被调用: FilterDispatcher 会根据请求来决定哪一个action被调用
- Interceptors are applied: Interceptors configured for applying the common functionalities such as workflow, validation, file upload etc. are automatically applied to the request.
Interceptors 被应用在框架中: 配置拦截器是为了在接受请求时自动调用公共的一些功能 比如工作流, 验证,文件上传等等这些 - Execution of Action: Then the action method is executed to perform the database related operations like storing or retrieving data from the database.
Action 的执行:拦截器拦截了请求之后,action中的方法会被执行以便于执行一些数据库相关操作 比如排序, 数据检索。 - Output rendering: Then the Result renders the output.
输出转向:然后结果会被转到输出。 - Return of Request: Then the request returns through the interceptors in the reverse order. The returning request allows us to perform the clean-up or additional processing.
Request的返回:然后结果会以拦截器拦截请求相反的顺序被返回。返回请求可以使我们来执行一些清理或者附加的操作。 - Display the result to user: Finally the control is returned to the servlet container, which sends the output to the user browser.
Image: Struts 2 high level overview of request processing:
图片:struts 2 request 执行过程一览
Struts 2 Architecture
struts 2 架构
Struts 2 is a very elegant and flexible front controller framework based on many standard technologies like Java Filters, Java Beans, ResourceBundles, XML etc.
struts 2 是非常优雅和灵活的前端控制器类型的框架,此框架是基于很多标准技术比如java 过滤器, java beans, ResourceBundles 和xml 等等。
For the Model, the framework can use any data access technologies like JDBC, EJB, Hibernate etc and for the View, the framework can be integrated with JSP, JTL, JSF, Jakarta Velocity Engine, Templates, PDF, XSLT etc.
在模型端, struts 2 框架可以利用任何数据访问技术比如说jdbc , EJB, Hibernate 等等。 在视图端,struts2 可以结合JSP, JTL, JSF, akarta Velocity Engine, Templates, PDF, XSLT 等等。
Exception Handling:
异常处理
The Struts 2 Framework allows us to define exception handlers and inceptors.
strtus 2 框架允许我们定义异常处理和拦截器
- Exception Handlers:
- Exception handlers allows us to define the exception handling procedure on global and local basis. Framework catches the exception and then displays the page of our choice with appropriate message and exception details.
- Interceptors:
- The Interceptors are used to specify the "request-processing lifecycle" for an action. Interceptors are configured to apply the common functionalities like workflow, validation etc.. to the request.
The following diagram depicts the architecture of Struts 2 Framework and also shows the the initial request goes to the servlet container such as tomcat, which is then passed through standard filer chain.
下面的图表描述了Struts 2框架的架构,也显示了初始请求时如何在tomcat这样的容器中被处理的,处理过程是通过一些标准的过滤链。
Image: Struts 2 Architecture
图片:struts 2 架构
The filter chain includes:
过滤链包括了:
- Action ContextCleanUp filter:
The ActionContextCleanUp filter is optional and it is useful when integration has to be done with other technologies like SiteMash Plugin.
- FilterDispatcher:
Next the FilterDispatch is called, which in turn uses the ActionMapper to determine whether to invoke an Action or not. If the action is required to be invoked, the FilterDispatcher delegates the control to the ActionProxy.
- ActionProxy:(Action 代理)
The ActionProxy takes help from Configuration Files manager, which is initialized from the struts.xml. Then the ActionProxy creates an ActionInvocation, which implements the command pattern. The ActionInvocation process invokes the Interceptors (if configured) and then invokes the action. The the ActionInvocation looks for proper result. Then the result is executed, which involves the rendering of JSP or templates.
-
Then the Interceptors are executed again in reverse order. Finally the response returns through the filters configured in web.xml file. If the ActionContextCleanUp filter is configured, the FilterDispatcher does not clean the ThreadLocal ActionContext. If the ActionContextCleanUp filter is not present then the FilterDispatcher will cleanup all the ThreadLocals present.
(本章完).