Struts2-整理笔记(三)结果处理跳转、获得servletAPI原生

时间:2023-03-09 02:42:13
Struts2-整理笔记(三)结果处理跳转、获得servletAPI原生

在struts2中有很多种跳转方式如下是在struts-default.xml截取的一段源码,常用的跳转有 转发:dispatcher、重定向:redirect、转发到Action:chain、重定向到Action:redirectAction

  <package name="struts-default" abstract="true">
<result-types>
<result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
<result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
<result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>
<result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>
<result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
<result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
<result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
<result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>
<result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>
<result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />
<result-type name="postback" class="org.apache.struts2.dispatcher.PostbackResult" />
</result-types>
</package>
ActionContest: 数据中心
有原来一切 servlet 年代的东西
原生 request HttpServletRequest
源生 response HttpServletResponse
原生 ServletContext ServletContext
request域 Map 命短
session域 Map
appliction域 Map 命长
param参数 Map
attr域 Map 如果重复以最小的键值对为准 3个域合一
ValueStack 值栈
ActionContext :数据中心其实也是一个 Map
ActionContext域是什么?
答:比如问一个木桶有多少水 要看最短的木板,所以
域最小的是request 就是一次请求
ActionContext生命周期:每次请求都会创建一个请求对应
的ActionContext对象。请求处理完ActionContext销毁,他
不会影响别的域的声明周期
如何获得ActionContext。struts2设计的是,将ActionContext对象
创建好之后,将ActionContext与当前线程绑定。我们要获得ActionContext
只需要从ThreadLocal中获得即可。
第一种方式
通过ActionContext
 public class DemoAction extends ActionSupport {

     @Override
public String execute() throws Exception {
// request域->map(struts2并不推荐使用request原生)
// 不推荐
Map<String, Object> requestScope = (Map<String, Object>) ActionContext.getContext().get("request");
// 推荐
ActionContext.getContext().put("name", "requestTom");
// session域->map
Map<String, Object> sessionScope = ActionContext.getContext().getSession();
sessionScope.put("name", "sessionTom");
// application域->map
Map<String, Object> applicationScope = ActionContext.getContext().getApplication();
applicationScope.put("name", "applicationTom");
return SUCCESS;
}
}

第二种方式

通过ServletActionContext

 public class Demo2Action extends ActionSupport {
// 并不推荐
@Override
public String execute() throws Exception { // 原生request
HttpServletRequest request = ServletActionContext.getRequest();
// 原生session
HttpSession session = request.getSession();
// 原生response
ServletActionContext.getResponse();
// 原生servletContext
ServletActionContext.getServletContext(); return super.execute();
} }

第三种方式

通过实现ServletRequestAware

public class Demo3Action extends ActionSupport implements ServletRequestAware {
private HttpServletRequest request; // 并不推荐
@Override
public String execute() throws Exception { System.out.println("原生request" + request);
request.setAttribute("name", "haha");
return super.execute();
} @Override
public void setServletRequest(HttpServletRequest arg0) {
this.request = arg0;
} }

第四种方式(推荐)

通过实现接口获得Map类型元素
  接口RequestAware
  接口SessionAware
  接口ApplicationAware
 public class ScopeAction extends ActionSupport implements SessionAware{
Map<String,Object> session; @Override
public void setSession(Map<String, Object> arg0){
session = arg0;
} @Override
public String execute() throws Exception{
session.put("abc","haha");
System.out.println(session.get("abc"));
return SUCCESS;
}
}