Reference book: "Java EE open source programming Essentials 15"
Struts is an open-source Java web framework that implements MVC design patterns well. Through a configuration file, the application components of the various layers are linked, so that the components are less connected at the program level and the coupling degree is low. It defines its own tag library to implement is the view layer, creating an intrinsic servlet, called Actionservlet, as its own controller, to create an object called the action class to implement the business logic. The relationship between the view component and the business logic component is declared in the Struts-config.xml configuration file. When the struts application starts, it first reads the information from the Struts-config.xml file. According to the information, the controller Actionservlet know to forward the request in the view to the corresponding business logic component processing, there is no code connection between the components.
Struts working principle diagram:
Core components
Controller components
Accept input data from the client, invoke business logic operations, decide which view to return to the customer, including: Actionservlet, Action, actionmapping, and Actionforward
1) Actionservlet class
The HTTP message package is routed to the appropriate processor in the framework, non-abstract classes, which can be used directly as a specific controller and must be configured in detail in the configuration file
2) Action Class
The expansion of the controller component, which connects user actions to business operations, makes the customer request and the business model loosely coupled. When the request is received, call the Execute () method
3) configuration file Struts-config.xml
The hub of the entire struts application, declaring components and their relationships, mainly including <form-beans>, <action-mappings>, for Actionform, action declarations,
Such as:
(Detailed configuration file) http://blog.csdn.net/zzs0829/article/details/2695410
4) actionmapping
The action map (actionmapping) encapsulates the configuration information contained in the Action-mappings element in the configuration file, and the Actionmapping object includes a path property to be used with the foreign
Request URI Match
5) Actionforward
Represents a target where the controller is transferred to control after the action has been completed. Instead of specifying the actual JSP page, the forwarding map of an action is declared in a declarative way with the JSP page
And then use that Actionforward object throughout the application. The Actionforward object encapsulates the information contained in the forward element in the configuration file.
View Components
JSP pages, Actionform, struts tags, Java resource packs, and more
1) actionform
Used to transfer the customer's input data between the user and the business layer, also called the form Bean. The Struts framework automatically collects input data from the request and gives it to an action pair that uses the form bean instance
To the business layer
The struts framework handles the Actionform object steps for each request:
1. Check the corresponding mapping of the action to see if any actionform are configured
2. If a actionform is configured, use the Name property in the action element to find the configuration information for the form bean
3. See if an instance of Actionform has been created
4. If an Actionform instance already exists in the appropriate scope and the type of the instance is the type required for this new request, then the instance is reused
5. Otherwise, create a new instance, stored in the appropriate scope (the Scope property setting of the action Element)
6. Call the Reset () method of the Actionform instance
7. Repeatedly processing the request parameters, if the parameter name in the Actionform instance has a corresponding setting method (setter method), it is filled with the value of the parameter
8. If the Validate property is True, call the Validate () method of the Actionform instance and return any errors that occur
A simple Actionform example:
1 Importjavax.servlet.http.HttpServletRequest;2 Importorg.apache.struts.action.ActionErrors;3 ImportOrg.apache.struts.action.ActionForm;4 Importorg.apache.struts.action.ActionMapping;5 6 Public classLoginactionformextendsActionform {7 PrivateString username;8 9 PublicString GetUserName () {Ten returnusername; One } A Public voidSetusername (String username) { - This. username=username; - } the Publicactionerrors Validate (actionmapping mapping, - HttpServletRequest Request) { - return NULL; - } + Public voidReset (actionmapping mapping, HttpServletRequest request) { - } +}
View Code
Before the controller fills in the form data into the Actionform instance, the Reset () method is called before the Actionform property is reset to the default state. When the output is inserted, the controller calls the
The Validate () method validates the input data and returns any errors to the controller. Business logic validation should be done in the business object, and the validation performed in Actionform is only a test of the presentation layer
Certificate
2) Struts Tag Library
Includes HTML tag library, bean Tag Library, logic tag library, nested tag library, template tag library, and Tiles tag library
To use these tag libraries, you need to register them in Web. xml
For example, use the HTML tag library:
<taglib>
<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
and introduce the Taglib element on the JSP page: <%@ taglib uri= "/web-inf/struts-html.tld" prefix= "html"%> and finally adding the required jar package
3) using the message resource bundle
There is a set of classes in the Java library that allow you to read a message resource from a Java class or property file. Struts also provides a similar set of classes,
Org.apache.struts.util.MessageResources
For example, log in to the application's resource bundle:
#页面标题
Title.login=login
#图像
Image.logo=image/logo.gif
How to place the title. Login is inserted into the page:
<title><bean:message key= "Title.login"/></title>
Model components
JavaBeans or EJB
Application Example One: http://flforever1213.iteye.com/blog/941053
Simple application Example two: http://kuai.xunlei.com/d/Dgv0CU6inOj7VAQA24b
Introduction to the main components of Java Web programming technology--struts