user2660852
user2660852

Reputation:

How do get the list from @ResponseBody in JSP?

I use Spring MVC, I have controller with a method:

    @RequestMapping(value = "/listReader", method = RequestMethod.POST)
    public @ResponseBody
    List<Reader> getListReader(ModelMap model) {

        return libraryService.getAllReaders();
    }

But I do not know:

Give an example, please.

Upvotes: 1

Views: 3740

Answers (2)

Thomas Junk
Thomas Junk

Reputation: 5676

@RequestMapping(value = "/listReader", method = RequestMethod.POST)

That defines an Endpoint. I would suggest using RequestMethod.GET than RequestMethod.POST, since you want something from the server not POSTING something to the server. On the other Hand, if you want to use that information on a JSP, there are two ways:

1) Some view could consume that information via a http-GET-Request like $.get in jQuery or ajax. After you got the data, you can insert it in your HTML

2) Instead of using ajax you could display it directly on the page. Then, you have to put your List into a Model, which you can acces on your JSP with Expression Language. Then you have to remove the @ResponseBody and return an according view instead.

Upvotes: 0

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279960

You can't. @ResponseBody is basically telling Spring: take the object I (method) return and use any serializer you have that supports it and write it directly to the body of the HTTP response. There's no JSP involved here.

Instead you should add the list to the model and return the String view name of your JSP.

@RequestMapping(value = "/listReader", method = RequestMethod.POST)
public String getListReader(ModelMap model) {
    model.addAttribute("someKey", libraryService.getAllReaders());
    return "my-jsp";
}

then you can use EL in the JSP to retrieve it

<h3>${someKey}</h3>

Use JSTL to iterate over it.

Upvotes: 3

Related Questions