简体   繁体   中英

Why does the same code work in a servlet but not in a Spring controller?

This code works in a servlet:

PicasawebService service = new PicasawebService("Picasa test");
PicasawebClient picasaClient = new PicasawebClient(service);
List<AlbumEntry> albums = picasaClient.getAlbums("cgcmh1@gmail.com");
for(AlbumEntry album: albums){
    resp.getWriter().println(album.getTitle().getPlainText());
    List<PhotoEntry> photos = picasaClient.getPhotos(album);
    req.setAttribute("photos", photos);
}

So I tried putting it in a Spring controller by using model.addAttribute (below) instead of req.setAttribute (above):

PicasawebService service = new PicasawebService("Picasa test");
PicasawebClient picasaClient = new PicasawebClient(service);
List<AlbumEntry> albums = picasaClient.getAlbums("cgcmh1@gmail.com");
for (AlbumEntry album : albums){
    logger.warn("albums:" + album.getTitle().getPlainText());
    List<PhotoEntry> photos = picasaClient.getPhotos(album);
    model.addAttribute("photos", photos);
}

However, the Spring code fails to find any albums in Picasa while the servlet code finds them successfully.

Anyone know why this is the case?

In both cases they are using this version of the PicasawebClient and this version of the PicasawebService .

model.addAttribute("photos", photos);

将在每次迭代时覆盖地图的photos属性,因此您将只能访问最后一个相册。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM