如何解决春季启动中的GET参数到列表
我必须从表单提交名称并在GET方法中接收值。 URL中的接收值应以列表形式显示在表单顶部。 我想将所有来自表单提交的输入显示为无序列表。 但是我的代码仅显示最新输入。
朋友班有 私人的String朋友和getter设置者。
@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>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。