Click here to Skip to main content
15,885,900 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to learn thymeleaf deeper and facing a problem, that after a post method nothing appears in my other page. I've watched tutorials and docs, but seems I`m missing something.

So first of all I have main page with MainController:

Java
package com.gallery.galleryproject.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class MainController {
    @RequestMapping(value = "", method = RequestMethod.GET)
    public String loadMainPage() {
        return "main.html";
    }
}

<!doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Welcome</title>
</head>
<body style="background-color: #D3D3D3">
<nav>
    <div>
        <h2 style="text-align: center">Welcome to gallery</h2>
    </div>
</nav>
<section style="padding-top: 20px">
    <div style="text-align: center;">
        <p>View gallery: <a href="/gallery" style="text-decoration: none">Visit</a></p>
        <p>Add new photo to gallery: <a href="/photo" style="text-decoration: none">Visit</a></p>
    </div>
</section>
</body>
</html>


Then I have a gallery controller and gallery page(in this page i want display information which was sent from photo page). GalleryController + page:
Java
package com.gallery.galleryproject.controller;

import com.gallery.galleryproject.model.Photo;
import com.gallery.galleryproject.service.GalleryService;
import com.gallery.galleryproject.service.PhotoService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class GalleryController {

    private GalleryService galleryService;

    public GalleryController(GalleryService galleryService) {
        this.galleryService = galleryService;
    }


    @RequestMapping(value = "/gallery", method = RequestMethod.GET)
    public String listOfObjects(Model model) {
        Photo photo = galleryService.getAllPhotos();
        model.addAttribute("photo", photo);
        return "gallery.html";
    }
}


<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Gallery</title>
</head>
<body>
<nav>
    <div>
        <h2 style="text-align: center">Take a look..</h2>
    </div>
</nav>
<section>
    <div class="container">
        <div class="display-galery" style="padding-left: 30px">
            <p>Id of the photo: </p>
            <p>Name of the photo: </p>
            <p>Tag of the photo: </p>
            <p>Quality of the photo: </p></div>
    </div>
</section>
</body>
</html>


Here's my photo controller + html page where i fill up the form.
Java
package com.gallery.galleryproject.controller;

import com.gallery.galleryproject.model.Photo;
import com.gallery.galleryproject.service.PhotoService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class PhotoController {

    private PhotoService photoService;

    public PhotoController(PhotoService photoService){
        this.photoService = photoService;
    }

    @RequestMapping(value = "/photo", method = RequestMethod.GET)
    public String displayPhoto(Model model) {
        Photo photo = new PhotoService().displayPhotos();
        model.addAttribute("photoC", photo);
        return "photo";
    }
}


<!doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Welcome</title>
</head>
<body >
<nav>
    <div>
        <h2 style="text-align: center">Fill all the fields</h2>
    </div>
</nav>
<section>
    <form action="#" th:action="@{/gallery}" th:object="${photoC}" method="POST">
        <p>Enter ID: </p> <input type="number" th:field="*{id}"><br>
        <p>Enter photo name: </p> <input type="text" th:field="*{name}"><br>
        <p>Enter photo tag: </p> <input type="text" th:field="*{tag}"><br>
        <p>Enter photo quallity:</p> <input type="number" th:field="*{quality}"><br>
        <input type="file">
        <input type="submit" value="Submit"> <input type="reset" value="Cancel">
    </form>
</section>
</body>
</html>


Also I have services: GalleryService which is empty right now(now i'm kinda lost so that's the reason why it's empty) and PhotoService where I use getters to get info

Java
package com.gallery.galleryproject.service;

import com.gallery.galleryproject.controller.GalleryController;
import com.gallery.galleryproject.model.Photo;
import org.springframework.stereotype.Service;

@Service
public class GalleryService {

    public Photo getAllPhotos() {
        Photo photo = new Photo();

        return photo;
    }
}

package com.gallery.galleryproject.service;

import com.gallery.galleryproject.model.Photo;
import org.springframework.stereotype.Service;

@Service
public class PhotoService {

    public Photo displayPhotos() {
        Photo photos = new Photo();
        photos.getId();
        photos.getName();
        photos.getTag();
        photos.getQuality();
        return photos;
    }
}


What I have tried:

Stack, videos, spring tutorials, but probably I'm missing something.
Posted
Comments
Member 14160044 23-Feb-19 2:59am    
Can you put in your Photo model. You are creating an instance of the Photo model in the GalleryService, but i do not think, that the Photo model returns all Photos. Furthermore i would like to see the setters in the Photo model, because you used them in the PhotoService without any sense if they just return the elements.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900