struts2系列4:Struts2 Action(三) 访问web元素的四种方式

1: Map IoC  方式
package  com.action;

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 依赖容器 方式:
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 依赖容器  方式:

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

你可能感兴趣的:(struts2系列4:Struts2 Action(三) 访问web元素的四种方式)