struts2resultType.docx
- 文档编号:26783672
- 上传时间:2023-06-22
- 格式:DOCX
- 页数:12
- 大小:19.34KB
struts2resultType.docx
《struts2resultType.docx》由会员分享,可在线阅读,更多相关《struts2resultType.docx(12页珍藏版)》请在冰豆网上搜索。
struts2resultType
首先看一下在struts-default.xml中对于result-type的定义:
chain:
用来处理Action链,被跳转的action中仍能获取上个页面的值,如request信息.
dispatcher:
用来转向页面,通常处理JSP.
freemaker:
处理FreeMarker模板.
httpheader:
控制特殊HTTP行为的结果类型.
redirect:
重定向到一个URL,被跳转的页面中丢失传递的信息,如request.
redirectAction:
重定向到一个Action,跳转的页面中丢失传递的信息.
stream:
向浏览器发送InputSream对象,通常用来处理文件下载,还可用于返回AJAX数据.
velocity:
处理Velocity模板.
xslt:
处理XML/XLST模板.
plainText:
显示原始文件内容,例如文件源代码.
重点说一下redirect和redirectAction的区别:
(1)使用redirect需要后缀名,使用redirect-action可以不需要后缀名.
(2)type="redirect"的值可以转到其它命名空间下的action,而redirect-action只能转到同一命名空下的action,因此它可以省略.do的后缀直接写action的名称.
1、dispatcher结果类型
Struts2在后台使用ServletAPI的RequestDispatcher来转发请求,因此在用户的整个请求/响应过程中,目标Servlet/JSP接收到的request/response对象,与最初的Servlet/JSP相同。
Dispatcher结果类型的实现是org.apache.struts2.dispatcher.ServletDispatcherResult,该类的二个属性(property):
location和parse,这两个属性可以通过struts.xml配置文件中的result元素的param子元素来设置。
param元素的name属性指定结果类型实现类的属性名,param元素的内容是属性的值。
例如:
说明:
A、location参数用于指定action执行完毕后要转向的目标资源,parse属性是一个布尔类型的值,如果为true,则解析location参数中的OGNL表达式;如果为false,则不解析。
parse属性的默认值就是true.
location参数是默认的参数,在所有的Result实现类中,都定义了一个字符串类型的DEFAULT_PARAM静态常量,专门用于指定默认的参数名。
DEFAULT_PARAM常量的定义:
publicstaticfinalStringDEFAULT_PARAM=“location”;
B、在设置location参数时,可以在参数值中使用OGNL表达式。
--如果参数是中文: 请参看最底部例子--> id=${id} 考虑到默认值的使用(dispatcher和location都是默认值),上述可以简化为: id=${id} 2、redirect结果类型(重定向到一个Url,也可以是Action或一个页面) Redirect结果类型在后台使用HttpServletResponse的sendRedirect方法将请求重定向到指定的URL,它的实现类是org.apache.struts2.dispatcher.ServletRedirectResult.该类同样有二个属性(property): location和parse,在使用redirect结果类型的场景中,用户要完成一次与服务器之间的交互,浏览器需要完成两次请求,因此第一次请求中的数据在第二次请求中是不可用的,这意味在目标资源中是不能访问action实例、action错误以及错误等。 如果有某些数据需要在目标资源中访问, i、一种方式是将数据保存到Session中, ii、另一种方式是通过请求参数来传递数据。 示例 (1)、 --不解析OGNL--> 示例 (2)、 <--Passparameters(reportType,widthandheight),重定向到Url并且传参,如果参数是中文: 请参看最底部例子--> -- Theredirect-actionurlgeneratedwillbe: /genReport/generateReport.jsp? reportType=pie&width=100&height=100 --> 3、redirectAction结果类型(重定向到一个Action) 他经常用于防止表单重复提交,比方说在增加完用户之后要显示列表 redirectAction结果类型的实现类是org.apache.struts2.dispatcher.ServletActionRedirectResult,该类是ServletRedirectResult的子类,因此我们也就可以判断出redirectAction结果类型和redirect结果类型的后台工作原理是一样的,即都是利用HttpServletResponse的sendRedirect方法将请求重定向到指定的URL。 示例、 --Redirecttoanothernamespace重定向到不同命名空间下的action--> <--Redirecttoanactioninthesamenamespace,重定向到同一命名空间下的action--> <--Passparameters(reportType,widthandheight),重定向到Action并且传参,如果参数是中文: 请参看最底部例子--> -- TheredirectActionurlgeneratedwillbe: /genReport/generateReport.action? reportType=pie&width=100&height=100 --> 4、链接类型result: chain(从一个Action转发到另一个Action) chain结果类型有4个属性,分别是: actionName(default)-thenameoftheactionthatwillbechainedto namespace-usedtodeterminewhichnamespacetheActionisinthatwe'rechaining.Ifnamespaceisnull,thisdefaultstothecurrentnamespace method-usedtospecifyanothermethodontargetactiontobeinvoked.Ifnull,thisdefaultstoexecutemethod skipActions-(optional)thelistofcommaseparatedactionnamesfortheactionsthatcouldbechainedto 示例、 --ChaincreatAccounttologin,usingthedefaultparameter,链接到同一命名空间下的Action,--> --Chaintoanothernamespace--> 5、HttpHeaderResult: HttpHeader(用来控制特殊的Http行为) httpheader结果类型很少使用到,它实际上是返回一个HTTP响应的头信息 示例: 6、StreamResult(向浏览器发送InputSream对象,通常用来处理文件下载) 7、PlainTextResult(显示原始文件内容,例如文件源代码) 若仅设置type="plainText"的话,页面中显示中文时会乱码,这时就可以借助它的charSet属性以解决中文显示时的乱码问题,如果不设置charSet属性,反而去配置struts.i18n.encoding全局属性,是不能解决问题的 设置charSet属性的目的就是让JSP页面的编码与明文显示时的编码一致 8、VelocityResult(处理Velocity模板) 9、XLSResult(处理XML/XLST模板) 10、FreeMarkerResult(处理FreeMarker模板) 附、另外第三方的Result类型还包括JasperReportsPlugin,专门用来处理JasperReport类型的报表输出。 <%@tagliburi="http: //tiles.apache.org/tags-tiles"prefix="tiles"%> <%@taglibprefix="s"uri="/struts-tags"%> <%--Showusage;UsedinHeader--%> importAttributename="title"scope="request"/> getAsStringname="title"/> insertAttributename="header"/> insertAttributename="body"/> NoticethatthisisalayoutmadeinJSP 注意! ! ! ! .传递中文 记住永远不要在浏览器的地址栏中传递中文。 在传递中文前先进行编码 A.action中 publicclassUserextendsActionSupport{ privateStringusername; publicStringgetUsername(){ returnusername; } publicvoidsetUsername(Stringusername){ this.username=username; } @Override publicStringexecute()throwsException{ //TODOAuto-generatedmethodstub username=URLEncoder.encode("郭蕾","utf-8");//先进行编码 System.out.println(username); return"redirect"; } } B.struts.xml中 /redirect.jsp? username=${username}//如果要传递两个参数,中间用&代替& 在这里使用了类似于el表达式的方式传值,${username}其中username为action中定义的 C.redirect.jsp中
重定向
<%Strings=request.getParameter("username");
s=newString(s.getBytes("iso8859-1"),"utf-8");
s=URLDecoder.decode(s,"utf-8");
out.print
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- struts2resultType