04-struts2框架中获取servlet api及Result结果类型

在struts2框架中获取servlet api


对于struts2框架,不建议直接使用servlet api;


在struts2中获取servlet api有三种方式:

1.通过ActionContext来获取


1.获取一个ActionContext对象。
ActionContext context=ActionContext.getContext();
2.获取servlet api
注意:通过ActionContext获取的不是真正的Servlet api,而是一个Map集合。

1.context.getApplication()
2.context.getSession()
3.context.getParameter();---得到的就相当于request.getParameterMap()
4.context.put(String,Object) 相当于request.setAttribute(String,String);


2.注入方式获取(这种方式是真正的获取到了servlet api)


1.要求action类必须实现提定接口。
ServletContextAware : 注入ServletContext对象
ServletRequestAware :注入 request对象
ServletResponseAware : 注入response对象

2.重定接口中的方法。
private HttpServletRequest request;
3.声明一个web对象,使用接口中的方法的参数对声明的web对象赋值.
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}

扩展:分析其实现:
是使用struts2中的一个interceptor完成的.
<interceptor name="servletConfig" class="org.apache.struts2.interceptor.ServletConfigInterceptor"/>

if (action instanceof ServletRequestAware) { //判断action是否实现了ServletRequestAware接口
HttpServletRequest request = (HttpServletRequest) context.get(HTTP_REQUEST); //得到request对象.
((ServletRequestAware) action).setServletRequest(request);//将request对象通过action中重写的方法注入。
}


3.通过ServletActionContext获取.


在ServletActionContext中方法都是static。
getRequest();
getResposne();
getPageContext();
-------------------------------------------------------------------------------


Result结果类型

 


<result>标签

1.name 与action中的method的返回值匹配,进行跳转.

 

2.type 作用:是用于定义跳转方式


对于type属性它的值有以下几种:
在struts-default.xml文件中定义了type可以取的值

<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" />

必会: chain dispatcher redirect redirectAction stream

dispatcher:它代表的是请求转发,也是默认值。它一般用于从action跳转到页面。
chain:它也相当于请求转发。它一般情况下用于从一个action跳转到另一个action。

redirect:它代表的是重定向 它一般用于从action跳转到页面
redirectAction: 它代表的是重定向 它一般用于从action跳转另一个action。

stream:代表的是服务器端返回的是一个流,一般用于下载。

了解: freemarker velocity

----------------------------------------------------

3.局部结果页面与全局结果页面


局部结果页面 和 全局结果页面
<action name="result" class="cn.itcast.struts2.demo6.ResultAction">
<!-- 局部结果 当前Action使用 -->
<result name="success">/demo6/result.jsp</result>
</action>

<global-results>
<!-- 全局结果 当前包中 所有Action都可以用-->
<result name="success">/demo6/result.jsp</result>
</global-results>

原文地址:https://www.cnblogs.com/1963942081zzx/p/6485455.html