【问题标题】:Sample program for Getting JAVA LIST from SPRING MVC 3 CONTROLLER to JQUERY THROUGH AJAX CALL从 SPRING MVC 3 CONTROLLER 到 JQUERY THROUGH AJAX CALL 获取 JAVA LIST 的示例程序
【发布时间】:2011-05-25 15:40:59
【问题描述】:

查询:

$.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 调用的响应中没有得到列表 xx。

请帮助它。

谢谢 兰迪

【问题讨论】:

  • 您是否从服务器或 JavaScript 控制台中收到任何错误?

标签: java jquery json spring spring-mvc


【解决方案1】:

你的类路径中有Jackson 吗? Spring 需要 Jackson 来输出 JSON。

这个标签注册了 DefaultAnnotationHandlerMapping 和 AnnotationMethodHandlerAdapter bean Spring MVC 所需的 向@Controllers 发送请求。这 标记将这两个 bean 配置为 基于什么是合理的默认值 存在于您的类路径中。这 默认值为:
...

  • 支持读写JSON, 如果杰克逊在场 类路径。

来源: Configuring Spring MVC > 15.12.1. mvc:annotation-driven

【讨论】:

    【解决方案2】:

    您不能只使用模型并以这种方式传递变量吗?这是我使用的示例代码。

    @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",
            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);
            }
        });
    });
    

    `

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-07
      • 1970-01-01
      • 1970-01-01
      • 2011-04-28
      • 2014-05-17
      • 1970-01-01
      • 2011-04-26
      • 1970-01-01
      相关资源
      最近更新 更多