Добрый день.
Не могу получить значение чекбоксов с формы.
View:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<title>Order</title>
</head>
<body>
<h1>Сделайте Ваш заказ:</h1>
<form method="post" th:object="${order}">
<p>
<input type="text" th:field="*{name}"/>
</p>
<p>
<input type="checkbox" th:value="${burger.getName()}" th:field="*{dishes}"/>
<span th:text="${burger.getName()}">DISHES</span>
</p>
<p>
<input type="checkbox" th:value="${cola.getName()}" th:field="*{dishes}"/>
<span th:text="${cola.getName()}">DISHES</span>
</p>
<p>
<input type="checkbox" th:value="${chicken.getName()}" th:field="*{dishes}"/>
<span th:text="${chicken.getName()}">DISHES</span>
</p>
<p>
<input type="checkbox" th:value="${tea.getName()}" th:field="*{dishes}"/>
<span th:text="${tea.getName()}">DISHES</span>
</p>
<input type="submit" value="ok"/>
</form>
</body>
</html>
Контроллер:
package com.example.demo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
@Controller
@SessionAttributes("user")
public class HomeController {
@GetMapping("/")
public String showHomePage()
{
return "home";
}
@PostMapping("/")
public String getOrder(Order order, @ModelAttribute User user)
{
System.out.println(order.getDishes().size());
System.out.println(order.getDate());
System.out.println(order.getName());
user.addOrders(order);
return "orders";
}
@ModelAttribute
public void AddDishes(Model model)
{
Dish burger = new Dish("Burger");
Dish cola = new Dish("Cola");
Dish chicken = new Dish("Chicken");
Dish tea = new Dish("Tea");
model.addAttribute("burger", burger);
model.addAttribute("cola", cola);
model.addAttribute("chicken", chicken);
model.addAttribute("tea", tea);
}
@ModelAttribute(name="order")
public Order order()
{
return new Order();
}
@ModelAttribute(name="user")
public User user()
{
return new User();
}
}
Классы:
public class Dish {
String name;
Dish(String name)
{
this.name=name;
}
public Dish(){}
public String getName()
{
return this.name;
}
}
public class Order {
private Date date = new Date();
private String name;
private List<Dish> dishes = new ArrayList<>();
public List<Dish> getDishes()
{
return this.dishes;
}
public Date getDate()
{
return date;
}
public void SetDish(Dish dish)
{
this.dishes.add(dish);
}
public void SetDishes(List<Dish> dishes)
{
this.dishes=dishes;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name=name;
}
}
@Component
public class ConverterDishesByName implements Converter <String, Dish>
{
private Map<String, Dish> dishesMap = new HashMap<>();
public ConverterDishesByName()
{
dishesMap.put("burger", new Dish("Burger"));
dishesMap.put("cola", new Dish("Cola"));
dishesMap.put("chicken", new Dish("Chicken"));
dishesMap.put("tea", new Dish("Tea"));
}
@Override
public Dish convert(String name)
{
return dishesMap.get(name);
}
}