[英]Thymeleaf retriving data from view
I have a problem with accessing to data from html form with controller. 我在使用控制器从html表单访问数据时遇到问题。 I'm getting a NullPointerException
when I try to get data from html. 当我尝试从html获取数据时,出现NullPointerException
。 When I try to print username and password in html view, there is no problem, but when I try to do this in controller, the data is empty except the id that I add to object. 当我尝试在html视图中打印用户名和密码时,没有问题,但是当我尝试在控制器中执行此操作时,除了我添加到对象的id之外,数据为空。
Controller: 控制器:
@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 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.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; 路径为[]的Servlet [dispatcherServlet]的Servlet.service()抛出异常[请求处理失败; nested exception is java.lang.NullPointerException] with root cause java.lang.NullPointerException: null 嵌套异常是java.lang.NullPointerException],其根本原因是java.lang.NullPointerException:null
How can I retrieve and use data from view that had been declared previously in controller? 如何从以前在控制器中声明的视图中检索和使用数据?
1) You have to add @ModelAttribute
annotation to the method argument: 1)您必须在方法参数中添加@ModelAttribute
批注:
public ModelAndView showKPI(@Valid @ModelAttribute Kpi kpi, BindingResult bindingResult, Model model) throws Exception {
// ...
}
2) There is no input for the username in HTML page. 2)HTML页面中没有用户名输入。 You have to add something like: 您必须添加以下内容:
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.