A implementation of Struts - - - MVC 2
It is the implementation of the open source of MVC Model 2
It is a group of collaborative classes, servlets, and JSP tags that make up a reusable design.
Is a framework, using Struts, I decide the framework of your Application.
The rich tag library and utility classes independent of the framework can be used as used
Is a subproject in the Apache group
Struts overview
Client Browser (customer browser)
Each HTTP request from the customer browser creates an event. The Web container will respond with an HTTP Request.
Controller
The controller receives a request from the browser and decides where to send this request. For Struts, the controller is a command design pattern implemented by a servlet. Struts-config.xml file configuration controller.
Business logic
Business logic updates the status of the model and helps control the process of the application. For Struts, this is done by the Action class "thin" packaging as the actual business logic.
MODEL (model) status
The model represents the status of the application. Business object updates the status of the application. The ActionForm bean represents the status of the model in the session stage or the request grade, not in the persistence. JSP files read information from Actionform Bean using JSP tags.
View (view)
The view is a JSP file. There is no process logic, no business logic, and there is no model information - only the tag. The tag is one of the factors that make Struts different from other frameworks (such as Velocity).
Components used in the Struts framework
ActionServlet class
The controller component is implemented by org.apache.struts.Action.ActionServlet class, this class is an extension of the javax.servlet.http.httpservlet class, which is the core of this frame.
The controller maps the event (event usually HTTP POST) a servlet. With configuration files, you don't have to hardcode these values.
ActionServlet (Command) creates and uses Action, ActionForm, and ActionForward. When you read the struts-config.xml file, configure the Commuts-Config.xml file. When you create a web project, you need to extend Action and ActionForm to resolve specific issues.
Basic features of Struts Controller
1. Intercept user HTTP request
2. Map this request to the corresponding Action class, if this is the first request received by this class, the initialization instance is slowed down.
3. Create or discover an ActionForm Bean instance (see if the configuration file is defined), and then transplant the request process to bean.
4. Call the perform () method of the Action instance and pass the Actioform bean, Action Mapping object, the request, and the response object to it
5. Perform Returns an actionForWord object, this object is connected to the corresponding JSP page
ActionServlet configuration
We need to declare an actionServlet in web.xml, and configure it to load when starting
In most cases, the standard servlet can meet the needs of users.
init-param>
init-param>
init-param>
servlet>
Action class
All Action classes expand the org.apache.struts.Action.Action class and override the Perform () method defined in the class.
The Action class is a packaging of business logic, and the use is to convert HTTPSERVLETREQUEST to business logic.
When the event progresses to this step, the input form data (or HTML form data) has been extracted from the request stream and transferred to the actionform class. The ActionForm class is passed to the Actino class, which is all automated.
Be careful when expanding the Action class. The Action class should control the process of the application without controlling the logic of the application. We can provide greater flexibility and reusability by placing business logic in separate packages or EJBs.
The Action class must program as "thread security" mode, because the controller will share the plurality of simultaneous requests to share the same instance, according to the design of the Action class, you need to pay attention to the following points.
You cannot use instances or static variables to store status information for specific requests, which share global resources across the request in the same operation.
If you want to access resources (such as JavaBean and Session Variables) need to be protected when parallel access, then access to synchronize
Action configuration
We need to configure an action information in struts-config.xml
Example
Type = "org.apache.struts.Webapp.example.cautionsavection" Name = "CAUTIONSAVEFORM" Scope = "session" INPUT = "/ s63.jsp"> action> Actionform class Actionform class expands org.apache.struts.Action.Actionform class, the BEAN created by program developers can contain additional properties. The framework assumes that the user creates an actionform bean for each form in the application to maintain the session state of the web application. If you use a dynamic ActionForm class, you only need to configure the corresponding configuration in Struts-Config.xml, the framework can automatically generate an ActionForm Bean typical actionFrom bean only the settings and reading methods (Getxxx) of the attribute, without transaction logic. . Only simple input check logic, the purpose of use is to store the latest data entered in the relevant form so that the same web page can be regenerated while providing a set of error messages so that the user can modify the incorrect input data. The Struts framework will do the following for ActionForm Check if the userActionform exists; if there is no existence, it will create an instance of the class. The status of the ActionForm will be set using the corresponding domain in HTTPSERVLETREQUEST. There is not much annoying request.getParameter () call. The STRUTS frame will update its status before passing the ActionForm to the service packaging action. Before passing it to the Action class, Struts also performs form authentication () method for ActionForm, but does not advocate this approach. ActionForm can be maintained at the session level. ActionForm configuration Struts-config.xml file controls the mapping relationship between the HTML form request and the ActionForm. Multiple requests can be mapped to the ActionForm. ActionForm can map across multiple pages to perform operations such as the wizard. Examples are as follows (non-dynamic ActionForm configuration): Need to develop org.apache.struts.Webapp.Example.logonform.java files. Only the domain of the private correspondence page form and its setxxx () and getxxxxxx () methods are required in LogonForm.java. form-bean> Examples are as follows (Dynamic ActionForm configuration): If you need to utilize the data check function of the frame, type = "org.apache.struts.validator.dynavalidatorform" Otherwise Type = "org.apache.struts.action.DynaActionform" form-bean> ActionMApping class Struts-config.xml configuration information is converted to a set of actionMApping, while the latter is placed in the ActionMAppings container. The ActionMApping object helps flow control inside the framework, which can map a specific request URI to a specific Action class and associate an Action class with an ActionForm bean. ActionServlet (Command) passes ActionMApping to the Action class via Perform (). This allows the Action to access information for controlling the flow. Action will use an actionMApping's FindForward () method, this method returns an ActionForward that specifies the name so that Action has completed local forwarding. ActionError class ActionError is a Struts to keep an error list, encapsulate a single error message. The ActionError class is not independently erroneous, which is always stored in the ActionerRors object, and View can use tags to access these classes. As follows: Struts tag library The Struts tag library used by JSP view components consists of four types of tags. Bean tag: Manage Bean in JSP page Logical tag: Control flow in the JSP page HTML tag: Used to generate an HTML tag, display data in the form, program the URL using the session ID Template tag: Use dynamic template to construct a page of normal format (About their detailed introduction)