0

I've just started learning Spring MVC. I try to save some data from the Thymeleaf form to repository, which extends CrudRepository. Unfortunately, the data doesn't display.

When I go to the results page I see used IDs but no data typed to form. Where is the mistake?

enter image description here

Here is a Controller

package com.jtm.twiservice.controller;
import com.jtm.twiservice.Main;
import com.jtm.twiservice.model.Customer;
import com.jtm.twiservice.repository.CustomerRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
public class CustomerClientController {
private static final Logger myLog = LoggerFactory.getLogger(Main.class);

@Autowired
private CustomerRepository customerRepository;

@GetMapping("/customer")
public String customerForm(Model model) {
    model.addAttribute("customer", new Customer());
    return "customer";
}

@PostMapping("/customer")
public String createCustomer(Customer customer, Model model) {
    customerRepository.save(customer);
    return "customer";
}

@GetMapping("/customers")
public String getCustomers(Model model) {
    model.addAttribute("customers", customerRepository.findAll());
    return "customers;
}
}

`Model:

package com.jtm.twiservice.model;
import org.springframework.beans.factory.annotation.Autowired;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Customer {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String firstName;
private String lastName;
private String email;
private String schoolForm;

public Customer() {}

public Customer(String firstName, String lastName, String email, String schoolForm) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.email = email;
    this.schoolForm = schoolForm;
}

@Override
public String toString() {
    return String.format(
            "Customer[id=%d, firstName='%s', lastName='%s', email='%s', schoolForm='%s']",
            id, firstName, lastName, email, schoolForm);
}

public Long getId() {
    return id;
}

public String getFirstName() {
    return firstName;
}

public String getLastName() {
    return lastName;
}

public String getEmail() {
    return email;
}

public String getSchoolForm() {
    return schoolForm;
}
}

repository:

package com.jtm.twiservice.repository;

import com.jtm.twiservice.model.Customer;
import org.springframework.data.repository.CrudRepository;

public interface CustomerRepository extends CrudRepository<Customer, Long> { }

form extract:

        <form action="#" th:action="@{/customer}" th:object="${customer}" method="post">
            <p class="text"><label>first name:</label>  <input type="text" th:field="*{firstName}" /></p>
            <p class="text"><label>last name:</label> <input type="text" th:field="*{lastName}" /></p
            <p class="text"><label>school form:</label> <input type="text" th:field="*{schoolForm}" /></p>
            <div class="text">email: <input type="text" th:field="*{email}" /></div>
            <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /> </p>

        </form>

And extract from results template:

<td th:text="${customer.id}">customer ID</td>
<td th:text="${customer.firstName}">first name</td>
<td th:text="${customer.lastName}">last name</td>
Phenomenal One
  • 2,501
  • 4
  • 19
  • 29
James121
  • 81
  • 10

4 Answers4

0

Try to use

List<Customer> customers = new customerRepository.findAll();
Model.addAttribute("c" , customers);

See my question for more information Store pictures in H2 database spring boot thymleaf

Vishal Torne
  • 366
  • 5
  • 24
0

Another way, with this code:

  model.addAttribute("customers", customerRepository.findAll());

for result template:

<th:block th:each="customer : ${customers}">
  <td th:text="${customer.id}">customer ID</td>
  <td th:text="${customer.firstName}">first name</td>
  <td th:text="${customer.lastName}">last name</td>
</th:block>
Falcon
  • 338
  • 3
  • 9
0

try this :

@PostMapping("/customer")
public String createCustomer(Customer customer,BindingResult result)
{
    if (result.hasErrors())
 {
            return "your error page in here";
        }
    customerRepository.save(customer);
    return "customer";
}
0

Although this post is already old, I just stumbled upon it when debugging my own code; my answer may still help someone in the future.

I believe I had a similar issue, where the Thymeleaf form didn't actually assign values to the entity's fields. For me the issue was that the entity had no setters for its fields - it looks like you have that problem here as well.

Kyera
  • 1