簡體   English   中英

為什么相同的代碼在servlet中起作用,但在Spring控制器中卻不起作用?

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

此代碼在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);
}

所以我嘗試通過使用model.addAttribute (下)而不是req.setAttribute (上)將其放在Spring控制器中:

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

但是,Spring代碼無法在Picasa中找到任何相冊,而servlet代碼可以成功找到它們。

有人知道為什么會這樣嗎?

在這兩種情況下,他們都使用此版本的PicasawebClient此版本的PicasawebService

model.addAttribute("photos", photos);

將在每次迭代時覆蓋地圖的photos屬性,因此您將只能訪問最后一個相冊。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM