Witam,
Próbuję zrobić formularz, w którym z bazy będę mógł wybrać film i kino z dropdownlist. Napisałem dropdowna, ale wyświetlają się w nim dodatkowe rzeczy, mimo że przesłoniłem metodę toString(). Jak to zrobić, aby można było wybrać film i kino, a następnie prawidłowo zapisać do bazy encje Showing.
Encje:
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.controllers;
import com.app.models.Cinema;
import com.app.models.Movie;
import com.app.models.Showing;
import com.app.services.CinemaService;
import com.app.services.MovieService;
import com.app.services.ShowingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
import java.util.Map;
import java.util.Optional;
/**
* Created by Marcin on 19.05.2016.
*/
@Controller
@RequestMapping("/showing")
public class ShowingController {
@Autowired
private ShowingService showingService;
@Autowired
private MovieService movieService;
@Autowired
private CinemaService cinemaService;
@RequestMapping(value = {"/add", "/add/{id}"})
public ModelAndView getAddCinemaForm(Map<String, Object> modelMap, @PathVariable Optional<Long> id) {
List<Movie> movies = movieService.findAll();
List<Cinema> cinemas = cinemaService.findAll();
modelMap.put("movies", movies);
modelMap.put("cinemas", cinemas);
Showing showing = null;
if (id.isPresent()) {
showing = showingService.findOne(id.get());
if (showing != null) {
modelMap.put("showing", showing);
return new ModelAndView("/showing/add");
} else {
return new ModelAndView("redirect:/showing/all");
}
}
showing = new Showing();
modelMap.put("showing", showing);
return new ModelAndView("/showing/add");
}
@RequestMapping(value = {"/add"}, method = RequestMethod.POST)
public ModelAndView processAddCinemaForm(@ModelAttribute("showing") Showing showing) {
if (showing.getId() != null) {
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"/>-->
<!--<br/><input type="text" th:field="*{movie}" class="form-control" placeholder="Name"/>-->
<!--<br/><input type="text" th:field="*{cinema}" class="form-control" placeholder="City"/>-->
<!--<br/><input type="text" th:field="*{cinemaHall}" class="form-control" placeholder="Street"/>-->
<!--<br/><input type="number" th:field="*{date}" class="form-control" placeholder="Number"/>-->
<!--<br/><input type="number" th:field="*{time}" class="form-control" placeholder="Number"/>-->
<br/>
<!--<select th:field="*{movie}">-->
<!--<option th:each="type : ${movies}" th:value="${movie.id}" th:text="${movie.title}">Wireframe</option>-->
<!--</select>-->
<select th:field="*{movie}">
<option th:each="movie : ${movies}"
th:value="${movie}"
th:text="#{${'seedstarter.type.' + movie}}">Wireframe</option>
</select>
<br/>
<button type="submit" class="btn btn-md btn-primary pull-right">Save</button>
</form>
</div>
</html>
Dropdownlist