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

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

 1  <package name="struts-default" abstract="true">
 2         <result-types>
 3             <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
 4             <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
 5             <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>
 6             <result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>
 7             <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
 8             <result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
 9             <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
10             <result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>
11             <result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>
12             <result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />
13             <result-type name="postback" class="org.apache.struts2.dispatcher.PostbackResult" />
14         </result-types>
15 </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
 1 public class DemoAction extends ActionSupport {
 2 
 3     @Override
 4     public String execute() throws Exception {
 5         // request域->map(struts2并不推荐使用request原生)
 6         // 不推荐
 7         Map<String, Object> requestScope = (Map<String, Object>) ActionContext.getContext().get("request");
 8         // 推荐
 9         ActionContext.getContext().put("name", "requestTom");
10         // session域->map
11         Map<String, Object> sessionScope = ActionContext.getContext().getSession();
12         sessionScope.put("name", "sessionTom");
13         // application域->map
14         Map<String, Object> applicationScope = ActionContext.getContext().getApplication();
15         applicationScope.put("name", "applicationTom");
16         return SUCCESS;
17     }
18 }

 第二种方式

通过ServletActionContext

 1 public class Demo2Action extends ActionSupport {
 2     // 并不推荐
 3     @Override
 4     public String execute() throws Exception {
 5 
 6         // 原生request
 7         HttpServletRequest request = ServletActionContext.getRequest();
 8         // 原生session
 9         HttpSession session = request.getSession();
10         // 原生response
11         ServletActionContext.getResponse();
12         // 原生servletContext
13         ServletActionContext.getServletContext();
14 
15         return super.execute();
16     }
17 
18 }

第三种方式

通过实现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
 1 public class ScopeAction extends ActionSupport implements SessionAware{
 2       Map<String,Object> session;
 3       
 4      @Override
 5       public void setSession(Map<String, Object> arg0){
 6            session = arg0;  
 7        }
 8 
 9      @Override
10      public String execute() throws Exception{
11        session.put("abc","haha");
12        System.out.println(session.get("abc"));
13        return SUCCESS;
14     }           
15 }

 

 
posted @ 2017-11-19 19:11  mosterRan  阅读(600)  评论(0编辑  收藏  举报