1
我有簡單的類,其中包含2 String
,1 Integer
和1 ArrayList<String>
。使用Spring MVC和Thymeleaf我試圖創建一個表單,我可以在其中插入數據來創建一個對象。可悲的是我不知道如何處理Arraylist。這是我走到這一步:輸入與thymeleaf arraylist
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>F O R M</h1>
<form th:action="@{/form/person}" th:object="${person}" method="post">
<table>
<tr>
<td>Name:</td>
<td><input type="text" th:field="*{name}" /></td>
<td th:if="${#fields.hasErrors('name')}" th:errors="*{name}">Name
error</td>
</tr>
<tr>
<td>Age:</td>
<td><input type="number" th:field="*{age} " /></td>
<td th:if="${#fields.hasErrors('age')}" th:errors="*{age}">Age
Error</td>
</tr>
<tr>
<td>City:</td>
<td><input type="text" th:field="*{city}" /></td>
<td th:if="${#fields.hasErrors('city')}" th:errors="*{city}">City
Error</td>
</tr>
<tr>
<td><input type="submit" value="Submit" /></td>
<td><input type="reset" value="Reset" /></td>
</tr>
</table>
</form>
</body>
</html>
我們假設我的arraylist用於暱稱(而class是Person.class)。每個人都可以擁有很多暱稱,有沒有方法使用SpEL在簡單的form.html中添加暱稱? –
ogarogar
@ogarogar我不太瞭解SpEL,所以我無法幫助你。你是否想讓用戶添加儘可能多的暱稱?爲什麼不添加例如暱稱的3個輸入字段? –