Jak wyciągnąć id z selecta i otrzymać wartość w kontrolerze?

Odpowiedz Nowy wątek
2016-05-20 14:47
0

Witam,
Pytanie jak w temacie. Jak powinno się to zrobić prawidłowo? Co prawda to co napisałem działa i zapisuje dane do bazy, ale przy walidacji dostaję informację o błędzie.

[Field error in object 'showing' on field 'movie': rejected value [2]; codes [typeMismatch.showing.movie,typeMismatch.movie,typeMismatch.com.app.models.Movie,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [showing.movie,movie]; arguments []; default message [movie]]; default message [Failed to convert property value of type [java.lang.String] to required type [com.app.models.Movie] for property 'movie'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@javax.persistence.OneToOne com.app.models.Movie] for value '2'; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: Provided id of the wrong type for class com.app.models.Movie. Expected: class java.lang.Long, got class java.lang.String; nested exception is java.lang.IllegalArgumentException: Provided id of the wrong type for class com.app.models.Movie. Expected: class java.lang.Long, got class java.lang.String]]

dzial.PNG

    @RequestMapping(value = {"/add"}, method = RequestMethod.POST)
    public ModelAndView processAddCinemaForm(@Valid @ModelAttribute("showing") Showing showing, BindingResult result, HttpServletRequest request) {
        Long idOfMovie = Long.parseLong(result.getFieldValue("movie").toString());
        showing.setMovie(movieService.findOne(idOfMovie));
        if(result.hasErrors()){
            System.out.println(result.getAllErrors());
        }
        if (showing.getId() != null) {
            System.out.println(showing.getMovie());
            Showing newShowing = showingService.findOne(showing.getId());
            newShowing = showing;
            showingService.save(showing);
        } else {
            showingService.save(showing);
        }
        return new ModelAndView("redirect:/showing/all");
    }

<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      layout:decorator="layout">

<div layout:fragment="content">
    <form action="#" th:action="@{/showing/add}" th:object="${showing}" method="post">
        <br/><input type="hidden" th:field="*{id}" class="form-control" placeholder="Id"/>
        <select th:field="*{movie}" class="dropdown">
            <option th:each="movie : ${movies}"
                    th:value="${movie.id}"
                    th:text="${movie.title}">Wireframe
            </option>
        </select>

        <br/>
        <button type="submit" class="btn btn-md btn-primary pull-right">Save</button>
    </form>
</div>
</html>

Pozostało 580 znaków

2016-05-21 00:19
0

Provided id of the wrong type for class com.app.models.Movie. Expected: class java.lang.Long, got class java.lang.String; nested exception is java.lang.IllegalArgumentException: Provided id of the wrong type for class com.app.models.Movie. Expected: class java.lang.Long, got class java.lang.String]]

mówi panu to coś ?

 Showing newShowing = showingService.findOne(showing.getId());

je suis @niezdecydowany

Pozostało 580 znaków

2016-05-21 11:05
0

@maryiusz rozumiem komunikat błędu, ale moje getId() zwraca Longa, więc nie wiem dlaczego piszę niby, że dostarczam Stringa.

Pozostało 580 znaków

2016-05-21 11:38
0

wrzuć DTO i service, chce zobaczyć sygnatury metod.


je suis @niezdecydowany

Pozostało 580 znaków

2016-05-21 13:22
0

package com.app.models;

import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import java.util.Date;

@Getter
@Setter
@Entity
public class Showing {
    @Id
    @GeneratedValue
    private Long id;

    @Temporal(TemporalType.DATE)
    private Date date;

    @Temporal(TemporalType.TIME)
    private Date time;

    @OneToOne
    private Cinema cinema;

    @OneToOne
    private CinemaHall cinemaHall;

    @OneToOne
    private Movie movie;

    public Showing() {
    }
}
package com.app.services.impl;

import com.app.models.Showing;
import com.app.repositories.ShowingRepository;
import com.app.services.ShowingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShowingServiceImpl implements ShowingService {
    @Autowired
    private ShowingRepository showingRepository;

    @Override
    public Showing save(Showing showing) {
        return showingRepository.save(showing);
    }

    @Override
    public Showing findOne(Long id) {
        return showingRepository.findOne(id);
    }

    @Override
    public List<Showing> findAll() {
        return showingRepository.findAll();
    }

    @Override
    public void delete(Showing showing) {
        showingRepository.delete(showing);
    }
}

Pozostało 580 znaków

2016-05-21 20:02

w exceptionie mowa jest o Movie

showing.setMovie(movieService.findOne(idOfMovie));


je suis @niezdecydowany

Pozostało 580 znaków

Odpowiedz
Liczba odpowiedzi na stronę

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