通过 AJAX CALL 从 SPRING MVC 3 CONTROLLER 获取 JAVA LIST 到 JQUERY 的示例程序

发布于 2024-11-09 09:33:58 字数 749 浏览 0 评论 0原文

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);
        }
    });

SPRING CONTROLLER

/**
 * 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。

请帮助它。

谢谢 兰迪

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);
        }
    });

SPRING CONTROLLER

/**
 * Handles request for adding two numbers
 */
@RequestMapping(value = "/appendStudentView.page")
public @ResponseBody String appendStudentField() {

    List xx=new ArrayList();
    xx.add("CONTROLLER");
return xx;
}

I am calling the appendStudentField() method through JQUERY AJAX and returning a list .I am not getting the List xx in the response of the AJAX Call.

Kindly help it .

Thx
Randy

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

熊抱啵儿 2024-11-16 09:33:58

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

该标签注册
DefaultAnnotationHandlerMapping 和
AnnotationMethodHandlerAdapter bean
Spring MVC 所需的
将请求分派给@Controllers。这
标签配置这两个 bean
合理的默认值基于什么
存在于你的类路径中。这
默认值是:
...

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

来源:
配置 Spring MVC> 15.12.1。 mvc:注解驱动

Do you have Jackson on your classpath? Spring needs Jackson to output JSON.

This tag registers the
DefaultAnnotationHandlerMapping and
AnnotationMethodHandlerAdapter beans
that are required for Spring MVC to
dispatch requests to @Controllers. The
tag configures those two beans with
sensible defaults based on what is
present in your classpath. The
defaults are:
...

  • Support for reading and writing JSON,
    if Jackson is present on the
    classpath.

Source:
Configuring Spring MVC > 15.12.1. mvc:annotation-driven

夜雨飘雪 2024-11-16 09:33:58

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

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

`

Couldn't you just use a Model and pass your variables that way? Here is an example bit of code I use.

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

[Edited to add the jquery portion of this]

   // **************************************** 
//  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);
        }
    });
});

`

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文