Witam,
Mam problem jak w temacie. Niby mam @PathVariable Long, ale nie wiem czemu tutaj powstaje błąd konwersji.
Treść błędu:
8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to bind request element: org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type [java.lang.String] to required type [java.lang.Long]; nested exception is java.lang.NumberFormatException: For input string: "{idCinema}"
package com.app.controllers;
import com.app.models.Cinema;
import com.app.models.CinemaHall;
import com.app.services.CinemaService;
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.Optional;
@Controller
@RequestMapping("/hall")
public class CinemaHallController {
@Autowired
private CinemaService cinemaService;
@RequestMapping(value = {"/add/{idCinema}"})
public ModelAndView getAddMovieForm(Model model, @PathVariable Long idCinema) {
CinemaHall cinemaHall = new CinemaHall();
model.addAttribute("cinemaHall", cinemaHall);
return new ModelAndView("/cinema/addHall");
}
@RequestMapping(value = {"/add/{idCinema}"}, method = RequestMethod.POST)
public ModelAndView processAddMovieForm(@ModelAttribute("cinemaHall") CinemaHall cinemaHall, @PathVariable Long idCinema) {
Cinema cinema = cinemaService.findOne(idCinema);
if(cinema!=null){
cinemaService.addCinemaHall(cinemaHall, cinema.getId());
}
return new ModelAndView("redirect:/cinema/all");
}
}
Widok:
<!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="@{/hall/add/{idCinema}}" th:object="${cinemaHall}" method="post">
<br/><input type="hidden" th:field="*{id}" class="form-control" placeholder="Id"/>
<br/><input type="text" th:field="*{name}" class="form-control" placeholder="Name"/>
<br/><input type="number" th:field="*{numberOfSeats}" class="form-control" placeholder="Number of Seats"/>
<br/>
<button type="submit" class="btn btn-md btn-primary pull-right">Save</button>
</form>
</div>
</html>
Adres url, który wyświetla formularz:
` <a th:href="@{/hall/add/{idCinema}/(idCinema=${cinema.id})}" class="btn btn-primary">Details</a>`
Adres url wyświetlonego formularza:
![co jest.PNG](//static.4programmers.net/uploads/attachment/9467855255722757c11c7d.png)
Adres url po post:
![poPost.PNG](//static.4programmers.net/uploads/attachment/102496093157227580e803c.png)
Treść błędu: