7.1、通过ActionContext获取
通过该方法操作的对象不是真正的HttpServletRequest、HttpServletResponse,只是ActionContext提供的一个map而已,这样的设计师降低ServletAPI的耦合度。
main.jsp
session = ${sessionScope.session }
<
br
>
application= ${applicationScope.application }
<
br
>
request = ${requestScope.request }
mainAction.java
package
com.action;
import
com.opensymphony.xwork2.ActionContext;
public
class
mainAction {
public
String execute(){
ActionContext act = ActionContext.
getContext
();
//request域中放入数据
act.put(
"request"
,
"ccccc"
);
//session域中放入数据
act.getSession().put(
"session"
,
"aaaaa"
);
//application域中放入数据
act.getApplication().put(
"application"
,
"bbbbb"
);
return
"success"
;
}
}
7.2、通过ServletActionContext获取
通过该方法获取到的各种对象就是真正的HttpServletRequest、HttpServletResponse。
main.jsp
session = ${sessionScope.session }
<
br
>
application= ${applicationScope.application }
<
br
>
request = ${requestScope.request }
mainAction.java
package
com.action;
import
javax.servlet.http.HttpServletRequest;
import
org.apache.struts2.ServletActionContext;
import
com.opensymphony.xwork2.ActionContext;
public
class
mainAction {
public
String execute(){
//获取HttpServletRequest
HttpServletRequest req = ServletActionContext.
getRequest
();
req.setAttribute(
"request"
,
"xxxxx"
);
//获取HttpSession
req.getSession().setAttribute(
"session"
,
"yyyyy"
);
//获取application
ServletActionContext.
getServletContext
().setAttribute(
"application"
,
"zzzzz"
);
return
"success"
;
}
}
7.3、通过实现特定的接口获取(理解即可)
欢迎大家的提问或纠正。