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 ";
}
}