Struts2与Servlet实现耦合的方法
1、使用 ActionContext
ActionContext.getContext().getSession().setAttribute(key,value)
或
ActionContext.getContext().put(key,value)
HttpServletRequest setAttribute getAttribute getParameters
对应
ActionContext set put getParameterMap
2、通过实现以下接口
ServletContextAware
org.apache.struts2.util.ServletContextAware(接口)
ServletRequestAware(接口)中的setRequest方法
ServletResponseAware(接口)中的setResponse方法
3、通过类 ServletActionContext
其中的方法都是static方法
例: getRequest(), getResponse(), getActionContext()
首选ActionContext 单元测试最方便
其次ServletActionContext
最后ServletRequestAware、ServletResponseAware、ServletContextAware
本文介绍了Struts2框架与Servlet技术如何实现耦合的多种方式,包括使用ActionContext进行属性设置与获取,通过实现特定接口如ServletContextAware、ServletRequestAware及ServletResponseAware来直接操作Servlet API,以及利用ServletActionContext类提供的静态方法。这些方法便于开发者在不同场景下灵活选择。

369

被折叠的 条评论
为什么被折叠?



