如何在 Thymeleaf 中显示迭代索引?

How to display iteration index in Thymeleaf?

我正在尝试使用 Thymeleaf 显示数组中的每一行 - 按照他们的 documentation 我无法使用 th:each 中的以下任何属性:

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";

}

每个数组中都正确提交了必要的数据,但我相信我误解了文档:

Thymeleaf 在一个特殊变量中维护 th:each 标签的迭代状态。请参阅 relevant documentation

在该变量提供的不同信息中,您可以找到 index 属性,它对应于实际迭代索引。

在您的示例中,您可能可以像这样迭代结果:

 <tr th:each="year, iterStat : ${years}">
      <th scope="row" th:text="${year}"></th>
      <th scope="row" th:text="${bdcInterest[iterStat.index]}"></th>
      <td th:text="${bdAmount[iterStat.index]}"></td>
 </tr>

为避免此类问题,请考虑在您的代码中定义一个简单的 java 对象,该对象聚合您正在迭代的四个属性:

public class MuCustomObject {
  private BigDecimal bdAmount;
  private BigDecimal bdcInterest;
  private BigDecimal initialInvestment;
  private int year;

  // getters and setters omitted for brevity
}

然后,在您的控制器中使用该对象:

    @RequestMapping(value = "/submit", method = RequestMethod.GET)
public String userInput(Model model, BigDecimal lumpsum, BigDecimal interestrate, BigDecimal monthlywithdrawal) {
    
    BigDecimal initialinvestment = lumpsum;
    
    List<MyCustomObject> myCustomObjectList = new ArrayList<MyCustomObject>();
    
    MyCustomObject myCustomObject = new MyCustomObject();
    myCustomObject.setBdcInterest(new BigDecimal(0));
    myCustomObject.setInitialInvestment(initialinvestment);
    myCustomObjectList.add(myCustomObject);
    
    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 

        myCustomObject = new MyCustomObject();
        myCustomObject.setBdAmount(amount);
        myCustomObject.setBdcInterest(cInterest);
        myCustomObject.setInitialInvestment(initialinvestment);
        myCustomObject.setYear(increment);
        myCustomObjectList.add(myCustomObject);
        
        lumpsum = amount;

        increment++;
    }
    
    model.addAttribute("myCustomObjects", myCustomObjectList);

    return "userinput";

}

有了这些信息,您可以直接迭代集合:

 <tr th:each="myCustomObject, iterStat : ${myCustomObjects}">
      <th scope="row" th:text="${myCustomObject.year}"></th>
      <th scope="row" th:text="${myCustomObject.bdcInterest}"></th>
      <td th:text="${myCustomObject.bdAmount}"></td>
 </tr>