一、response对象返回 ???? ??response.setContentType("text/html"); ?? ? ?PrintWriter out = response.getWriter(); ?? ? ?out.println("json"); ?? ? ?out.flush(); ?? ? ?return?null; ?? ? 输入结果: json 二、通过struts返回stream来输出 ?? ?action代码 ?? ? public?class TextResult?extends?ActionSupport { ?? ? ? ? ? ?private?InputStream inputStream; ?? ? ? ? ? ?public?InputStream getInputStream() { ?? ? ? ? ? ? ? return?inputStream;? ?? ? ? ? ? ?}? ?? ? ? ? ?publicString?execute()?throws?Exception { ?? ? ? ? ? ? ? inputStream =?new?StringBufferInputStream("json"); ?? ? ? ? ?return?SUCCESS; }? ?? ? } ?? ? xml配置 ?? ??<action name="text-result"?class="actions.TextResult"> ?? ? ? ? ? ? ? ? ? <result type="stream"> ?? ? ? ? ? ? ? ? ? ? ? ? ??<param name="contentType">text/html</param> ?? ? ? ? ? ? ? ? ? ? ? ? ?<param name="inputName">inputStream</param> ?? ? ? ? ? ? ? ? ? </result> ?? ? ? ?</action> ?? ? 输出结果: json 三、使用struts2-json-plugin插件 ?? ?action代码 ?? ? public?class TextResult?extends?ActionSupport { ?? ? ? ???private?Object name;?? ? ? ? ?? ?? ? ? ? ?public?String?execute()?throws?Exception { ?? ? ? ? ? ? ? name = "json"; ?? ? ? ? ?return?SUCCESS; }??? ?? ?? ? ?? public String getName() { ?? ? ? ?? return this.name; ?? ? ? ?} ?? ? } ?? xml配置 ?<package name="example"?extends="json-default"> ?? ? ? <action name="JSONExample"?class="example.JSONExample"> ?? ? ? ? ? ??<result type="json"> ?? ? ? ? ? ? ? ? ? ??<param name="contentType">text/html</param> ?? ? ? ? ? ? </result> ?? ? ? </action> </package> 输出结果:{"name":"json"} 使用这个插件,默认会把action中所有有get方法的属性把输出,可以使用@JSON(serialize =false)这个注解进行取消。 参考strtus2文档
详细解决方案
struts2回来json和几种方式
热度:368 发布时间:2012-06-27 14:20:09.0
相关解决方案
- struts2.xml的action中的result报错,该怎么处理
- (Struts2+JSON+Ajax) XMLHttpRequest ==500如何解决
- struts2 对象属性流入不进去, 报错:target is null for setProperty(null, "x" [Ljava.lang.Stri
- json 解析有关问题
- Struts2 <select>上拉框 回显有关问题
- struts2 添加和修改怎么在一个jsp页面实现
- struts2 查询输出的有关问题
- 零配置的有关问题(struts2+Hibernate)
- struts2 神奇有关问题,太神奇了!
- struts2 的<s:iterator>标签展示未完全初始化的数组出现奇怪的有关问题
- struts2 文件下传有关问题
- struts2+ajax请求失败,帮忙看一上
- struts2-json中诠注@JSON(deserialize=false)是什么用?不是阻止JSON反序列化成JAVA对象吗?求解
- struts2 循环累加属性值有关问题
- struts2,hibernate,spring3 常用框架的API CHM版本,该怎么处理
- struts2 将jsp回到到另一个jsp中
- struts2 提交表单不能跳转页面有关问题!action已经执行,但是不能跳转到指定的页面
- struts2,hibernate,spring3 惯用框架的API CHM版本
- Struts2+Spring+JPA+FREEMARKER 登录程序异常
- struts2+Hibernate一个很奇怪的现象,该如何解决
- struts2 action中部分语句被直接跳过不执行. 请教这会是什么原因
- struts2+iReport 子表格 急需高手解决,已经困惑三天了!
- import cn.luxh.struts2.entity.Novel 急需的jar包是哪个
- struts2 hibernate 分页有关问题
- struts2 + spring 整合有关问题
- 请教哪里可以上到 struts2-json-plugin-2.1.8的源文件
- struts2+freemarker中文参数乱码有关问题
- struts2 exception,该怎么解决
- struts2 fileupload 下传有关问题
- struts2.3.4+spring3.1+mybats帮忙看看这个有关问题