0

我的豆类,

import java.util.LinkedList;

public class Information {

    private LinkedList<String> information ;

    public LinkedList<String> getInformation() {
        return information;
    }

    public void setInformation(LinkedList<String> information) {
        this.information = information;
        System.out.println("List is : "+information);
    }


}

我的控制器,

@RequestMapping(value="/registerDn.html",method=RequestMethod.POST)
public @ResponseBody Information registerDn( @RequestParam(value = "dn", required = false) String dn,
        @RequestParam(value = "acd", required = false) String acd ){
    System.out.println("DN is : "+dn+   "    acd : "+acd);
    WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(context);
    UserOperation oper = (UserOperation)ctx.getBean("operation");
    oper.registerDn(dn);

    Information info = (Information) ctx.getBean("info");
    return info;
}

我的 jquery 将是,

function registerDn(){

    $.ajax({  
        type: "POST",  
        url: contexPath + "/registerDn.html",
        data: "dn=" + dn + "&acd=" + acd,  
        success: function(response){

            var userInfo = "<ol>";

            for( i =0 ; i < response.information.length ; i++){
                userInfo += "<br><li><b>Name</b> : " + response.information[i] ;
            }

            userInfo += "</ol>";

            //Used to display the list
            $('#getlist').html(dn + " is : " + userInfo); 

        },  
        error: function(e){  
            alert('Error: ' + e);  
        }, 

    }); 

} 

我在 jquery-ajax 中获得了成功。但不知道如何解析它并在视图中显示它。

或单击按钮时如何使用 jquery-ajax 获取 bean 类中的列表。

好的答案绝对值得赞赏。

4

1 回答 1

0

您的示例代码中没有任何内容表明响应应该是/应该是 JSON 对象。

  1. Jackson jars 必须在类路径上,以便在应用程序的上下文中配置 MappingJackson HttpMessageConverter
  2. jQuery ajax 配置应将 dataType 定义为 json 或使用注释的produces属性@RequestMapping并将值设置为application/json

基本上,如果 Spring 知道响应内容类型应该是 json,它会使用 Jackson Mapping Converter 将你的 pojo 转换为 JSON,然后你的 jQuery 成功回调会得到一个 JSON 对象。

不过,您应该确切地看到 FireBug 发生了什么。

于 2013-04-20T16:57:46.800 回答