Struts installation and configuration in Tomcat and workflow
1. Preparations
Install JDK and tomcat, set the environment variables java_home, classpath, and comcat_home, and ensure that Tomcat works properly. This article introduces the installation and workflow of struts in Tomcat using the jdk1.5 and tomcat5.5.12 environments.
2. Install struts (via binary release package)
2.1 Download struts binary release package (http://struts.apache.org/downloads.html)
2.2 decompress the downloaded binary release package and extract all the files in the lib directory. copy the JAR file to the tomcat_home/Web application/web_inf/lib directory, that is, only some Web applications use the struts architecture.
If multiple files must use the struts architecture at the same time, copy all. Jar files in the lib directory under the decompressed directory to the tomcat_home/common/lib directory.
2.3 Add the Struts. jar file path under the corresponding lib directory to the classpath environment variable.
With the preceding configuration, you can use the struts architecture for project development.
You can also install the SDK by using the struts source code.
3. Composition of struts Architecture
The entire struts application is an organic whole with a clear division of labor. Under the unified control of the controller, each component is responsible for and coordinates the work, which is the specific implementation of the MVC design pattern. The MVC mode consists of three parts: model, view, and controller.
3.1 model ):The business logic layer is used to implement specific business logic. This part is generally composed of JavaBean or EJB (enterprise-level JavaBean. The model in struts is divided into two parts:
1. Internal system status
2. operations that can change the status (transaction logic)
The internal status is usually represented by a group of actinform JavaBean. Depending on the design or application complexity, these beans can be self-contained and continuous, or only obtain data (from a database) as needed ).
Large applications typically encapsulate transaction logic (Operations) within a method, which can be called by bean with state information. For example, the shopping cart bean has the information of the purchased items, and the checkout () method may be used to check the user's credit card and send the order information to the warehouse.
In small programs, operations may be embedded in the action class, which is part of the controller role in the Struts framework. This method is suitable when the logic is simple.
Note: We recommend that you separate the transaction logic (what to do) from the role played by the action class (what to do.
3.2 View ):Presentation layer, a view that interacts directly with users. This part is generally implemented through JSP pages. In addition, Struts also provides a wide range of user-defined identity libraries, which can effectively interact with the model.
3.3 Controller: the control layer is the control center of the entire application. It is used to control the entire business flow (flow control) and work collaboratively with the view. The Controller is implemented through a dedicated servlet, which is an actionservlet-type instance provided by the struts API.
3.4 Struts framework components
Struts-config.xml struts configuration file
Actionservlet Controller
Action class contains transaction logic
Actionform display module data
Actionmapping helps the Controller map requests to operations
Actionforward indicates the object to be transferred
Actionerror is used to store and recycle errors.
Struts tag library can reduce development and display layer work
3.5 main configuration files
Web. XML (tomcat_home/Web application/web_inf). Web. XML is a configuration file used by web applications to configure actionservlet and configure the homepage.
Struts-config.xml (tomcat_home/Web application/web_inf), struts-config.xml is the core of the struts architecture, used to assemble each component, is a control center.
4. Struts framework workflow ()
It may be difficult to understand the working process of struts when you are a beginner. The simplest understanding is to regard struts as a "page navigation" architecture. In struts_config.xml, each part of the program should be specified, struts organizes applications by reading the configuration file.
5. process description
First, the server filters the request after receiving the client's browser request. If the request belongs to the struts processing scope, it is automatically submitted to the struts controller for processing. Otherwise, the server responds in the traditional way.
Note: The filtering criteria are based on the servlet configuration information in Web. xml:
<Servlet>
<Servlet-Name> actionservlet </servlet-Name>
<Servlet-class> org. Apache. Struts. Action. actionservlet </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-Name> actionservlet </servlet-Name>
<URL-pattern> *. DO </url-pattern>
</Servlet-mapping>
The above information configures the Controller servlet named actionservlet, and explains that everything is. the page requests at the end of do (or other) are processed by the servlet. The class implementing this servlet is Org. apache. struts. action. actionservlet. (Component classes defined in struts APIs)
Second, the Controller servlet starts to work and reads the configuration information in struts_config.xml.
Note: The struts_config.xml configuration is as follows:
<? XML version = "1.0" encoding = "UTF-8"?>
<Struts-config>
<Form-beans>
<Form-bean name = "formbeanname" type = "package. classnameform"/>
</Form-beans>
<Global-forwards>
<Forward name = "failed" Path = "/error. jsp"/>
<Forward name = "successed" Path = "/Right. jsp"/>
</Global-forwards>
<Action-mappings>
<Action Path = "/login" type = "classmate. loginaction" name = "formbeanname" Scope = "request" input = "/login. jsp"/>
<Action Path = "/regist" Forward = "/regist. jsp"/>
</Action-mappings>
</Struts-config>
1. Use the specified Javabean to automatically receive the form data submitted by the customer, and declare that the code of the JavaBean correspondsIn struts_config.xml:
<Form-beans>
<Form-bean name = "formbeanname" type = "package. classnameform"/>
</Form-beans>
Name: bean name
Type: class that implements the bean
To implement the automatic filling function, the bean (package. classnameform class) must be an object of the org. Apache. Struts. Action. actionform class (that is, its subclass.
2. The servlet controller reads the HTTP distribution rule (that is, the correspondence between the HTTP request and the class that processes the request)
<Action-mappings>
<Action Path = "/login" type = "package. loginaction" name = "formbeanname" Scope = "request" input = "/login. jsp"/>
<Action Path = "/regist" Forward = "/regist. jsp"/>
</Action-mappings>
Note:
Path: The relative path of the Request page corresponding to this action (excluding post. Do ).
Type: Action Bean class for processing requests (full path ).
Name: name of the actionform Bean used (declared in the <form-bean> element)
Scope: the scope of actionform Bean (default: session ).
Input: Processing page for form verification errors.
Forward: Specifies the target response page (unlike the <forward> element, this attribute takes effect only when the type attribute is not specified ).
In addition, you can configure the <forward> element in <action>, that is, the target page of the reversed response after the action is processed.
The preceding Code uses the <action-mapping> sub-element <action> of the <action-mapping> element to specify the rule for the Controller (actionsevlet object) to distribute HTTP requests:
1. If the page URI is login. Do, use formbeanname to automatically receive data from the customer request form.(The attribute in the formbeanname must be the same as the attribute name in the form submitted by the user. Otherwise, the request cannot be received.) and the request is distributed to a package. loginaction type instance for processing. This instance serves as the event listener. The type must be a subclass of org. Apache. Struts. action. Action.
Note: The Controller (actionservlet) object is similar to the JavaBean object. The first runtime will automatically instantiate an object.
2. If the page URI is regist. Do, direct the response to the/regist. jsp page.
3. Functions of the Action object
The action object receives the sent HTTP request and the form object filled by the actionservlet object, and then calls its execute () method for processing. This method has four parameters:
Actionmapping mapping: maps configuration information to struts_config.xml.
Actionform form: a reference to an actionform bean filled with an actionservlet object. You can obtain form data from the object.
Httpservletrequest request: http request information from the client.
Httpservletresponse response: responds to the HTTP information of the client.
After the execution is complete, an actionforward object encapsulated on the target page is returned to the Controller actionservlet object. At this time, the mapping object calls the findforward () method to find the ing information in the <forward> element in struts_config.xml, <forward> the element is defined as follows:
<Global-forwards>
<Forward name = "name1" Path = "/page1.jsp"/>
<Forward name = "name2" Path = "/page2.jsp"/>
</Global-forwards>
Parameters in the findforward () method in the execute method correspond to the name attribute in the forward element. After finding the parameter, you will be directed to the corresponding page.
Note: <forward> can be defined in struts_config.xml in two places: one is in the <global-forwards> element, which affects the effect in the entire application and can be understood as a global variable in the programming language; in the <action> element, the scope is action. In the findforward () method, the query order is <action>? <Global_forwards>
6. Struts framework workflow Summary
Actionserlvet receives HTTP requests from customers, and uses actionform bean to automatically fill in the Form data sent by customers to the actionform bean object? According to the configuration information (struts_config.xml), the request is distributed to the corresponding action object? The action object calls the execute () method to process the request (obtain business logic and form data based on the JavaBean and actionform), and returns the actionforward object to the actionserlvet object? Based on the information of the actionforward object (target page), actionservlet once again redirects the HTTP request to the target page and sends it to the client.
The above is the installation and basic configuration of struts in Tomcat. The core components and running mechanism of struts will be detailed in future articles.
7. Related Resources
1. Struts Official Website: http://jakarta.apache.org/struts/
2. Struts User Guide http://jakarta.apache.org/struts/userGuide
3. Article --- framework save the day
Http://www.javaworld.com/jw-09-2000/jw-0929-ejbframe.html
4. Article --- building a Java Servlet Framework Using Reflection
Http://www.javaworld.com/javaworld/jw-11-1999/jw-11-servlet.html
Statement: Reprint please join: http://blog.csdn.net/CloneIQ/archive/2006/07/18/936801.aspx
Thank you for your cooperation!