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;
}
}
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;
}
}
上一篇: jquery.extend函数详解
下一篇: PHP语言标记、指令分隔符、注释讲解