"Javaweb" "Notes" "Javaweb Introductory Classics" 15th Chapter struts Framework __java

Source: Internet
Author: User
Tags java web
This article is the author studies "Javaweb Introduction Classics" a book to tidy up the reading notes, only for the study and the reference. If you have related copyright issues, please contact the author in time
Overview of Struts 2 FrameworkThe Struts 2 framework originates from the WebWork framework and is an MVC framework. 1, MVC principle model, view, controller 2, struts 2 of the generation of Struts 2 framework is on the basis of webwork development, so is webwork technology and Struts technology combination. WebWork is a very good open source web framework for open source organization Opensymphony. 3, STRUTS2 structure system Struts2 intercepts the request to process through the filter, when the client sends an HTTP request, needs to pass through a filter chain. This filter chain includes Actioncontextclearup filters, other Web application filters, and Strutsprepareandexecutefilter filters,      Where the strutsprepareandexecutefilter must be configured. When the Strutsprepareandexecutefilter filter is invoked, the action Mapper looks for the action object that needs to be invoked and returns the proxy for that object. The action agent then reads the associated configuration of Struts 2 from the Configuration Manager. (Struts.xml). The action container invokes the specified Action object, which requires a series of interceptors that pass through the Struts2 before being invoked.      Interceptors are similar in principle to filters. When the action processes the request, it returns the view results (JSP and Freemarker, etc.) of the response, where you can use the Struts tab to display the data and control the data logic. The HTTP request then responds to the browser, which also passes through the filter chain in the filtering of the response.
Ii. Introduction to Struts 21, get and configure the development of Struts 2 project needs to add the class library
Name Description
Struts2-core-2.3.4.jar The core class library of Struts 2
Xwork-core-2.3.4.jar Core Class Library of Xwork
Ongl-3.0.4.jar OGNL Expression Language class library
Freemarkder-2.3.19.jar Freemarker Template Language Support class library
Commons-io-2.0.1.jar Tool class library for processing IO operations
Commons-fileupload-1.2.2.jar File Upload support class library
Javassist-3.11.0ga.jar Class library for parsing, editing, and creating Java bytecode
Asm-commons-3.3.jar Asm-3.3.jar ASM is a Java bytecode processing framework that can be used to dynamically generate stub classes and proxy classes that dynamically modify the contents of a class before the Java virtual machine loads a class
Commons-lang3-3.1.jar Contains a number of data type tool classes, which are java.lang.* extensions

2. Create the first Struts 2 program the Struts 2 framework inherits the struts into the Web application primarily through a filter object, which org.apache.Struts 2. Dispatcher.ng.filter.StrutsPrepareAndExecuteFilter. It Struts2 the HTTP request in the Web application and forwards the HTTP request to the specified action handler, which returns the page to the client response based on the processing results. Therefore, in the STRUTS2 framework, the filter strutsprepareandexecutefilter is the portal between Web applications and struts 2API, which plays an important role in the application of Struts 2. (1) Create the project and add the Struts 2 class library file to the Lib folder in the Web-inf directory. (2) Declare the filter provided by Struts 2 in the Web.xml file, the class name is Org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExcecuteFilter (3) Create a configuration file in the source folder of the Web project, where you define the action object (4) in Struts 2 to create a master page where you can write a hyperlink to access the Action object defined above. (5) Create the page as the action object to process the successful return page.
third, the action object 1. Understanding the Action object      action object is an important object in the STRUTS2 framework, primarily for processing HTTP requests. In the Struts2 API, the Action object is an interface, located in the COM.OPENSYMPHONY.XWORK2 package. Normally, when we write the Struts 2 project, Creating an Action object directly or indirectly implements the Com.opensymphony.xwork2.Action interface, in which a static constant of 5 string types is defined in addition to defining the Execute () method.       Key code for this interface:         public interface action{        &NBS P      public static final String success= "SUCCESS";                public static final String none= "NONE";                public static final String error= "ERROR";                public static final String input= "INPUT";                public static final String login= "LOGIN";                public String execute () throws Exception;          }       in the action interface, it contains 5 static constants, which are STruts 2API defines static constants for processing results. (1) SUCCESS       on behalf of the action to perform a successful return value, if the action execution is successful, you need to return the Success page, you can set the return value of SUCCESS (2) NONE       Returns a successful return value on behalf of the action, but does not need to be returned to the Success page, primarily for business logic that does not need to return the results page (3) ERROR       represents the return value of the action execution failure, This value can be returned by the action when some information validation fails (4) input       represents the return value of an input information page (5) LOGIN       Static variable login represents a return value that requires a user login
2. The principle of injection of request parameters in the Struts 2 framework, the data submitted by the form is automatically injected into the corresponding property in the Action object, which is the same as the IOC injection principle in the spring framework, and is injected by the action object to provide a setter method for the property.      The action object that needs to inject the property value must provide a setter () method for the property, because the internal implementation of Struts 2 is to automatically inject values into the property according to the setter method provided in the JavaBean specification. * Because the properties of the action object in Struts 2 are injected through its setter method, you need to provide a setter method for the property. However, the getter method is needed to get the value of this property, so it is best to provide a setter and getter method for the properties of the action object when writing code.
3. The basic process of action the STRUTS2 framework intercepts HTTP requests primarily through STRUTS2 filter objects, and then assigns the request to the specified action processing. Because the STRUTS2 filter is configured in the Web project, when the browser sends an HTTP request to the Web container, the Web container calls the Dofilter () method of the Struts2 filter. At this point Struts2 receives an HTTP request, and the internal processing mechanism of the STRUTS2 determines whether the request matches an action object. If a matching action is found, the Execute () method of the object is invoked and the value of the response is returned based on the processing result.      Then Struts2 the return value of the action to find the page mapped by the returned value, and finally responds to the browser through a certain view. * In the STRUTS2 framework, the return view of a "*.action" request is determined by the action object. The discovery method is to determine the returned view by looking for a configuration item that corresponds to the returned string, such as the string returned by the Execute () method in the action is success, so Struts2 will look for a configuration item named success in the configuration file. and returns the corresponding view for this configuration item.
4, the dynamic Action STRUTS2 Framework provides the concept of dynamic action, known as the active action.      Implement the processing of a business logic by dynamically requesting the method in the Action object. Dynamic action is implemented dynamically by requesting a specific method in the action object, which is implemented dynamically by requesting a specific method in the Action object. The action is done by matching the request string (method name) with the method in the action object at the request of the URL address of the action, noting the action address and the request string. Number separation.
5. Apply dynamic Action (1) to create a Java Web project, add the Struts2 support class library file to the Lib folder in the Web-inf directory, and then register the STRUTS2 provided filter web.xml the file. (2) Create the Action object and write the Add () and update () methods separately to handle the request to add the user information and update the user information and return the request to the appropriate page (3) Create a configuration file named Struts.xml in the source folder of the Web project (the default is src directory in eclipse). Configure Useraction (4) to create a JSP page named user_add.jsp as a return page to successfully add user information. (5) Create a JSP page as a successful return page. (6) Create the first page in the program, add two hyperlinks to it, and use the dynamic Action feature provided by STRUTS2 to point the two hyperlink requests to the class's add to update request respectively. * Use the URL address of the action request when using the dynamic action of Struts2. The number separates the action request from the request string, and the name of the request string needs to correspond to the method name in the action class, or else throw the Java.lang.NoSuchMethodException
Four, Struts 2 of the configuration file1, Struts 2 configuration file type
Name Description
Struts-default.xml In the ORG.APACHE.STRUTS2 package in the Struts 2-core-2.3.4.jar file
Struts-plugin.xml In a package that is located in each plug-in provided by STRUTS2
Struts.xml Web application default Struts2 configuration file
Struts.properties Struts2 in the framework of the property configuration file
Xml This file is a Web.xml file in the Web application, where you can also set some information about the STRUTS2 framework
Where the Struts-default.xml and Struts-plugin.xml files are Struts2 provided, they are in the package provided by the STRUTS2, and the web applies the default Struts.xml profile when the file is Struts2; struts. The properties file is a property profile in the Struts framework, and the latter two configuration files require a developer to write
2, Configuration Struts 2 package in the Struts.xml file there is a package concept, similar to the Java package. The packages in the profile struts.xml use the <package> element declaration, which is primarily used to prevent related configurations in some projects and can be understood as a logical unit in a configuration file. Packages that have already been configured can be inherited by other packages, thereby increasing the reusability of the configuration file. <struts> <package name= "extends=" "> </package> </struts>Package using the <package> element declaration, you must have a Name property to specify the name <package> element contained properties of the package
Property Description
Name To declare the name of the package, you must
Extends To declare an inherited package, a parent package
Namespace Specifies the namespace, which is the path to prevent the action under this package from being accessed
Abstract Declaring a package as an abstract type


3. Configuration namespace Struts 2 provides a namespace-specific feature that specifies the access path to an action object by using the namespace attribute in the package declaration of the configuration file Struts.xml. <package name= "extends=" "namespace=" > * Specifies a namespace attribute in the <package> element, and the value of the namespace needs to start with "/", otherwise the access address of the action object cannot be found.
Create a folder in the WebContent node that is the same as the namespace, and add the namespace/folder name before the page when you access the page.
4, action-related configuration Struts2 the action object in the framework is the role of a controller, STRUTS2 framework handles HTTP requests, and its request address mapping requires the use of the <action> element configuration configuration in Struts.xml files The <action> element in the component is primarily used to create a mapping of the Action object, which allows you to specify the action request address and the mapped page after processing. <action> element Common Properties
Property Description
Name Used to configure the URL mappings that the action object is requested to
Class Specifies the class name of the Action object
Method Sets which method of the object is invoked when the action object is requested
Converter Class that specifies the Action object type converter
Invokes an action object, which executes the execute () method by default. If you need to request a specified method in the action object of a multiple-business logic branch, you can configure it by using the <action> element's methods property. To give a request to a specified business logic method to handle the <action> element's methods property is primarily used to distribute a specified business logic method for an action request, such as set to add, so the request is processed by the Add () method of the Action object. This method of compounding can reduce the number of action objects. The method property value of the *<action> element must be the same as the name of the methods in the action object, because the STRUTS2 framework looks for a method that matches it by means of a property value.
5. Use wildcard characters to simplify configuration. "*": Match 0 or more characters "\": an escape character, if you want to match "/", then use "\/" match
6, the configuration returns the result action object is the request processing object in the STRUTS2 framework, and returns a string for different business requests and processing results, that is, the logical view name of the action processing result.      The STRUTS2 framework finds its matching view in the configuration file Struts.xml based on the logical view name and responds to the browser after it is found. The result mapping in the profile Struts.xml file uses the <result> element <action name= "class=" "> <result> XXX.JSP&L t;/result> <result name= "error" > yyy.jsp</result> <result name= "Input" > The two properties of the zzz.jsp</result> </action> <result> elements are name and type, where the Name property is used to specify the logical people's name for result, and the method in the Action object To the corresponding return value. The Type property is used to set types of return results, such as request forwarding and redirection. * The default value of the <result> element without the Name property is success
Five, Struts2 tag libraryIn order to use STRUTS2 Tag Library in JSP, we should first make the introduction of tag library. <% @taglib prefix= "s" url= "/struts-tags"%>1, the application of data labels (1) Property label function is to get the data value and directly output to the page properties
Name Description
Default Optional
Escape Optional
Escapejavascript Optional
Value Optional

(2) The set label is used to define a variable and assign it a value, while setting the scope of the variable. By default, variables defined through the set label are placed in the value stack. Properties of the Set label
Name Whether you must Type Description
Scope Optional String Sets the scope of the variable. Application, request, session, page, or action (default)
Value Optional String Variable Value
Var Optional String Variable name
* Also contains the ID and name attribute in the set label, which is obsolete in struts2.
(3) A tag is used to build a hyperlink, the final build effect forms an HTML hyperlink a label Common properties
Name Whether you must Type Description
Action Optional String Point the address of a hyperlink to the action
Href Optional String Hyperlink Address
Id Optional String Set the name of a property in HTML
Method Optional String If the address of the hyperlink points to Action,method, you can also call the method for the action declaration
Namespace Optional String If the address of the hyperlink points to Action,namespace, you can declare the namespace for the action

(4) The Param label is used to assign a value to a parameter and can be a child label for another label.
Name Whether you must Type Description
Name Optional String Set parameter names
Value Optional Object Set parameter values

(5) The action label is used to perform an action request. When an action request is executed on a JSP page through the action tag, it can be output to the current page or not to the output.
Name Whether you must Type Description
Executeresult Optional Boolean Do you want the action to return execution results, default false
Flush Optional Boolean Output is refreshed, default True
Ignorecontextparams Optional Boolean Whether to pass the page request parameter to the invoked action, the default value is False
Name Have to String The name of the Action object map, which is the name configured in Struts.xml
Namespace Optional String Specify the name of the namespace
Var Optional String Name that refers to this action

(6) The push label is used to push the object or value into the value stack and prevent the top, because the object in the value stack can be called directly, so the main role of the label is to simplify the operation. The push Label property has only value and is used to declare objects in the stack of indentation values <s:push value= "" ></s:push>(7) The date label date tag is used to format datetime, which can be formatted with the specified formatting style
Name Whether you must Type Description
Format Optional String Set the style of a formatted date
Name Have to String Date value
Nice

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.