struts2访问web资源

2021-06-15 09:03

阅读:452

标签:bsp   页面   class   put   web资源   res   gen   etc   context   

通过ActionContext访问

public class TestActionContextAction {

    public String execute(){
        
        //获取 ActionContext 对象
        ActionContext actionContext = ActionContext.getContext();
        
        //application
        Map applicationMap = actionContext.getApplication();
        
        applicationMap.put("applicationKey", "applicationValue");
        
        //获取属性
        Object date = applicationMap.get("date");
        System.out.println("date: " + date);
        
        //2. session
        Map sessionMap = actionContext.getSession();
        sessionMap.put("sessionKey", "sessionValue");
        
        
        //清空session
        /*if(sessionMap instanceof SessionMap){
            SessionMap sm = (SessionMap) sessionMap;
            sm.invalidate();
            System.out.println("session 失效了. ");
        }*/
        
        //request
        Map requestMap = (Map) actionContext.get("request");
        requestMap.put("requestKey", "requestValue");
        
        //parameter
        Map parameters = actionContext.getParameters();
        System.out.println(parameters.get("name"));
        
        return "success";
    }
    
}

 

  Aware方式

public class TestAwareAction implements ApplicationAware, SessionAware, RequestAware
    ,ParameterAware{
    
    public String execute(){
        
        //1. 向 application 中加入一个属性: applicationKey2 - applicationValue2
        application.put("applicationKey", "applicationValue");
        
        //2. 从 application 中读取一个属性 date, 并打印. 
        System.out.println(application.get("date"));
        request.put("requestKey", "requestValue");
        
        session.put("sessionKey", "sessionValue");
    
        
        return "success";
    }
    
    public String save(){
        
        return null;
    }

    private Map application;
    
    @Override
    public void setApplication(Map application) {
        this.application = application;
    }
    private Map parameters;
    @Override
    public void setParameters(Map parameters) {
        this.parameters = parameters;
    }

    private Map request;
    @Override
    public void setRequest(Map request) {
        // TODO Auto-generated method stub
        this.request = request;
        
    }

    private Map session;
    @Override
    public void setSession(Map session) {
        // TODO Auto-generated method stub
        this.session = session;
        
    }
    
}

jsp页面

application : ${applicationScope.applicationKey }
    br>br>
    
    session: ${sessionScope.sessionKey }
    br>br>
    
    request: ${requestScope.requestKey }
    br>br>
    
    parameters ${parameters.name }

 

struts.xml

 action name="TestActionContext" class="com.TestActionContextAction">
      result  name="success">/actionContext.jspresult>    
 action>
        
 action name="TestAware" class="com.TestAwareAction">
      result>/aware.jspresult>    
 action>

 

struts2访问web资源

标签:bsp   页面   class   put   web资源   res   gen   etc   context   

原文地址:http://www.cnblogs.com/lusufei/p/7275563.html

上一篇:Web应用的目录结构

下一篇:CSS属性操作


评论


亲,登录后才可以留言!