我必须提交表单中的name并在get方法中接收值。url中的接收值应显示在窗体顶部的列表中。我想把表单提交的所有输入显示为无序列表。但我的代码只显示最新的输入。
friend类有私有字符串friend和getter setters。
@Controller
public class FriendController {
@GetMapping(value = "/index")
public String friendForm(@RequestParam(value = "friend", required = false) Friend friend, Model model) {
List<Friend> fList = new ArrayList<Friend>();
fList.add(friend);
model.addAttribute("friends", fList);
model.addAttribute("friend", new Friend());
return "result";
}
}
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Friends List</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1 th:text="'My Friends'"></h1>
<ul>
<li th:each="friend: ${friends}" th:text="${friend}"></li>
</ul>
<h1 th:text="'Add a new friend to List'"></h1>
<form action="#" th:action="@{/index}" th:object="${friend}"
method="get">
<table>
<tr>
<td>Name: <input type="text" th:field="*{friend}" /></td>
</tr>
</table>
<p>
<input type="submit" value="Submit" />
</p>
</form>
</body>
</html>
暂无答案!
目前还没有任何答案,快来回答吧!