0

i'm writing my first webapp in Spring MVC. It's dynamic web page, where user can add wallpaper by multipart/form-data. Then he's redirected to home page, where are displayed all of added pictures. But when i'm trying to add image and see the result i can see only empty div, like pictures would not exist. But after restarting Tomcat image is displaying properly. I tried to change a scope, but it was bad idea I think. Also i changed LinkedList to ArrayList, also unsuccessfully.

home.jsp

<div class="row">
    <c:forEach items="${paths}" var="path">
        <div class="col-lg-3 col-md-4 col-xs-6 thumb">
            <a class="thumbnail" href="#">
                <img class="img-responsive" src="../../../resources/wallpapers/${path}">
            </a>
        </div>
    </c:forEach>
</div>

WallpaperService.java

public List<Wallpaper> wallpapers = new LinkedList<>();
public List<String> paths = new ArrayList<>();

public Wallpaper addWallpaper(File image) {
    Wallpaper wallpaper = new Wallpaper();

    wallpaper.setImage(image);
    wallpapers.clear();
    wallpapers.add(wallpaper);

    return wallpaper;

HomeController.java

File[] files = new File("D:\\IntelliJ IDEA\\Wallpapers\\src\\main\\web\\resources\\wallpapers\\").listFiles();

@RequestMapping("/")
public String home(Model model){
    wallpaperService.paths.clear();

    for (File file : files) {
        wallpaperService.paths.add(file.getName());
    }

    model.addAttribute("paths", wallpaperService.paths);

    return "home";
}

img: home view

generated html(first div with image is not displaying, another one was added earlier, displayed properly after restarted tomcat):

<div class="row">

        <div class="col-lg-3 col-md-4 col-xs-6 thumb">
            <a class="thumbnail" href="/resources/wallpapers/wallhaven-138570.jpg"
               data-lightbox="wallhaven-138570.jpg">
                <img class="img-responsive" src="/resources/wallpapers/wallhaven-138570.jpg" />
            </a>
        </div>

        <div class="col-lg-3 col-md-4 col-xs-6 thumb">
            <a class="thumbnail" href="/resources/wallpapers/wallhaven-29611.jpg"
               data-lightbox="wallhaven-29611.jpg">
                <img class="img-responsive" src="/resources/wallpapers/wallhaven-29611.jpg" />
            </a>
        </div>

</div>
Tymek
  • 37
  • 8
  • you should place images to the static to be able retrieve them from different applications. – Roman C Jun 27 '16 at 10:37
  • If I understand well I tried to write like this, but there is still same problem: – Tymek Jun 27 '16 at 19:37
  • If you put manually a file to `/resources/wallpapers/` can you access the file in the browser? – Roman C Jun 27 '16 at 20:01
  • Yes, work well. I just found solution of one guy, but I can't apply it to my code. He used html tag and rewrite page. Like this: src="", but my src is incorrect I suppose and I don't know why. – Tymek Jun 27 '16 at 20:21
  • Spring hasn't `html:rewrite` tag, use JSTL or `spring:url`, see [my answer](http://stackoverflow.com/a/38062628/573032) below. – Roman C Jun 27 '16 at 20:29
  • This most likely answers your http://xyproblem.info: http://stackoverflow.com/q/18664579 – BalusC Jun 27 '16 at 21:31
  • Possible duplicate of [Recommended way to save uploaded files in a servlet application](http://stackoverflow.com/questions/18664579/recommended-way-to-save-uploaded-files-in-a-servlet-application) – Roman C Jun 30 '16 at 11:34

1 Answers1

0

The list of files is created in the constructor of your controller. This controller is called only once, at application startup, since Spring beans are singletons (by default).

If you need an up-to-date list of files, create that list in your home() method, not in the constructor.

Also, make your service stateless, and don't ever use public fields. I don't think this service is useful at all in fact.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thanks for help. I moved list to home() method and you were right, service wasn't useful so I deleted this class. But there is still same problem. When I change HomeController to @RequestScope I think I have my list updated, becouse I'm creating a new div, only problem is that i cannot see content of this div. – Tymek Jun 27 '16 at 12:08