如何在springboot中使用thymeleaf将两个不同的Atributt传递给同一个'tr',并使用相同的'th:each'?

mzsu5hc0  于 2021-08-20  发布在  Java
关注(0)|答案(2)|浏览(362)

我正在使用基本的springboot和thymeleaf模板。
这是一个基本且简单的控制器类。我没有创建很多类或mvc。

@Controller
public class HomeController 
{

@GetMapping("/")
 public String home(Model model)
 {
  int[] days = IntStream.range(1, 46).toArray();
  int golds[] = {15000, 12000, 11000, 9000, 7200, 16400, 6600, 5400, 7000, 6300, 4700, 5400, 10600, 7000, 4400, 4500, 4400, 4400, 4400, 6300, 3400, 3600, 5100, 3600, 3700, 3800, 5700, 3700, 3500, 16700, 3600, 3700, 3500, 36000, 3400, 3500, 4000, 3500, 3700, 4000, 4800, 4400, 3600, 4300, 3500};

  model.addAttribute("days", days);
  model.addAttribute("golds", golds);

  return "home";
 }
}

我试着把两者都渲染出来 daysgolds 在同一表行的两个数据单元格中 th:each .
我已经考虑过创建一个对象并在其上保存两个属性,以便将它们分配给列表中的单个属性并迭代列表。但是它创建了许多不必要的类。我觉得太过分了。我试过一次,但没用。

<table>
  <thead>
      <tr>
          <th> Dia </th>
          <th> Ouros </th>
      </tr>
  </thead>
  <tbody>
      <tr th:each="day, gold : ${days}">
          <td>[[${day}]]</td>
          <td>[[${gold}]]</td>
      </tr>
  </tbody>
</table>

更新2

我遵循了@andrewjames的重新设计,但它不起作用:

@Controller
public class HomeController implements Serializable 
{
  public static class GoldData 
  {
    private int[] day;
    private int[] gold;

    public int[] getDay() {
      return day;
    }

    public int[] getGold() {
      return gold;
    }

    public void setGold(int[] gold) {
      gold = new int[]{15000, 12000, 11000, 9000, 7200, 16400, 6600, 5400, 7000, 6300, 4700, 5400, 10600, 7000, 4400, 4500, 4400, 4400, 4400, 6300, 3400, 3600, 5100, 3600, 3700, 3800, 5700, 3700, 3500, 16700, 3600, 3700, 3500, 36000, 3400, 3500, 4000, 3500, 3700, 4000, 4800, 4400, 3600, 4300, 3500};
      this.gold = gold;
    }

    public void setDay(int[] day) {
      day = IntStream.range(1, 46).toArray();
      this.day = day;
    }

  }

  List<GoldData> goldData = new ArrayList<>();

  @GetMapping("/")
  public String home(Model model) 
  {
    int total = Arrays.stream(golds).sum();
    int reais = total / 10000;

    model.addAttribute("soma", total);
    model.addAttribute("reais", "R$" + reais);
    model.addAttribute("goldData", goldData);

    return "home";
  }
}

更新3

无出口:

@Controller
public class HomeController implements Serializable 
{
  private int[] day;
  private int[] gold;

  day = IntStream.range(1, 46).toArray();
  gold = new int[]{15000, 12000, 11000, 9000, 7200, 16400, 6600, 5400, 7000, 6300, 4700, 5400, 10600, 7000, 4400, 4500, 4400, 4400, 4400, 6300, 3400, 3600, 5100, 3600, 3700, 3800, 5700, 3700, 3500, 16700, 3600, 3700, 3500, 36000, 3400, 3500, 4000, 3500, 3700, 4000, 4800, 4400, 3600, 4300, 3500};

  int total = stream(gold).sum();
  int reais = total / 10000;

  @GetMapping("/")
  public String home(Model model)
  {
    List<HomeController> goldData = new ArrayList<>();
    List<HomeController> total;
    List<HomeController> reais;

    model.addAttribute("soma", total);
    model.addAttribute("reais", "R$" + reais);
    model.addAttribute("goldData", goldData);

    return "home";
  }
}
oxosxuxt

oxosxuxt1#

在这种情况下,因为 days 只是一个整数序列,您不需要将其传递给模型。相反,您可以使用thymeleaf的迭代状态跟踪器变量。在本例中,变量 count (从1开始)满足您的需要:

<table>
    <thead>
        <tr>
            <th> Dia </th>
            <th> Ouros </th>
        </tr>
    </thead>
    <tbody>
        <tr th:each="gold,iterStat : ${golds}">
            <td th:text="${iterStat.count}"></td>
            <td th:text="${gold}"></td>
        </tr>
    </tbody>
</table>

你可以看到 iterStat.count 每行第一个单元格中的值。
在我的例子中,我将这些值放在一个标准中 th:text 属性您可以使用其他语法 [[...]] 如果你愿意的话。
如果你没有一个简单的整数序列 days 我建议两种可能的选择:
1-使用 iterStat.index -从零开始(不同于 count 从1)开始,并使用该值作为数组索引值-例如, ${days[iterStat.index]} .
2-将两个单独的数组重新设计成一个简单的javabean,其中包含您需要的两个字段(一个day值和一个gold值)。然后创建这些对象的列表。然后,在thymeleaf中迭代列表变得简单,您不需要任何 iterStat 变量。
更新
关于备选方案2的一些补充说明:
假设您有一个非常基本的pojo(纯旧java对象),包含两个字段:

public static class GoldData {
    private int day;
    private int gold;
    // getters and setters not shown, but are needed.
}

然后,您将构建此类对象的列表:

List<GoldData> goldData = new ArrayList<>();

并填充它,使每个golddata对象包含一对当前存储在阵列中的数据。
然后将其添加到模型中,而不是2个(或更多)阵列:

model.addAttribute("goldData", goldData);

然后,html表可以使用该对象并直接引用其字段:

<tr th:each="goldDataItem : ${goldData}">
    <td th:text="${goldDataItem.day}"></td>
    <td th:text="${goldDataItem.gold}"></td>
</tr>

现在,不需要任何迭代跟踪计数器/变量。
更一般地说,如果您不需要将数据存储在java数组中,那么对象可以更容易/灵活地使用。
更新2
在对问题进行编辑时,只需将两个数组移动到一个单独的类中。这不是我的例子所做的。
在我的示例类中,我完全去掉了数组。
理想情况下,您首先不需要这两个阵列。但为了简单起见,让我们假设您确实从这两个阵列开始。
创建一个我的 GoldData 对象,可以执行以下操作:

GoldData gd = new GoldData();
gd.setDay(days[0]);
gd.setGold(gold[0]);

(我忽略了通过添加适当的构造函数可以实现的改进,仅针对本演示。)
现在,这就创造了一个 GoldData 对象,包含一个 day 价值与一 gold 价值
你把它加到 List 详情如下:

List<GoldData> goldData = new ArrayList<>();
goldData.add(gd);

现在,想象一下同时迭代两个数组中的每个项目,创建45个对象,并将每个对象添加到列表中。
看看是否可以使用上面的注解来构建 List 所以它包含这些对象。您将在任何地方看到最终结果中都没有涉及数组。
最后更新
这是你的电话号码 GoldData 类,但这次我添加了getter和setter。我还添加了构造函数,这一次:

public static class GoldData {

    private int day;
    private int gold;

    public GoldData() {
    }

    public GoldData(int day, int gold) {
        this.day = day;
        this.gold = gold;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public int getGold() {
        return gold;
    }

    public void setGold(int gold) {
        this.gold = gold;
    }

}

下面是获取数组数据并将其加载到 GoldData 物体:

// Here is your array data, from the question:
int[] days = IntStream.range(1, 46).toArray();
int[] golds = {15000, 12000, 11000, 9000, 7200, 16400, 6600, 5400, 7000, 6300, 4700, 5400, 10600, 7000, 4400, 4500, 4400, 4400, 4400, 6300, 3400, 3600, 5100, 3600, 3700, 3800, 5700, 3700, 3500, 16700, 3600, 3700, 3500, 36000, 3400, 3500, 4000, 3500, 3700, 4000, 4800, 4400, 3600, 4300, 3500};

// Here is what we actually want to send to the Thymeleaf template, instead of your arrays:
List<GoldData> goldDataList = new ArrayList<>();

// Here is how we transfer your array data into the above list:
for (int i = 0; i < days.length; i++) {
    goldDataList.add(new GoldData(days[i], golds[i]));
}

model.addAttribute("goldDataList", goldDataList);

这是thymeleaf模板表的主体,它使用 goldDataList :

<tbody>
    <tr th:each="goldData : ${goldDataList}">
        <td th:text="${goldData.day}"></td>
        <td th:text="${goldData.gold}"></td>
    </tr>
</tbody>

请注意,在这段代码中,除了您的问题中的两个原始数组之外,没有任何数组。

7vux5j2d

7vux5j2d2#

只要你 Arrays 都是相同的大小,您可以这样迭代:

<tr th:each="i : ${#numbers.sequence(0, #lists.size(days) - 1)}">
  <td>[[${days[i]}]]</td>
  <td>[[${golds[i]}]]</td>
</tr>

相关问题