0
@RequestMapping(value = "/dropDown", method = RequestMethod.GET)
public @ResponseBody
DropDown getList(Map<String, Object> map, HttpServletRequest request,
        HttpServletResponse response) {
    DropDown dropDown = new DropDown();
    List<Map<String, Object>> rows = new ArrayList<Map<String, Object>>();
    List<MapTable2> list = contactService.mapProcess();
    for (MapTable2 table : list) {
        Map<String, Object> dataRow = new HashMap<String, Object>(1);
        dataRow.put("text", table.getProcess());
        dataRow.put("value", table.getId());
        dataRow.put("selected", false);
        dataRow.put("description", table.getProcess());
        dataRow.put("imageSrc", "image.jpg");
        rows.add(dataRow);
    }
    dropDown.setRows(rows);
    return dropDown;
}

次のものを作成する必要があります

var ddData = [
{
    text: "Facebook",
    value: 1,
    selected: false,
    description: "Description with Facebook",
    imageSrc: "http://dl.dropbox.com/u/40036711/Images/facebook-icon-32.png"
},
{
    text: "Twitter",
    value: 2,
    selected: false,
    description: "Description with Twitter",
    imageSrc: "http://dl.dropbox.com/u/40036711/Images/twitter-icon-32.png"
}]

上記のJavaコーディングの問題は知っていますが、上記のようなjson配列を作成することを知りません。確認して、修正するのを手伝ってください。

MapTable2 には ProcessId と ProcessName があります

public class MapTable2 {
private int id;
private String process;
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getProcess() {
    return process;
}
public void setProcess(String process) {
    this.process = process;
}

}

4

2 に答える 2

3

@theonは正しいです。

を使用しているので@Responsebody、Spring に JSON 変換を任せることができます。JSON 配列内のオブジェクトに一致するクラスを作成します。

public class SomeObject {
    public String getText() { //... }
    public int getValue() { //... }
    public boolean getSelected { // ... }
    public String getDescription { // ... }
    public String getImageSrc { // ... }
}

オブジェクトにデータを入力し、コントローラーからリストとして返します。

@RequestMapping(value = "/dropDown", method = RequestMethod.GET)
@ResponseBody
public List<SomeObject> getList(Map<String, Object> map, HttpServletRequest request, HttpServletResponse response) {
     // Get the objects, return them in a list
}

まだ行っていない場合を除き、<mvc:annotation-driven />またはをアプリケーション構成に追加します。@EnableWebMvcJackson がクラスパスで利用可能であることを確認すると、Spring は自動的にオブジェクトを JSON にシリアル化します(リクエストにContent-Type: application/json.@RequestMapping

于 2012-11-24T21:39:12.140 に答える
1

よくこのライブラリを使用してください。非常に軽量 (16KB) で、まさに必要なことを実行します。したがって、あなたの場合、JSONObject内部的に拡張HashMap して実行する which を使用します

JSONObject o = new JSONObject();
o.put("text","whatever text");
o.put("value",1);
o.put("selected",false);
//and so on
JSONArray arr = new JSONArray();
arr.add(o);

上記はあなたにこれを与えるでしょう:

[
{
text: "Facebook",
value: 1,
selected: false,
description: "Description with Facebook",
imageSrc: "http://dl.dropbox.com/u/40036711/Images/facebook-icon-32.png"
}
]

さらにオブジェクトを追加するJSONObjectsには、ループでさらに追加しますJSONArray

したがって、指定されたコードに従って、dataRow「JSONObject」に置き換えてrowsJSONArrayそれだけです。最後に、文字列を取得するには、rows.toString().

于 2012-11-24T18:14:32.607 に答える