struct2访问或添加request/session/application

时间:2021-10-08 22:33:12

访问或添加request/session/application
1 通过ActionContext
//这样放置
public String execute()
 {
    ActionContext ctx = ActionContext.getContext();
    ctx.getApplication().put("app","lll");
    ctx.getSession().put("ses","session范围");
    ctx.put("req", "request范围");
    return "success";
 }
//这样取出
 <body> 
   ${applicationScope.app}<br>
   ${sessionScope.ses}<br>
   ${requestScope.req}<br>
 </body>

2 获取相应对象

2.1 通过ServletActionContext类直接获取:
public String rsa() throws Exception{
 HttpServletRequest request = SerletActionContext.getRequest();
 request.setAttribute("req","请求范围属性");
 request.getSession().setAttribute("res","会话属性");
}

2.2实现指定接口,由struct2框架运行时注入
public class HelloWorldAction implements ServletRequestAware,ServletResponseAware,ServletContextAware
{
  private HttpServletRequest request;
  public void setServletRequest(HttpServletRequest req){
     this.request = res;
  }
}