Four Methods for struts2 action to access web elements

Source: Internet
Author: User
1: Package com. Action in map IOC mode;

Import java. util. Map;

Import org. Apache. struts2.interceptor. applicationaware;
Import org. Apache. struts2.interceptor. requestaware;
Import org. Apache. struts2.interceptor. sessionaware;

Import com. opensymphony. xwork2.actionsupport;

Public class useraction1 extends actionsupport implements requestaware, sessionaware, applicationaware {
Private Map <string, Object> request;
Private Map <string, Object> session;
Private Map <string, Object> application;
Public String add (){
Request. Put ("R1", "R1 ");
Session. Put ("S1", "S1 ");
Application. Put ("A1", "A1 ");
Return "success ";
}
Public void setrequest (Map <string, Object> request ){
This. Request = request;
}
Public void setsession (Map <string, Object> session ){
This. Session = session;
}
Public void setapplication (Map <string, Object> application ){
This. Application = application;
}
}

2: Map depends on the container mode: Package com. Action;

Import java. util. Map;
Import com. opensymphony. xwork2.actioncontext;
Import com. opensymphony. xwork2.actionsupport;

Public class useraction2 extends actionsupport {
Private Map <string, Object> request;
Private Map <string, Object> session;
Private Map <string, Object> application;
Public useraction2 (){
Actioncontext context = actioncontext. getcontext ();
Request = (Map <string, Object>) Context. Get ("request ");
Session = context. getsession ();
Application = context. getapplication ();
}
Public String add (){
Request. Put ("R2", "R2 ");
Session. Put ("S2", "S2 ");
Application. Put ("A2", "A2 ");
Return "success ";
}
}

3: servlet IOC: Package com. Action;

Import javax. servlet. servletcontext;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpsession;
Import org. Apache. struts2.interceptor. servletrequestaware;
Import com. opensymphony. xwork2.actionsupport;

Public class useraction3 extends actionsupport implements servletrequestaware {
Private httpservletrequest request;
Private httpsession session;
Private servletcontext application;
Public String add (){
Request. setattribute ("R3", "R3 ");
Session. setattribute ("S3", "S3 ");
Application. setattribute ("A3", "A3 ");
Return "success ";
}
Public void setservletrequest (httpservletrequest request ){
This. Request = request;
This. Session = request. getsession ();
This. Application = session. getservletcontext ();
}
}

4: servlet dependent container mode:

Package com. Action;

Import javax. servlet. servletcontext;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpsession;
Import org. Apache. struts2.servletactioncontext;
Import com. opensymphony. xwork2.actionsupport;

Public class useraction4 extends actionsupport {
Private httpservletrequest request;
Private httpsession session;
Private servletcontext application;
Public useraction4 (){
Request = servletactioncontext. getrequest ();
Session = request. getsession ();
Application = session. getservletcontext ();
}
Public String add (){
Request. setattribute ("r4", "r4 ");
Session. setattribute ("S4", "S4 ");
Application. setattribute ("A4", "A4 ");
Return "success ";
}
}

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.