Action直接访问Servlet API
1.ActionContext
import com.opensymphony.xwork2.ActionContext; ... ActionContext ctx = ActionContext.getContext(); ... //操作session ctx.getSession().put("key:xxx", "value:xxx"); //操作application ctx.getApplication().put("key:xxx", "value:xxx");
2.一些重要接口
虽然Struts2提供了ActionContext来操作ServletAPI,但是这种访问
毕竟不能获得ServletAPI实例。
Action如果实现了以下接口,便可访问Servlet对象。
ServletContextAware-ServletContext
ServletRequestAware-HttpServletRequest
ServletResponseAware-HttpServletResponse
eg:
import javax.servlet.http.HttpServletRequest; import org.apache.struts2.interceptor.ServletRequestAware; ... public class SharpAction implements ServletRequestAware { ... //给Action配上需要的servlet类属性 private HttpServletRequest request; //这个是接口的方法,必须要实现 public void setServletRequest(HttpServletRequest request) { this.request = request; } public String execute() throws Exception { ... request.setAttribute("key:xxx", "value:xxx"); ... }
相关推荐
JudeJoo 2020-08-21
meleto 2020-08-15
lantingyue 2020-07-30
zyjj 2020-07-27
pengpengflyjhp 2020-07-19
htofly 2020-07-09
三动 2020-06-21
hyxinyu 2020-06-08
洗尽铅华 2020-06-07
shayuchaor 2020-06-07
xiamubawei 2020-06-04
方志朋 2020-05-30
一片荷叶 2020-05-27
spring艳 2020-05-16
zhangdy0 2020-05-06
80304053 2020-05-04
neweastsun 2020-05-04
shuiluobu 2020-05-01
itjavashuai 2020-04-26