Thymeleaf表单中的forEach不发布结果

时间:2019-03-27 15:33:59

标签: java spring spring-mvc thymeleaf

我的格式是:

<body>
    <form action="#" th:action="@{/}" th:object="${wrapper}" method="post">
        <tr th:each="w: ${wrapper.answers}">
            <td><input type="checkbox" th:value="${w.validWithNoun}"></td>
            <td th:text="${descriptor}"/>
            <td th:text="${w.noun}"/><br>
        </tr>
        <input type="submit" value="Submit" />
    </form>
</body>

但是当我转到帖子页面时,没有结果。

这是我的控制人:

@Controller
public class AskController {

    @GetMapping("/")
    public String ask(Model model) throws OWLOntologyCreationException {
        Word word = AskUser.getWords().iterator().next();
        String descriptorLabel = word.getDescriptorLabel();
        Map<String, OWLNamedIndividual> askWords = word.getAskWords();

        List<Answer> answers = new ArrayList<>();
        for (String n : askWords.keySet()) {
            Answer a = new Answer(n);
            answers.add(a);
        }
        AnswerWrapper wrapper = new AnswerWrapper(answers);

        model.addAttribute("wrapper", wrapper);
        model.addAttribute("descriptor", descriptorLabel);

        return "default";
    }

    @PostMapping("/")
    public String askSubmit(@ModelAttribute AnswerWrapper wrapper) {
        System.out.println("RESULTS");
        for (Answer a : wrapper.getAnswers()) {
            System.out.println(a.getNoun());
            System.out.println(a.getValidWithNoun());
        }
        return "result";
    }
}

我把照片放进去看看发生了什么,但是没有物体。原始页面上的列表中有两个对象。

堆栈跟踪为

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause

java.lang.NullPointerException: null
    at linguistics.AskController.askSubmit(AskController.java:43) ~[classes/:na]
...

0 个答案:

没有答案