通过SPD MVC 3控制器将JAVA LIST获取到JQUERY的示例程序通过AJAX调用

问题描述

| JQUERY:
$.ajax({
        datatype:\"json\",url:\"<%=request.getcontextpath()%>/appendStudentView.page\",type: \'post\',success: function(data,status) {
        alert(\"status==\"+data)
        },error: function(xhr,desc,err) {
        alert(\"xhr==\"+xhr+\"Desc: \" + desc + \"\\nErr:\" + err);
        }
    });
弹簧控制器
/**
 * Handles request for adding two numbers
 */
@RequestMapping(value = \"/appendStudentView.page\")
public @ResponseBody String appendStudentField() {

    List xx=new ArrayList();
    xx.add(\"CONTROLLER\");
return xx;
}
我正在通过JQUERY AJAX调用appendStudentField()方法并返回一个列表。我没有在AJAX调用的响应中得到List xx。 请帮忙。 谢谢 兰迪     

解决方法

您的上课路上有杰克逊吗? Spring需要Jackson来输出JSON。   该标签注册了   DefaultAnnotationHandlerMapping和   AnnotationMethodHandlerAdapter豆   Spring MVC所需的   将请求分派给@Controllers。的   标签使用以下命令配置这两个bean   基于什么是合理的默认值   存在于您的类路径中。的   默认值为:   ...         支持读写JSON,   如果杰克逊在场   类路径。    资源: 配置Spring MVC> 15.12.1。 mvc:注释驱动     ,您不能仅使用模型并以这种方式传递变量吗?这是我使用的一些示例代码。
@Controller
@Scope(\"prototype\")
@RequestMapping(\"/favorites\")
public class FavoritesController {

    protected final Log logger = LogFactory.getLog(getClass());

    @Autowired
    FavoriteService favoriteService;

        @RequestMapping(method = RequestMethod.POST)
        public void handle(String circuit,String customer,String action,Model model) {

        String userid = SecurityContextHolder.getContext().getAuthentication().getName();

        List<Map<String,String>> favorites = null;

        if (action.equals(\"save\")) {
            favoriteService.setFavorite(userid,circuit,customer);
            favorites = favoriteService.getFavorites(userid);
        }

        if (action.equals(\"delete\")) {
            favoriteService.deleteFavorite(userid,circuit);
            favorites = favoriteService.getFavorites(userid);
        }

        model.addAttribute(\"userid\",userid);
        model.addAttribute(\"circuit\",circuit);
        model.addAttribute(\"customer\",customer);
        model.addAttribute(\"favorites\",favorites);
    }
}
[编辑添加了这个的jQuery部分]
   // **************************************** 
//  SAVE TO FAVORITES 
// ****************************************
$(\"#save-to-favorite\").live(\"click\",function() {
    var saveCircuit = $(this).attr(\'circuit\');
    var saveCustomer = $(this).attr(\'customer\');

    var data = \"action=save&circuit=\" + saveCircuit + \"&customer=\" + saveCustomer;
    $.ajax( {
        type : \"POST\",url : \"favorites.html\",data : data,success : function(xhr) {
            $(\"#favorite-list\").html(xhr);
        },error : function(xhr) {
            var response = xhr.responseText;
            response = response.replace(/<html>.+<body>/i,\"\")
            response = response.replace(/<\\/body><\\/html>/i,\"\")

            alert(response);
        }
    });
});

// ****************************************
// DELETE FROM FAVORITES
// ****************************************
$(\".delete-favorite-icon\").live(\"click\",function() {
    var deleteCircuit = $(this).attr(\'circuit\');

    var data = \"action=delete&circuit=\" + deleteCircuit;
    $.ajax( {
        type : \"POST\",\"\")

            alert(response);
        }
    });
});
`