SSH整合之Struts2的JSON数据返回

来源:互联网 发布:js控制embed视频播放 编辑:程序博客网 时间:2024/06/05 18:57

写在前面的话:之前一直从事SpringMVC+Mybatis/Hibernate的开发,而SpringMVC做json返回对比于Struts简直是。。。首次接触Struts,其实Struts也是个挺简单的工具,个人感觉凡是工具,就不会太难,否则框架也就失去了存在的意义

上月底跳槽来到现在的公司,恰好遇到现在的项目,接近尾声,使用的ssh,是个二次开发项目,6年前(2010年)的老代码了,没有注释,没有数据库设计文档,还发现不少问题,不少的json数据都采用 jsp视图的方式转发,当时写这个代码的兄弟。。。。

Struts提供了多种返回JSON数据的方法

方式一、使用流的方式,把数据返回

protected void getResultView(boolean result, String resultMsg, Object resultData){Map<String, Object> jsonView = new HashMap<>();jsonView.put("success", result);jsonView.put("resultMsg", resultMsg);jsonView.put("resultData", resultData);setInputStream(new ByteArrayInputStream(JSON.toJSONBytes(jsonView)));}
<span style="font-family: Simsun;font-size:14px;">public InputStream getInputStream() {</span><span style="font-size:14px;">return inputStream;</span><span style="font-size:14px;">}</span>
此时需要指定返回的数据类型,顺带做IE的兼容 @Result(type = "stream",params = {"contentType","text/html;charset=UTF-8","inputName","inputStream"})
注意需要get方法否则客户端无法取得json数据

方法二:指定返回json

@ParentPackage("json-default")  public class JsonView{     private InputStream inputStream;     Map<String, Object> jsonView;     /**     * 默认返回成功      * {success: true,resultMsg: "ok"}     * @return     */     protected void getResultView() {         getResultView(true, "ok");     }     /**     * {success: true,resultMsg: "自定义"}     * @param result true成功  false 失败     * @param resultMsg 自定义返回字符串消息     * @return      */     protected void getResultView(boolean result, String resultMsg) {          getResultView(result, resultMsg, null);     }       /**     *{success: true,resultMsg: "OK",resultDate[]}     * @param resultData 需要返回的数据     * @return     */     protected void getResultView(Object resultData) {           getResultView(true, "ok", resultData);     }     /**     * {success: true,resultMsg: "操作成功",resultDate[]}     * @param result----boolean类型(true成功   false 失败)     * @param resultMsg--- String类型 json响应的文本信息描述       * @param resultData Object类型 json返回的数据      * @return     */     protected void getResultView(boolean result, String resultMsg, Object resultData){        jsonView = new HashMap<>();        jsonView.put("success", result);        jsonView.put("resultMsg", resultMsg);        jsonView.put("resultData", resultData);       }       public Map<String, Object> getJsonView() {        return jsonView;     }       public void setJsonView(Map<String, Object> jsonView) {        this.jsonView = jsonView;     }  }  

在返回的方法上添加注解,使用xml的配置也是一样的,推荐RESTful风格
@Action(value="get",results={@Result(type = "json", params = {"contentType","text/html;charset=UTF-8","root" ,"jsonView"})})

为了给Action瘦身,到达代码复用的目的,可以通过继承的方式,使得Action类更加干练
@Namespace("/test")public class AccountAction extends JsonView{   private static final Logger logger = Logger.getLogger(AccountAction.class);   public String setAccount() {      return ActionSupport.SUCCESS;   }   @Action(value="get",results={@Result(type = "json", params = {"contentType","text/html;charset=UTF-8","root" ,"jsonView"})})   public String getJson(){      logger.info("test Json");      Account account = new Account();      account.setId(110);      account.setUsername("dhweicheng");      account.setPassword("147258");      getResultView(account);//调用json工具,      return  ActionSupport.SUCCESS;   }}

后话:

认真阅读一次官方文档还是很有必要的,文档都有演示代码,很容易就看明白,那怕英文再差! (应该没有比我差的了上高中后就没及格过)


1 0