I'm attempting to display each row from an array using Thymeleaf - following their documentation I am unable to use any of the following attributes from th:each:
The current iteration index, starting with 0. This is the index property.
The current iteration index, starting with 1. This is the count property.
userinput.html:
<tr th:each="year : ${years}">
<th scope="row" th:text="${years}"></th>
<th scope="row" th:text="${bdcInterest}"></th>
<td th:text="${bdAmount}"></td>
</tr>
CalculatorController.java:
@RequestMapping(value = "/submit", method = RequestMethod.GET)
public String userInput(Model model, BigDecimal lumpsum, BigDecimal interestrate, BigDecimal monthlywithdrawal) {
BigDecimal initialinvestment = lumpsum;
BigDecimal[] bdAmount = new BigDecimal[11];
BigDecimal[] bdcInterest = new BigDecimal[11];
BigDecimal[] initialInvestment = new BigDecimal[11];
int[] years = new int[11];
bdcInterest[0] = new BigDecimal(0);
initialInvestment[0] = initialinvestment;
int increment = 1;
while(increment < 10) {
BigDecimal amount = lumpsum
.multiply(BigDecimal
.valueOf(1)
.add(interestrate
.divide(BigDecimal
.valueOf(100)))
.subtract(monthlywithdrawal
.multiply(BigDecimal
.valueOf(12)))); // Calculate the total yearly amount
BigDecimal cInterest = amount.subtract(initialinvestment); // Calculate only the interest earned
bdAmount[increment] = amount;
bdcInterest[increment] = cInterest;
initialInvestment[increment] = initialinvestment;
years[increment] = increment;
lumpsum = amount;
increment++;
}
model.addAttribute("years", years);
model.addAttribute("initialInvestment", initialInvestment);
model.addAttribute("bdAmount", bdAmount);
model.addAttribute("bdcInterest", bdcInterest);
return "userinput";
}
The necessary data is submitted correctly in each respective array, however I believe I've misunderstood the documentation: