This blog records some ideas, notes, and information of my works and studies in information technology.

Tuesday, June 16, 2009

How to transfer JSON to JQuery client in Struts 1.x

Other programmer's idea
http://www.junlu.com/msg/322793.html

JSON ORG

JSON-lib

  1. Download JSON-lib and install it into Web project
  2. There is nothing special required to do to pass the parameter to Actions.
    Just follow JQuery's function:
    $.post("/TSMWeb/cardImportAction.do", {actionType: "initAjax", comAccount: issuerSelectValue}, issuerSelectCallBack, "json");
  3. In Struts 2.x, the only thing you need to do is set up the struts.xml to notify the framework packing the response in JSON format.
  4. In Struts 1.x, you should create a JSONObject to transfer your java object into JSON format and use an "almost empty" JSP to send JSON messages:


    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    ${tsmJSON}

  5. Save your JSON Object with a key name "tsmJSON" by calling request.setAttribute().
  6. That's it! Now you can catch the response in JSON format in your client JSP!

No comments:

Post a Comment