欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

struct2访问或添加request/session/application

程序员文章站 2022-03-26 11:02:11
访问或添加request/session/application 1 通过ActionContext //这样放置 public String execute() { A...
访问或添加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";
}
//这样取出

${applicationScope.app}

${sessionScope.ses}

${requestScope.req}




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