У меня проблема с пониманием, вероятно, простой вещи. Я не знаю, почему моя программа возвращает carId = 0, когда я пытаюсь изменить свой существующий автомобиль. Я знаю об этом, потому что я проверил в режиме отладки. Наконец, моя программа не может добавлять и изменять какие-либо элементы для шоу carList в веб-приложении. Любая идея? Я представляю фрагменты своего кода ниже:
CarGui.java
@PostMapping("/cars-web/modifyCar")
public String modifyCar(@ModelAttribute Car updateCar) {
carService.updateAllFieldsForCar(updateCar);
return "redirect:/cars-web";
}
@PostMapping("/cars-web/modifyBrand")
public String modifyBrandCar(@ModelAttribute Car car) {
carService.modifyChoiceFieldCar(car.getCarId(), car.getBrand());
return "redirect:/cars-web";
}
CarServiceImplementation.java
@Override
public boolean updateAllFieldsForCar(Car car) {
Optional<Car> found = carList.stream().filter(car1 -> car1.getCarId() == car.getCarId()).findFirst();
if (found.isPresent()) {
carList.remove(found.get());
Long id = setNewIdForCar();
car.setCarId(id);
return carList.add(car);
}
return false;
}
@Override
public boolean modifyChoiceFieldCar(long id, String brand) {
Optional<Car> carOptional = carList.stream().filter(car -> car.getCarId() == id).findFirst();
if (carOptional.isPresent()) {
carOptional.get().setBrand(brand);
return true;
}
return false;
}
cars-web.html
<form th:action="@{/cars-web/modifyCar}" th:object="${modifyCar}" method="post">
<p>Id: <select name="id2">
<option th:each="id : ${readId}"
th:value="${id}" th:text="${id}"></option>
</select></p>
<p>Brand: <input type="text" th:field="*{brand}"></p>
<p>Model: <input type="text" th:field="*{model}"></p>
<p>Color: <select th:field="*{color}">
<option th:each="colors : ${color}" th:value="${colors}" th:text="${colors.getColor()}"></option>
</select></p>
<p><input type="submit" value="Modify Car"></p>
</form>
<form th:action="@{/cars-web/modifyBrand}" th:object="${modifyBrand}" method="post">
<p>Id: <select name="id3">
<option th:each="car : ${carList}"
th:value="${car.getCarId()}" th:text="${car.getCarId()}"></option>
</select></p>
<p>Brand: <input type="text" th:field="*{brand}"></p>
<p><input type="submit" value="Modify Brand Car"></p>
</form>
Вы можете увидеть больше кода в моей ветке разработки на сайте: https://github.com/rafpop/Spring-Training-Week-3-4/tree/develop