【问题标题】:I have created a web application using (Spring Boot + Thymeleaf)我使用(Spring Boot + Thymeleaf)创建了一个 Web 应用程序
【发布时间】:2021-10-16 23:18:27
【问题描述】:

我的问题是使用 Thymeleaf 显示 2 个不同的图像,包括来自数据库的数据值 here is the UI exemple.

当付款为 "espece" 时我需要显示 espece.png,当付款为 check.png 时我需要显示 check.png strong>“标准检查”

HTML:

    <div class="row">
        <div class="col-lg-4" th:each="projets : ${listeProjets}">
         <div class="card border border-primary">
          <div class="card-header bg-transparent border-primary d-flex align-items-start">
            <h5 class="my-0 text-primary" th:text="${projets.nomProjet}"></h5>
             <div class="flex-shrink-0 ms-2"> 
               <img class="card-img-top" alt="" src="/assets/images/tache/espece.png" width="40" height="40" />
          </div>
    <div class="flex-shrink-0 ms-2">
        <a href=""><i class="bx bx-x font-size-20"></i></a>
         </div>
    </div>
    <div class="card-body">
        <h5 class="card-title">card title</h5>
            <div class="progress">
                <div class="progress-bar progress-bar-striped progress-bar-animated bg-success"
                    role="progressbar" aria-valuemin="0" aria-valuemax="100" 
                    th:style="'width:'+${projets.avancement}+';'" th:text="${projets.avancement}"> 
                </div>
            </div>
        <p class="card-text">Some quick example</p>
    </div>
    <div class="card-footer bg-transparent border-primary">
     <button type="button" class="btn btn-outline-light waves-effect w-100" data-bs-toggle="modal" data-bs-target="#addModal">
    <i class="bx bx-plus font-size-16 align-middle"></i>Ajouter Tâche
    </button>
    </div>
    </div>
    </div>
    </div> 

Java 类:

    @Entity
    public class Projet {
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private int id;
        @Column(nullable = false, length = 25)
        private String nomProjet;
        @Column(length = 100)
        private String description;
        private Date dateDebut;
        private Date dateFin;
        private float prix;
        @Column(length = 15)
        private String methodePaiement;
        @Column(length = 10)
        private String avancement;
    
        @ManyToOne
        @JoinColumn(name = "clientid", insertable = false, updatable = false)
        private Client client;
        private int clientid;
    
        /** Default Constructor, getters and setters **/ 
    
    }

【问题讨论】:

    标签: java html spring-boot bootstrap-4 thymeleaf


    【解决方案1】:
    <img class="card-img-top" alt="" th:with="paymentImageName = ${projets.methodePaiement == 'espece'}? 'espece' : 'check'" th:src="@{/assets/images/tache/{imageName}.png(imageName=${paymentImageName})}" width="40" height="40" />
    
    1. 通过使用the:with,我引入了局部变量paymentImageName,根据conditional expression设置适当的名称。
    2. 使用link URL 处理具有适当图像名称的图像URL,在步骤#1 中设置并已由Thymeleaf 根据Attribute Precedence 解析
    3. 阅读更多Thymeleaf

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-26
      • 1970-01-01
      • 2018-07-02
      • 2014-03-29
      • 2015-09-14
      • 2016-02-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多