使用Spring MVC将数据从Thymeleaf表单保存到数据库的问题

时间:2018-09-18 06:28:07

标签: java spring spring-mvc spring-data-jpa thymeleaf

我刚刚开始学习Spring MVC。我尝试将一些数据从Thymeleaf表单保存到存储库,这扩展了CrudRepository。不幸的是,数据没有显示。

当我进入结果页面时,我看到了使用过的ID,但没有键入要形成的数据。错误在哪里?

enter image description here

这里是控制器

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

`模型

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

存储库

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 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>

并从结果模板中提取:

<td th:text="${customer.id}">customer ID</td>
<td th:text="${customer.firstName}">first name</td>
<td th:text="${customer.lastName}">last name</td>

3 个答案:

答案 0 :(得分:0)

尝试使用

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

有关更多信息,请参见我的问题Store pictures in H2 database spring boot thymleaf

答案 1 :(得分:0)

另一种方式,使用以下代码:

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

对于结果模板:

<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>

答案 2 :(得分:0)

尝试:

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