请教Struts2 Action类中Session值传递问题
1个回答
展开全部
1.实现ServletContextAware, ServletRequestAware, ServletResponseAware 接口
例:
@Override
public void setServletResponse(HttpServletResponse response) {
this.response = response;
}
@Override
public void setServletRequest(HttpServletRequest request) {
this.request = request;
this.session = request.getSession();
}
@Override
public void setServletContext(ServletContext application) {
this.application = application;
}
2.通过ActionContext获取Map类型的ServletAPI (与上面的类型不同,但数据相同)
ActionContext context = ActionContext.getContext();
Map<String,Object> session=context.getSession();
Map<String,Object> application=context.getApplication();
3.使用ActionContext直接保存数据:
ActionContext context = ActionContext.getContext();
context.put("key", "value");
在JSP中访问上面保存下数据的方法
key为键值:
HttpServletRequest request中保存的数据
Property 具有getter方法的成员
ActionContext ActionContext中直接保存的数据
HttpServletSession session中保存的数据
ServletContext application中保存的数据
按优先级(搜索key的顺序)先后排列:
${key}
HttpServletRequest > Property > ActionContext > HttpServletSession > ServletContext
${sessionScope.key}
HttpServletSession
${session.key}
HttpServletSession
${application.key}
ServletContext
<s:property value="key"/>
Property > ActionContext
<s:property value="#key"/>
ActionContext
<s:property value="#request.key"/>
HttpServletRequest > Property > ActionContext
<s:property value="#session.key"/>
HttpServletSession
<s:property value="#application.key"/>
ServletContext
例:
@Override
public void setServletResponse(HttpServletResponse response) {
this.response = response;
}
@Override
public void setServletRequest(HttpServletRequest request) {
this.request = request;
this.session = request.getSession();
}
@Override
public void setServletContext(ServletContext application) {
this.application = application;
}
2.通过ActionContext获取Map类型的ServletAPI (与上面的类型不同,但数据相同)
ActionContext context = ActionContext.getContext();
Map<String,Object> session=context.getSession();
Map<String,Object> application=context.getApplication();
3.使用ActionContext直接保存数据:
ActionContext context = ActionContext.getContext();
context.put("key", "value");
在JSP中访问上面保存下数据的方法
key为键值:
HttpServletRequest request中保存的数据
Property 具有getter方法的成员
ActionContext ActionContext中直接保存的数据
HttpServletSession session中保存的数据
ServletContext application中保存的数据
按优先级(搜索key的顺序)先后排列:
${key}
HttpServletRequest > Property > ActionContext > HttpServletSession > ServletContext
${sessionScope.key}
HttpServletSession
${session.key}
HttpServletSession
${application.key}
ServletContext
<s:property value="key"/>
Property > ActionContext
<s:property value="#key"/>
ActionContext
<s:property value="#request.key"/>
HttpServletRequest > Property > ActionContext
<s:property value="#session.key"/>
HttpServletSession
<s:property value="#application.key"/>
ServletContext
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询