Java Spring - problem z przesyłaniem obiektu z pliku HTML do metody

0

Witam. Robię prosty projekt w Springu. Na jednej stronie mam listę kategorii. Chciałbym po kliknięciu na link z nazwą kategorii przejść do szablonu wyświetlającego elementy należące do tej kategorii. Mój problem polega na tym, że pojawia mi się w przeglądarce błąd:

Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "home" - line 30, col 41)

Chciałbym prosić o pomoc w znalezieniu błędu w kodzie. Z góry dziękuję za każdą pomoc.

To jest mój kod w Javie:

@GetMapping("/")
public String homePage( Model model )
{
    List<Category> allCategories = categoryRepository.findAll();
    model.addAttribute("categories", allCategories);
    return "home";
}

@RequestMapping("/singleCategory")
    public String singleCategory( Category category, Model model )
    {
        model.addAttribute( "category", category );
        return "singleCategoryH";

    }

A tutaj plik HTML:

<table>
    <thead>
    <tr>
        <th>Category</th>
    </tr>
    </thead>
    <tbody>
    <tr th:each="c: ${categories}">

        <td>
            <form th:action = "@{/singleCategory}" th:object ='${category}'  method="post">
                <input type = "hidden"  th:field = "*{name}" value="${c.name}"> <!--30 linijka-->
                <input type = "hidden" th:field = "*{id}"  value="${c.id}">
                <button type="submit" class="btn btn-link">
                    <span th:text="${c.name}"></span>
                </button>
            </form>
        </td>

    </tr>
    </tbody>
</table>
0

Może pokaż jeszcze tę klasę Category?

0

Dorzucam klasę Category:

@Entity
public class Category {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Column(unique = true)
    private String name;
    @OneToMany
    @JoinColumn(name = "category_id")
    private List <Recipe> recipes;

    public List<Recipe> getRecipes() {
        return recipes;
    }

    public void setRecipes(List<Recipe> recipes) {
        this.recipes = recipes;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Category(String name) {
        this.name = name;
    }

    public Category() {
    }
}

0

ciapek ' a ''?

1

W pliku html próbujesz się odwołać do obiektu category, którego nie ma w modelu:
<form th:action = "@{/singleCategory}" th:object ='${category}' method="post">

Albo dodaj go do modelu w kontrolerze albo usuń z pliku html. Dodatkowo usuń th:field i zostaw tylko th:value. Powinno działać.

1

Z tego co rozumiem to chcesz z pliku home.html przekazać do metody singleCategory obiekt pobrany z listy categories. Spróbuj tak:

  @GetMapping("/")
  public String homePage(Model model) {
    List<Category> allCategories = categoryRepository.findAll();
    model.addAttribute("category", new Category());
    model.addAttribute("categories", allCategories);
    return "home";
  }

  @PostMapping("/singleCategory")
  public String singleCategory(@ModelAttribute Category category, Model model) {
    model.addAttribute("category", category);
    return "singleCategoryH";
  }

natomiast w pliku home.html

<table>
    <thead>
    <tr>
        <th>Category</th>
    </tr>
    </thead>
    <tbody>
    <tr th:each="c: ${categories}">

        <td>
            <form th:action = "@{/singleCategory}" th:object ='${category}'  method="post">
                <input type = "hidden" th:value="${c.name}" name="name"> <!--30 linijka-->
                <input type = "hidden" th:value="${c.id}" name="id">
                <button type="submit" class="btn btn-link">
                    <span th:text="${c.name}"></span>
                </button>
            </form>
        </td>

    </tr>
    </tbody>
</table>

1 użytkowników online, w tym zalogowanych: 0, gości: 1