Thymeleaf从视图

时间:2018-03-26 08:47:29

标签: java spring thymeleaf

我在使用控制器从html表单访问数据时遇到问题。当我尝试从html获取数据时,我获得了NullPointerException。 当我尝试在html视图中打印用户名和密码时,没有问题,但是当我尝试在控制器中执行此操作时,除了我添加到对象的id之外,数据是空的。

控制器:

@RequestMapping(value = "/kpi", method = RequestMethod.POST)
public ModelAndView showKPI(@Valid Kpi kpi, 
    BindingResult bindingResult, Model model) throws Exception {

    if(bindingResult.hasErrors()) {
        System.out.println("errors in binding");
    }
    model.addAttribute("kpi", kpi);
    System.out.println(kpi.getId());
    System.out.println(kpi.getPost().getUsername()); // Here is the NullPointer
    Map<String, Kpi> map = new HashMap<String, Kpi>();

    return new ModelAndView("kpi.html");
}

@RequestMapping(value = "/logged", method = RequestMethod.POST)
public ModelAndView addNewPost(@Valid Post post, 
    BindingResult bindingResult, Model model) throws Exception {

    if (bindingResult.hasErrors()) {
        return new ModelAndView("index.html");
    }

    ... 
    List<Entry<String, MyValue>> map = handshake_parser.getAllSprints();

    /*Loop for printing all information about sprints*/
    for(Map.Entry<String, MyValue> entry : map) {
        System.out.println(entry.getKey() + " " + entry.getValue().getName() + " : " + entry.getValue().getStartDate() + " to " + entry.getValue().getEndDate());
        String id = entry.getKey();
    }
    model.addAttribute("list", map);
    Kpi kpi = new Kpi();
    kpi.setPost(post);
    model.addAttribute("kpi", kpi);
    return new ModelAndView("result.html");
}

result.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Spring Boot and Thymeleaf example</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <h3>List of all sprints in board:</h3>
    <table class="table table-striped">
        <tr>
            <th>Name</th>
            <th>ID</th>
            <th>StarDate</th>
            <th>EndDate</th>
        </tr>
        <tr th:each="entry : ${list}">
            <td th:text="${entry.value.name}">name</td>
            <td th:text="${entry.key}">id</td>
            <td th:text="${entry.value.startDate}">startDate</td>
            <td th:text="${entry.value.endDate}">endDate</td>
        </tr>
    </table>
    <form action="#" th:action="@{/kpi}" th:object="${kpi}" method="post">
        <select th:field="*{id}">
            <option th:each="entry : ${list}"
                    th:value="${entry.key}"
                    th:text="${entry.value.name}">
            </option>
        </select>
        <div th:text="kpi.post.username"></div> <!-- Here is printing it well -->
        <button type="submit">See KPI</button>
    </form>
</body>
</html>
  

servlet [dispatcherServlet]的Servlet.service()与path []的上下文发生异常[请求处理失败;具有根本原因的嵌套异常是java.lang.NullPointerException]   java.lang.NullPointerException:null

如何从以前在控制器中声明的视图中检索和使用数据?

1 个答案:

答案 0 :(得分:0)

1)您必须在方法参数中添加@ModelAttribute注释:

public ModelAndView showKPI(@Valid @ModelAttribute Kpi kpi, BindingResult bindingResult, Model model) throws Exception {
    // ...
}

2)HTML页面中没有用户名输入。你必须添加如下内容:

<form action="#" th:action="@{/kpi}" th:object="${kpi}" method="post">
    <select th:field="*{id}">
        <option th:each="entry : ${list}"
                th:value="${entry.key}"
                th:text="${entry.value.name}">
        </option>
    </select>
    <div th:text="kpi.post.username"></div>
    <!-- THIS -->
    <input th:field="*{post.username}" th:value="${kpi.post.username}" />
    <button type="submit">See KPI</button>
</form>