Struts2 basics-request and response, obtain web resources, struts2web
I. Request and response
Action
1. Meaning:
(1) The action Element in struts. xml also refers to the action attribute of the from form. In short, it represents a struts2 request.
(2) Action class used to process Struts2 requests
2. Action class
(1) Use the setXxx () method and the getXxx () method to define attributes, use the setXxx () attribute method to accept the request parameter values, and use the getXxx () method to display data on the page.
(2) Whether there is a parameter Constructor
(3) There must be at least one method for Struts2 to call when executing this action.
(4) The same Action can contain multiple action methods.
(5) The Action class is not a singleton. Struts2 creates an Action instance for each request.
3. ActionSupport class
(1) ActionSupport is the default Action class.
(2) interfaces implemented by ActionSupport
Action: Provides string constants such as SUCCESS and INPUT, and provides the execute () abstract method for implementation.
Validateable: used for programmatic input verification
ValidationAware: used to obtain and Display error messages. There are two levels of error messages: class level and field level.
TextProvider: Read attribute values from resource files for internationalization
LocaleProvider: gets the Locale object for internationalization.
Serializable: Serializable Action class
4. Request Extension
(1) The suffix of the ServletPath part in the Request Path, for example:. action/. do
(2) Struts2 identifies which requests need to be processed by Struts2 Based on the extension and which do not need to be processed.
(3) default supported extensions:. action and none
5. default. properties
Many constants are defined in default. properties, such as the request extension supported by default.
Modify the constant defined by default. properties:
Add the following elements to the struts root tag:
<Constant name = "" value = ""/>
For example, modify the default supported request extension:
<Constant name = "struts. action. extension" value = ". action,. do"/>
Result:
1. Meaning:
(1) represents the response of the Struts2 request. Each action tag can contain multiple result elements.
(2) result Tag: name attribute: Return Value of the String type method corresponding to the action method, type attribute: execution result type, and how to redirect.
2. Result type:
(1) Struts2 defines 10 result types in struts-default.xml
<Result-types> <result-type name = "chain" class = "com. opensymphony. xwork2.ActionChainResult "/> <result-type name =" dispatcher "class =" org. apache. struts2.dispatcher. servletDispatcherResult "default =" true "/> <result-type name =" freemarker "class =" org. apache. struts2.views. freemarker. freemarkerResult "/> <result-type name =" httpheader "class =" org. apache. struts2.dispatcher. httpHeaderResult "/> <result-type name =" redirect "class =" org. apache. struts2.dispatcher. servletRedirectResult "/> <result-type name =" redirectAction "class =" org. apache. struts2.dispatcher. servletActionRedirectResult "/> <result-type name =" stream "class =" org. apache. struts2.dispatcher. streamResult "/> <result-type name =" velocity "class =" org. apache. struts2.dispatcher. velocityResult "/> <result-type name =" xslt "class =" org. apache. struts2.views. xslt. extends tresult "/> <result-type name =" plainText "class =" org. apache. struts2.dispatcher. plainTextResult "/> </result-types>Result types
There are five common types: chain, dispatcher, redirect, redirectAction, stream
(2) Explanation of common result types
Dispatcher: the default result type of struts2. it is forwarded to the target resource but cannot be an Action because struts2 cannot intercept the content during internal forwarding.
Chain: forward to an Action, such:
<Action name = "hello" class = "com. nucsoft. struts2.helloworld. helloWorld "method =" secondMethod "> <result type =" chain "> <param name =" actionName "> worldhello </param> <param name =" namespace ">/</ param> </result> </action>Chain
Redirect: redirects to the target resource.
<Result name = "redirect" type = "redirect">/result. jsp </result>Redirect
The Virtual Path of the target resource cannot contain the virtual path of the web application. The target resource can also be an Action.
RedirectAction: redirect to an Action
<Action name = "helloworld _ *" class = "com. nucsoft. struts2.helloworld. helloWorld "method =" {1} "> <result>/success. jsp </result> <result type = "redirectAction" name = "redirectAction"> <param name = "actionName"> redirect2Action </param> </result> </action> <action name = "redirect2Action" class = "com. nucsoft. struts2.helloworld. helloWorld "method =" redirectAction "> <result>/success. jsp </result> </action>RedirectAction
Stream:
Returns response results in the form of output streams for file download and Ajax
Wildcard ing rules:
1. Exact match takes precedence.
2. If the exact match is not prioritized, the statement is valid first (if a request can match multiple actionnames with wildcards, the statement is valid first ).
Dynamic method call:
Dynamically call the method in Action in the URL.
By default, Struts2 prohibits dynamic method calls and can be modified by modifying constants.
Normal access: http: // localhost: 8989/Web application virtual path/dynamicAction. action
Dynamic method call: http: // localhost: 8989/Web application virtual path/dynamicAction! DynamicMethod. action
Ii. web Resources
Struts2 encapsulates common data into a series of Map Objects: RequestMap HashMap SessionMap ApplicationMap
Obtaining method:
1. decoupling from Servlet
(1) using the ActionContext class, the appMap, sessionMap, and paramMap objects can be obtained through the ActionContext object.
(2) Implement the XxxAware interface, RequestAware, ParameterAware, SessionAware, and ApplicationAware.
2. Coupling with Servlet
(1) Use the ServletActionContext class to obtain HttpServletRequest, HttpServletResponse, and ServletContext
(2) Implement the ServletXxxAware interface: ServletRequestAware, ServletResponseAware, ServletContextAware