开发者

Sample program for Getting JAVA LIST from SPRING MVC 3 CONTROLLER to JQUERY THROUGH AJAX CALL

开发者 https://www.devze.com 2023-03-08 03:41 出处:网络
JQUERY: $.ajax({ datatype:\"json\", url:\"<%=request.getContextPath()%>/appendStudentView.page\", type: \'post\',

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


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


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

`

0

精彩评论

暂无评论...
验证码 换一张
取 消