gdpuzxs

1.导入阿里巴巴fastjson包。

  <!-- fastJson将对象转化为Json对象 -->
  <dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>fastjson</artifactId>
  <version>1.1.24</version>
  </dependency>

2.写一个公共的Action类,调用jar包中的toJSONStringWithDateFormat方法,其他Action类只要继承该类,就可以调用writeJson方法。

  public class BaseAction {

    public void writeJson(Object object){
      try {
          String json=JSON.toJSONStringWithDateFormat(object, "yyyy-MM-dd HH:mm:ss");

          ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");

          ServletActionContext.getResponse().getWriter().write(json);

          ServletActionContext.getResponse().getWriter().flush();

          ServletActionContext.getResponse().getWriter().close();
        }

      catch (IOException e) {


          e.printStackTrace();
        }
    }
}

3.前台收到json格式的返回值,使用jQuery.parseJSON(data)方法将json格式转化为javascript对象;

分类:

技术点:

相关文章:

  • 2021-12-02
  • 2023-03-29
  • 2021-10-24
  • 2022-02-20
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-14
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案