*** 赋值方式
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