一个表单绑定中的两个对象

时间:2018-02-08 13:30:15

标签: thymeleaf

如何获得Controller中的酒店价值?现在我只有null。不允许嵌套对象,因为html 5不允许嵌套。我在模板中有对象,但只有对象骑士带来了值,我还需要进一步编写控件值。我试图把对象但没有形成器,但我认为它现在没有意义。

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Add knight</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

    <link rel="stylesheet" th:href="@{/webjars/bootstrap/3.3.7-1/css/bootstrap.min.css}" />

    <script th:src="@{/webjars/jquery/3.2.1/jquery.min.js}"></script>
    <script th:src="@{/webjars/bootstrap/3.3.7-1/js/bootstrap.min.js}"></script>
</head>
<body>
<div class="container">

    <form class="form-horizontal" th:object="${knight}" th:action="@{/assignQuest}" th:method="post">

        <input type="hidden" th:field="*{id}"/>
        <input type="hidden" th:field="*{name}"/>
        <input type="hidden" th:field="*{age}"/>
        <input type="hidden" th:field="*{level}"/>

        <div class="form-group">
            <label class="control-label">Wykonaj zadanie</label>
            <select th:field="*{quest}">
                <option th:each="quest : ${notStartedQuests}"
                        th:value="${quest.id}"
                        th:text="${quest.description}">?</option>
            </select>
        </div>

        <input type="hidden" th:field="*{name}"/>
        <input type="hidden" th:field="*{id}"/>
        <ul>

            <label class="control-label">Zamiesszkaj w hotelu</label>
            <li th:each="hotel : ${hotels}">
                <input type="radio" th:field="*{id}" th:value="${hotel.id}" />
                <label th:for="${hotel.name}" th:text="${hotel.name}"></label>
            </li>
        </ul>

        <div class="row">
            <button type="submit" class="btn btn-default">Wyslij rycerza</button>
        </div>
    </form>
</div>
</body>
</html>

QuestController:

import java.util.List;

@Controller
public class QuestController {

    @Autowired
    KnightService knightService;

    @Autowired
    QuestService questService;

    @Autowired
    HotelService hotelService;
    @RequestMapping("/assignQuest")
    public String assignQuest(@RequestParam("knightId") Integer id, Model model) {
        Knight knight = knightService.getKnight(id);
        List<Quest> notStartedQuests = questService.getAllNotStartedQuests();
        List<Hotel> hotels = hotelService.getAllHotels();
        model.addAttribute("knight", knight);
        model.addAttribute("notStartedQuests", notStartedQuests);
        model.addAttribute("hotels", hotels);
        return "assignQuest";
    }

    @RequestMapping(value = "/assignQuest", method = RequestMethod.POST)
    public String assignQuest(Knight knight, Hotel hotels, BindingResult result) {
        System.out.println(result);
        System.out.println(hotels);
        knightService.updateKnight(knight);
//        Quest quest = knight.getQuest();
//        questService.update(quest);
        return "redirect:/knights";
    }

}

0 个答案:

没有答案