Problem z dostaniem się do pola w polu Thymeleaf

0

Witam a więc mój problem polega na tym ze potrzebuje dodać Samochód który zawiera pole obce łączące obiekt relacją @OneToOne z innym obiektem. Problem polega na tym, że nie wiem jak za pomocą thymeleafa dodać to pole jęzeli jest typu obiektowego. Probuje dodać numer z listy i wyskakuje mi błąd Field error in object 'car' on field 'parkingSpace': rejected value [10]; codes [typeMismatch.car.parkingSpace,typeMismatch.parkingSpace,typeMismatch.pl.pw.Parking.parking.Parking,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [car.parkingSpace,parkingSpace]; arguments []; default message [parkingSpace]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'pl.pw.Parking.parking.Parking' for property 'parkingSpace'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'pl.pw.Parking.parking.Parking' for property 'parkingSpace': no matching editors or conversion strategy found]] Mój kod wygląda tak:

@Entity
@Table(name="cars")
public class Car {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotBlank(message = "Wpisz marke")
    @Column

    private String brand;

    @NotBlank(message = "Wpisz model")
    @Column(name = "model")
    private String modelCar;


    public String getRegistrationNumber() {
        return registrationNumber;
    }

    public void setRegistrationNumber(String registrationNumber) {
        this.registrationNumber = registrationNumber;
    }

    @NotBlank(message = "Wpisz numer rejestracyjny")
    @Column(name = "rejestracja")
    private String registrationNumber;

    @OneToOne
    @JoinColumn(name = "parking_space__id"
            , unique=true)
    private Parking parkingSpace;

@Entity
@Table
public class Parking {

  public final static long parkingSpaces = 50;

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;
  private String parkingSpace;



 public Parking() {
  }

  public Parking(Long id, String parkingSpace) {
     this.id = id;
     this.parkingSpace = parkingSpace;
  }

  public static long getParkingSpaces() {
     return parkingSpaces;
  }

  public Long getId() {
     return id;
  }

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

  public String getParkingSpace() {
     return parkingSpace;
  }

  public void setParkingSpace(String parkingSpace) {
     this.parkingSpace = parkingSpace;
  }

  @Override
  public String toString() {
     return "Parking{" +
             "parkingSpace=" + parkingSpace +
             '}';
  }```html

}


<tr>
            <td><label th:text="#{msg.registrationNumber}" /></td>
        <td><select th:field="*{parkingSpace}">
            <option th:each="i : ${#numbers.sequence(0, 50)}" th:value="${i}" th:text="${i}">
            </option>
        </select>
        </td>
        </tr>
            <td><input type="submit" value="Submit" /></td>
        </tr>
0

Czy da się w ogóle dodać w ten sposób pole typu klasowego ?? czy może po prostu zrobić wypełnioną listę i ją wstrzyknąć ?

0
witu122 napisał(a):

Witam a więc mój problem polega na tym ze potrzebuje dodać Samochód który zawiera pole obce łączące obiekt relacją @OneToOne z innym obiektem. Problem polega na tym, że nie wiem jak za pomocą thymeleafa dodać to pole jęzeli jest typu obiektowego. Probuje dodać numer z listy i wyskakuje mi błąd Field error in object 'car' on field 'parkingSpace': rejected value [10]; codes [typeMismatch.car.parkingSpace,typeMismatch.parkingSpace,typeMismatch.pl.pw.Parking.parking.Parking,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [car.parkingSpace,parkingSpace]; arguments []; default message [parkingSpace]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'pl.pw.Parking.parking.Parking' for property 'parkingSpace'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'pl.pw.Parking.parking.Parking' for property 'parkingSpace': no matching editors or conversion strategy found]] Mój kod wygląda tak:

@Entity
@Table(name="cars")
public class Car {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotBlank(message = "Wpisz marke")
    @Column

    private String brand;

    @NotBlank(message = "Wpisz model")
    @Column(name = "model")
    private String modelCar;


    public String getRegistrationNumber() {
        return registrationNumber;
    }

    public void setRegistrationNumber(String registrationNumber) {
        this.registrationNumber = registrationNumber;
    }

    @NotBlank(message = "Wpisz numer rejestracyjny")
    @Column(name = "rejestracja")
    private String registrationNumber;

    @OneToOne
    @JoinColumn(name = "parking_space__id"
            , unique=true)
    private Parking parkingSpace;

@Entity
@Table
public class Parking {

  public final static long parkingSpaces = 50;

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;
  private String parkingSpace;



 public Parking() {
  }

  public Parking(Long id, String parkingSpace) {
     this.id = id;
     this.parkingSpace = parkingSpace;
  }

  public static long getParkingSpaces() {
     return parkingSpaces;
  }

  public Long getId() {
     return id;
  }

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

  public String getParkingSpace() {
     return parkingSpace;
  }

  public void setParkingSpace(String parkingSpace) {
     this.parkingSpace = parkingSpace;
  }

  @Override
  public String toString() {
     return "Parking{" +
             "parkingSpace=" + parkingSpace +
             '}';
  }```html

}


<tr>
            <td><label th:text="#{msg.registrationNumber}" /></td>
        <td><select th:field="*{parkingSpace}">
            <option th:each="i : ${#numbers.sequence(0, 50)}" th:value="${i}" th:text="${i}">
            </option>
        </select>
        </td>
        </tr>
            <td><input type="submit" value="Submit" /></td>
        </tr>

<!DOCTYPE html>

<html xmlns:th="http://www.thymeleaf.org">

<head lang="pl">
    <title>ADD CLIENT</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>

<body>

<h1><p th:utext="#{parking.welcome}"></h1>

</p>

<form action="#" th:action="@{/car/add}" th:object="${car}" method="post">
    <table border="1">

        <tr>
            <td><label th:text="#{msg.brand}" /></td>
            <td><input type="text" th:field="*{brand}" /></td>
        </tr>
        <tr>
            <td><label th:text="#{msg.modelCar}" /></td>
            <td><input type="text" th:field="*{modelCar}" /></td>
        </tr>
        <tr>
            <td><label th:text="#{msg.registrationNumber}" /></td>
            <td><input type="text" th:field="*{registrationNumber}" /></td>
        </tr>
        <tr>
            <td>Wybierz Klienta:</td>
            <td>
                <select th:field="*{client}">
                    <option value=""> -- </option>
                    <option th:each="client : ${clients}"
                            th:value="${client.id}"
                            th:utext="${client.lastName}"/>
                </select>
        </tr>
        
        <tr>
            <td><label th:text="#{msg.registrationNumber}" /></td>
        <td><select th:field="*{parkingSpace}">
            <option th:each="i : ${#numbers.sequence(0, 50)}" th:value="${i}" th:text="${i}">
            </option>
        </select>
        </td>
        </tr>
            <td><input type="submit" value="Submit" /></td>
        </tr>
    </table>
</form>
</body>

</html>

tak wygląda cały thymeleaf. W ogóle może macie dla mnie jakąś inna propozycje zeby zrobić to na liście Integerów ? bo może w sumie nie potrzebuje do tego dodatkowej encji ?

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