Spring Boot&Thymeleaf:链接列表

huangapple go评论78阅读模式
英文:

Spring Boot & Thymeleaf: list of links

问题

以下是翻译好的部分:

Java/Spring Boot web app here using Thymeleaf as the templating engine.

My bean:

public class InventoryItem {

  private String modelNumber;
  private String name;

  // getters, setters and ctors omitted for brevity
  
}

My Spring controller:

@Controller
@RequestMapping("/inventory")
public class InventoryController {

  @GetMapping("/{inventoryId}")
  public String viewInventory(@PathVariable("inventoryId") String inventoryId, Model model) {

      List<InventoryItem> inventory = getSomehow(inventoryId);
      model.addAttribute("inventory", inventory);

      return "inventory";

  }
  
}

And a snippet from the inventory.html file that Thymeleaf must template:

<div class="col-md-4 mt-5">
    <div class="panel-body">Inventory Items</div>
    <ul>
        <li th:each="item : ${inventory}" th:text="${item.name}"></li>
    </ul>
</div>

At runtime this produces a nice unordered list of inventory item names.

What I want now is to make this an unordered list of hyperlinks (<a/>) such that the rendered HTML looks like so:

<ul>
  <li><a href="/inventoryDetails/12345">Goose</a></li>
  <li><a href="/inventoryDetails/23456">Duck</a></li>
  <!-- etc. -->
</ul>

Where 12345 and 23456 are InventoryItem#modelNumbers and where Goose and Duck are InventoryItem#names. I've asked the Google Gods high and low and cannot find a working example of using Thymeleaf to render a list (ordered/unordered alike) of hyperlinks. Any ideas?

英文:

Java/Spring Boot web app here using Thymeleaf as the templating engine.

My bean:

public class InventoryItem {

  private String modelNumber;
  private String name;

  // getters, setters and ctors omitted for brevity
  
}

My Spring controller:

@Controller
@RequestMapping(&quot;/inventory&quot;)
public class InventoryController {

  @GetMapping(&quot;/{inventoryId}&quot;)
  public String viewInventory(@PathVariable(&quot;inventoryId&quot;) String inventoryId, Model model) {

      List&lt;InventoryItem&gt; inventory = getSomehow(inventoryId);
      model.addAttribute(&quot;inventory&quot;, inventory);

      return &quot;inventory&quot;;

  }
  
}

And a snippet from the inventory.html file that Thymeleaf must template:

&lt;div class=&quot;col-md-4 mt-5&quot;&gt;
    &lt;div class=&quot;panel-body&quot;&gt;Inventory Items&lt;/div&gt;
    &lt;ul&gt;
        &lt;li th:each=&quot;item :${inventory}&quot; th:text=&quot;${item.name}&quot;&gt;&lt;/li&gt;
    &lt;/ul&gt;
&lt;/div&gt;

At runtime this produces a nice unordered list of inventory item names.

What I want now is to make this an unordered list of hyperlinks (&lt;a/&gt;) such that the rendered HTML looks like so:

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;/inventoryDetails/12345&quot;&gt;Goose&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/inventoryDetails/23456&quot;&gt;Duck&lt;/a&gt;&lt;/li&gt;
  &lt;!-- etc. --&gt;
&lt;/ul&gt;

Where 12345 and 23456 are InventoryItem#modelNumbers and where Goose and Duck are InventoryItem#names. I've asked the Google Gods high and low and cannot find a working example of using Thymeleaf to render a list (ordered/unordered alike) of hyperlinks. Any ideas?

答案1

得分: 1

以下是翻译好的部分:

像这样可以工作...

    <div class="col-md-4 mt-5">
        <div class="panel-body">库存物品</div>
        <ul>
            <li th:each="item : ${inventory}">
                <a th:href="@{/inventoryDetails/{modelNumber}(modelNumber=${item.modelNumber})}" th:text="${item.name}">鹅</a>
            </li>
        </ul>
    </div>

如果您想要获取有关如何使用[链接URL][1]的更多信息,请参阅Thymeleaf文档。


  [1]: https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#link-urls
英文:

Something like this would work ...

&lt;div class=&quot;col-md-4 mt-5&quot;&gt;
    &lt;div class=&quot;panel-body&quot;&gt;Inventory Items&lt;/div&gt;
    &lt;ul&gt;
        &lt;li th:each=&quot;item :${inventory}&quot;&gt;
            &lt;a th:href=&quot;@{/inventoryDetails/{modelNumber}(modelNumber=${item.modelNumber})}&quot; th:text=&quot;${item.name}&quot;&gt;Goose&lt;/a&gt;
        &lt;/li&gt;
    &lt;/ul&gt;
&lt;/div&gt;

If you would like to get more information on how to work with Link URLs, follow Thymeleaf documentation.

huangapple
  • 本文由 发表于 2020年8月6日 23:07:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63286521.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定