Struts 1 components
Bài đăng này đã không được cập nhật trong 3 năm
Struts 1 Framework is popular framework supporting building web application with applying MVC pattern.
Audience
The document is prepare for people who has a basic knowledge of MVC pattern. After completeing the document you will get more clearly about struts 1 framwork.
Purpose:
In the document, I intend to mention to radical concepts of Struts I framework based on MVC pattern. Following this knowledge we achieve, we can apply to analyze and design a web application in detail such as the way to design, setup application using Struts.
Struts I framework advantages
- Struts I framework also likes other frameworks which is very helpful to design and build a web application with MVC 2 pattern. It includes many libraries and utilities to support user deploy a web application easily and quickly.
- According to MVC standard, the separating web application is into three parts: Model - View - Controller, that is quite useful. Nonetheless, the action still limits such as forwarding to new page, connection, transfer and processing data between Model and View is dependent very often Java code in servlet. With Struts I, mapping and rendering is very flexible via configuration file (struts-config.xml), which defines mapping, rendering, processing Model and View. The file is going to parse to corresponding class in running time with role of Controller to define the processing and hide complex mapping.
- In addition, with Struts configuration allow changing Model components to not impact to user interface because user can manipulate physical data directly. Therefore, although structure of data is changed, structure of Model for accessing, processing and storing data is not. Conceptually, components in Model are function’s names - the number of arguments are rarely changed. Changing content is only implementing way, which user does not concern about. Most users just concern about what interface is, value and how the value was rendered. Thus, this is flexible of MVC in struts framework.
- Struts framework support two primary components: View and Controller, because Struts Framework run towards connecting middleware application server components.
Struts framework components
- The components of Struts I framworks are described as below:
1.Basic: Basic component include radical element to build a web application based on MVC 2 pattern.
2.Library tags: The component supports some library tags to build a web application without any library tags. It uses HTML control directly.
3.Plugin tiles: It supports building application with Master page for whole web site.
4.Validator plugins: The component supports validating on server side by configuration in form and control in xml.
5.ActionServlet is Java class executing role of Controller to collect all HTTP request, create objects relating struts framework during its active time. Programmer can customize the ActionServlet by extending the class. ActionServerlet need to be required in web.xml. For example:
<servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> </servlet>
6.Struts configuration (struts-config.xml). All configuration will be parsed and storing in JavaBeans of package org.apache.struts.config.
7.ActionForm: This is Java class which receives and validate input data from a HTML form before sending to Action class. All ActionForm class are also extended from org.apache.struts.action.ActionForm. ActionForm is quie like Javabean, nevertheless its scopes are only session and request instead of 4 scopes. ActionForm is classified 2 types, which are StaticForm (extends form ActionForm) and DynamicForm (existed Form). All ActionForm class need to be defined in struts-config.xml to execute mapping between Action and View. For example:
Static form:
<form-beans> <form-bean name = “beanName” type = “packageName.className”/> </form-beans>
Dynamic form:
<form-beans> <form-bean name = “beanName” type = “packageName.className”> <form-property name = “propertyName” type = “type of data”/> … </form-bean> </form-beans>
8.Action: Action is Java class processing request and response by calling JavaObject or Model. Each action need to be mapped in struts-config.xml. One of necessary method needed to define in Action class is execute() method. The method have responsibility for processing business logic. The method will return a ActionForward instance which determines next processing component for request.
. For instance:
public ActionForward execute (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception.
Struts I work follow diagram
-
The work follow of struts I draw below:
-
When user send request from Browser, the request will be gone forwarded to ActionServlet which is available servlet of the framework to have Controller’s role.
-
ActionServlet is going to process request and mapping with content in XML file to determine corresponding processing components. In case, There are no corresponding components, response is 404 error for end user. Oppositely, Corresponding Action and View will be determined. The View includes form to collect input data and output data from user. Every Form’s values will be stored into Form Bean, which is a Java class. Before transferring to Action, input data may be validated by validator() method in FormBean.
-
Action will receive data from FormBean and call Java class (EJB or DAO) to connect to DB to execute business activities such as Create, Replace, Update And Delete.
-
When the Action’s processing is completed, the result will be sent back to ActionServlet to perform mapping in struts-config.xml to determine final out view for user.
-
Finally, the result from processing of Action and Form Bean will be poured into JSP page with successful state for ActionServlet which response the result for client. In conclusion, Struts’s activities follow is quite similar with MVC2 follow. The huge difference is appearing of struts-config.xml file which define every important components. In addition, mapping action is almost independent with servlet code so that it is quite flexible.
In conclusion, all of the components in Struts 1 framework are presented above. Hopefully, the document is helpful with people who is not any knowledge about the framwork.
All rights reserved