Struts2:action, Interceptor inherited base classes and how to get scopes

Source: Internet
Author: User
First, Struts2
1, Action:
1, implement STRUTS2 action class function, need to inherit Actionsupport class (Actionsupport implement the Action interface), you can rewrite the Excute () method, the actual development of the general need not
2, the way to get scope in action: There are three kinds
A, mode one: the Pure servlet API
HttpServletRequest request=servletactioncontext.getrequest ();
HttpServletResponse Response=servletactioncontext.getresponse ();
Request.setattribute ("Name", This.getusername ());
HttpSession session=request.getsession ();
ServletContext Application=session.getservletcontext ();
B, mode two: Through the action context Actioncontext
GET Request Session Application
Actioncontext Request=actioncontext.getcontext ();
Request.put ("Req_key", "Req_value");

Map<string,object> session=request.getsession ();
Session.put ("Ses_key", "Ses_value");

Map<string,object> application=request.getapplication ();
Application.put ("App_key", "App_value");
C, mode three: through the interface implementation, rewrite the inside method, location Struts API package is: Org. Apache. Struts2. Interceptor
Servletrequestaware
Servletresponseaware
Sessionaware
Applicationaware
For example:
Implements the following three interfaces, declares the corresponding scope object, generates the corresponding set method, and injects the corresponding scope parameter into the currently declared object when the Struts2 is loaded
Publicclass Testaction extends Actionsupport implements, Sessionaware Servletrequestaware, Servletresponseaware {
Private MAP session;
private HttpServletRequest request;
Private HttpServletResponse response;
Overrides the method in the interface, providing only the set method, and does not require a GET method to customize the build
Publicvoid setsession (Map att) {
This.session = att;
}

Publicvoid setservletrequest (HttpServletRequest request) {
This.request = Request;
}

Publicvoid Setservletresponse (httpservletresponse response) {
This.response = response;
}
2. Interceptor: Interceptor
1, implement STRUTS2 Interceptor class function, need to inherit Abstractinterceptor class, and rewrite intercept (actioninvocation invocation) method
2), access to the scope of the method: through the Interceptor method inside the parameters Actioninvocation invocation obtained (note: Interceptors in any parameter is obtained through invocation, other methods are illegal)
GET Request Session Application
Actioncontext Request=invocation.getinvocationcontext (). GetContext ();
Request.put ("Req_key", "Req_value");

Map<string,object> session=request.getsession ();
Session.put ("Ses_key", "Ses_value");

Map<string,object> application=request.getapplication ();
Application.put ("App_key", "App_value");
3), here is the difference between the Invoke () method and the Invokeactiononly () method:
Invocation.invoke () Method: The process of requesting a response: Action-----Result-----Interceptor
Invocation.invokeactiononly () Method: The process of requesting a response: Action-----Interceptor-----Result

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.