码迷,mamicode.com
首页 > Web开发 > 详细

struts2访问web资源

时间:2017-08-02 20:00:04      阅读:190      评论:0      收藏:0      [点我收藏+]

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

通过ActionContext访问

public class TestActionContextAction {

    public String execute(){
        
        //获取 ActionContext 对象
        ActionContext actionContext = ActionContext.getContext();
        
        //application
        Map<String, Object> applicationMap = actionContext.getApplication();
        
        applicationMap.put("applicationKey", "applicationValue");
        
        //获取属性
        Object date = applicationMap.get("date");
        System.out.println("date: " + date);
        
        //2. session
        Map<String, Object> sessionMap = actionContext.getSession();
        sessionMap.put("sessionKey", "sessionValue");
        
        
        //清空session
        /*if(sessionMap instanceof SessionMap){
            SessionMap sm = (SessionMap) sessionMap;
            sm.invalidate();
            System.out.println("session 失效了. ");
        }*/
        
        //request
        Map<String, Object> requestMap = (Map<String, Object>) actionContext.get("request");
        requestMap.put("requestKey", "requestValue");
        
        //parameter
        Map<String, Parameter> 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<String, Object> application;
    
    @Override
    public void setApplication(Map<String, Object> application) {
        this.application = application;
    }
    private Map<String, String[]> parameters;
    @Override
    public void setParameters(Map<String, String[]> parameters) {
        this.parameters = parameters;
    }

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

    private Map<String, Object> session;
    @Override
    public void setSession(Map<String, Object> 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.jsp</result>    
 </action>
        
 <action name="TestAware" class="com.TestAwareAction">
      <result>/aware.jsp</result>    
 </action>

 

struts2访问web资源

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

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!