PathVariable - nieudana konwersja ze String do Long

0

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

No bo ten adres URL po post (localhost:8080/hall/add/%7BidCinema%7D) pasuje do mapowania /add/{idCinema} który się spodziewa longa jako {idCinema}.
Ciężko zamienić %7BidCinema%7D na long :)

0

@Swr to chciałbym w post też mieć taki adres url: http://localhost:8080/hall/add/1 jak w get? Głównie to w POST potrzebuje dostać tą jedynkę dla przykładu. Jak to zrobić?

0

Podejrzyj prosze jakimis narzedziami developerskimi (firebug czy defaultowe f12) jak wyglada URL action w formularzu. Spodziewam sie ze wyglada tak: localhost:8080/hall/add/{idCinema}

Ten kawalek musi byc pewnie zly (nie znam Thymeleaf): @{/hall/add/{idCinema}}

Ponadto, idCinema wyglada jak String w Twoim wypadku ale moge sie mylic.

0

Kod z przeglądarki po wygenerowaniu:

<form action="/hall/add/{idCinema}" method="post" enctype="application/x-www-form-urlencoded">
        <br><input type="hidden" class="form-control" placeholder="Id" id="id" name="id" value="">
        <br><input type="text" class="form-control" placeholder="Name" id="name" name="name" value="">
        <br><input type="number" class="form-control" placeholder="Number of Seats" id="numberOfSeats" name="numberOfSeats" value="0">
        <br>
        <button type="submit" class="btn btn-md btn-primary pull-right">Save</button>
    </form>

Tutaj mam coś podobnego i działa: https://github.com/giecmarcin/CinemaT/blob/master/src/main/java/com/app/controllers/CinemaController.java więc się wzorowałem na tym co wcześniej zrobiłem i taka niespodzianka.

1
<form action="/hall/add/{idCinema}

No jesli nie podmieniasz idCinema chociazby jakims javascriptem to ciezko raczej. Poleci {idCinema} zamiast jakiegos ID.

Nie wiem czy to u Ciebie powinno byc to jakies dynamiczne czy nie ale odpowiedz to:
zly action URL dla formularza. Czy to poprzez zle wygenerowanie (jakies bledne uzycie nawiasow klamrowych czy cos w tym stylu) czy ogolnie poprzez blad w zalozeniach tutaj.

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