In Spring MVC, how to send information to the view (.jsp) without using Modelandview?

Asked

Viewed 612 times

5

I am developing a web application in Spring MVC.

I have a method that returns a list (java.util.List) and I need to pass it to the view (.jsp), but without updating the page.

I would like to know some other way to pass the information from Java to the view, but without using Modelandview, because when I use it the page is updated.

  • 3

    To avoid a new request, you will need to use AJAX

  • 2

    Create a Controller that will receive the AJAX requests, and call the methods that will return the values, then convert the values to JSON, for example, and return to the callback of your ajax function to process and update the html dynamically

1 answer

2


As stated in the comment, you need to Ajax to avoid page loading.

An example using Jquery:

$.ajax({
    url: '/lista',
    success: function(data) {
         //Aqui você recebe sua lista
    }
});

Your controller would look like this:

@ResponseBody//O spring vai retornar sua lista como texto, vai traduzir para Json
@RequestMapping(value = "/lista")
public List<Item> listagem() {
    //Aqui você retorna sua lista contendo os objetos
    return listaDao.findAll();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.