扩展Struts2--自定义String和XML格式的Result
来源:互联网 发布:c语言1000以内水仙花数 编辑:程序博客网 时间:2024/05/16 02:54
来源:http://blog.csdn.net/d8111/archive/2008/09/11/2914079.aspx
struts2虽然继承了webwork优秀的MVC分离,可是有很多地方让人百思不得其解!最让人离谱的是,返回的结果集中居然没有String,xml这两种非常常用的类型。还是自己动手,丰衣足食:
第一种方式:使用“PlainText Result”
先看官方文档对plain text结果的定义:“A result that send the content out asplain text. Usefull typically when needed to display the raw content ofa JSP or Html file for example.”这是一个纯扯蛋的说法。。。貌似感觉只能返回jsp页面似的,最起码他误导了我。
其实使用“PlainText Result” ,返回的结果是未进行格式和编码定义的字符串。什么意思?就类似于“FreeMarker Result” ,返回一个*.ftl格式的模板,你完全可以在*.ftl写string,那么结果就是string;也可以在里面写xml,那么结果就是xml。
举例如下:
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
- <struts>
- <package name="example" namespace="/example"
- extends="struts-default">
- <action name="outputXml" method="outxml" class="example.OutputXml">
- <result name="xmlMessage" type="plaintext"></result>
- </action>
- </package>
- </struts>
这里定义了xmlMessage为plain text结果,至于它具体是什么,看下面的Action类:
- public class OutputXml extends ActionSupport {
- public void outxml() throws Exception {
- HttpServletResponse response = ServletActionContext.getResponse();
- response.setContentType("text/xml ");
- PrintWriter pw = response.getWriter();
- pw.print("<cc>cccccc</cc>");
- }
在代码中,我们显式的给response定义了ContentType。那么返回去的内容"<cc>cccccc</cc>"就会被接收方按xml进行解析。
而如果需要返回的是String类型,那么contentType = "text/plain”。
如果进一步需要指明编码,那么contentType = "text/plain; charset=UTF-8";
到这里理解“plain text的结果是未进行格式和编码定义的字符串”应该就不困难了。基于http的内容传输实际都是字符串型,类型的定义是放在response的contentType 中。
第二种方式: 直接扩展struts2的结果集StrutsResultSupport :
代码如下:
应该很容易懂了。。嘿嘿
使用的方法:
1.Action
- package test;
- import com.opensymphony.xwork2.ActionSupport;
- public class MyAction extends ActionSupport{
- String result="abc";
- public String ajax() {
- return "ajaxResponse";
- }
- // getter && setter
- public String getResult() {
- return result;
- }
- public void setResult(String result) {
- this.result = result;
- }
- }
2.定义struts.xml
无非也就是将string结果集进行申明,然后给返回“ajaxResponse”的结果绑定变量名。这里定义返回Action的String result变量,即“abc”。
- 扩展Struts2--自定义String和XML格式的Result
- 扩展Struts2--自定义String和XML格式的Result
- 扩展Struts2--自定义String和XML格式的Result收藏
- 扩展Struts2--自定义String和XML格式的Result
- 扩展Struts2--自定义String和XML格式的Result
- Struts2输出XML格式的Result
- Struts2输出XML格式的Result
- Struts2输出XML格式的Result
- Struts2输出XML格式的Result
- 扩展struts2的结果集StrutsResultSupport 自定义Result处理JSON
- Struts2中自定义的Result
- struts2中struts.xml 的result 和param
- struts2.xml中的全局Result和局部Result
- Struts2的cosntant和result
- [Struts2]struts.xml中result的path
- [转]struts2.xml的result里传多个参数
- Struts2 自定义Result类型
- Struts2 自定义Result类型
- asp.net ajax 序列化出错
- C++中union的应用剖析
- 2009第一声雷,我听到了
- Dispatcher 源码
- 用ExtJS 实现动态载入树(Load tree)
- 扩展Struts2--自定义String和XML格式的Result
- extJs tree add edit delete js代码
- ROM、RAM、DRAM、SRAM、FLASH的区别
- Windows Live Messenger 2009 登录错误 80040154或不能登录 的解决办法
- 传智播客struts学习,Struts的概念和体系结构
- 菜猪的JAVA 网络编程学习之Socket用法详解(上)
- openmeetings052安装记录
- Oracle数据库重复数据删除的三种情况收藏
- ORACLE管理员指南(二):创建oracle数据库