首页 > 解决方案 > 无法在我的视图上显示数据:评估 SpringEL 表达式的异常

问题描述

我正在使用 Spring Boot 和 thymeleaf 创建银行应用程序,当我尝试显示帐户数据时遇到以下错误: 原因:org.attoparser.ParseException:评估 SpringEL 表达式的异常:“compte.codeCompte”(模板:“comptes”——第 34 行,第 14 列)

这是我的银行控制器:

import java.util.Arrays;
import java.util.Date;
import java.util.List;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.projets.bank.entities.Client;
    import com.projets.bank.entities.Compte;
    import com.projets.bank.entities.CompteEpargne;
    import com.projets.bank.metier.ServiceLayer;
    
    @Controller
    
    
    public class BankController 
    {
        @Autowired
        private ServiceLayer banque;
        
    @RequestMapping("/operation")
    
        public String index()
        {
            return "comptes";
        }
        
        @RequestMapping("/consulterCompte")
        public String consulterCompte(Model model, String codeCompte)
        {
            try
            {
                Compte cp = banque.consulterCompte(codeCompte);
                model.addAttribute("compte",cp);
            }
            catch(Exception e)
            {
                model.addAttribute("exception", e);
            }
            return "comptes";
    
        }
    
        
    
    }

comptes.html:

<div class="card-body">
                    <div>
                        <label>Code: </label>
                        <label th:text="${compte.codeCompte}">Code: </label>
                    </div>
                    <div>
                        <label>Solde: </label>
                        <label th:text="${compte.solde}"> </label>
                    </div>
                    <div>
                        <label>Date de création: </label>
                        <label th:text="${compte.dateCreation}"> </label>
                    </div>
                    <div>
                        <label>Type: </label>
                        <label th:text="${compte.class.simpleName}"> </label>
                    </div>
                </div>

Compte.java:

public abstract class Compte implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    @Id
    private String codeCompte;
    
    private Date dateCreation;
    private double solde;
    
    @ManyToOne
    @JoinColumn(name="CODE_CLI")
    private Client client;
    
    @OneToMany(mappedBy="compte" )
    private Collection<Operation> operation;
    
    public Compte() {
        super();
    }
    public Compte(String codeCompte, Date dateCreation, double solde, Client client) {
        super();
        this.codeCompte = codeCompte;
        this.dateCreation = dateCreation;
        this.solde = solde;
        this.client = client;
    }
    public String getCodeCompte() {
        return codeCompte;
    }
    public void setCodeCompte(String codeCompte) {
        this.codeCompte = codeCompte;
    }
    public Date getDateCreation() {
        return dateCreation;
    }
    public void setDateCreation(Date dateCreation) {
        this.dateCreation = dateCreation;
    }
    public double getSolde() {
        return solde;
    }
    public void setSolde(double solde) {
        this.solde = solde;
    }
    public Client getClient() {
        return client;
    }
    public void setClient(Client client) {
        this.client = client;
    }
    public Collection<Operation> getOperation() {
        return operation;
    }
    public void setOperation(Collection<Operation> operation) {
        this.operation = operation;
    }
    
    
    
}

提前致谢。

标签: javaspringspring-bootspring-datathymeleaf

解决方案


因为当你得到请求“/操作”或发生异常时,你没有在模型中设置属性“compte”。

有两种解决方案。您应该根据需要选择一个。

  1. 在获取请求中添加属性
    @RequestMapping("/operation")
    
        public String index(Model model)
        {
            model.addAttribute("compte",new Compte());
            return "comptes";
        }

        @RequestMapping("/consulterCompte")
        public String consulterCompte(Model model, String codeCompte)
        {
            try
            {
                Compte cp = banque.consulterCompte(codeCompte);
                model.addAttribute("compte",cp);
            }
            catch(Exception e)
            {
                model.addAttribute("exception", e);
                model.addAttribute("compte",new Compte());
            }
            return "comptes";
    
        }

或者

  1. 在 html 中添加 th:if
<div class="card-body" th:if="${compte != null}">
     <div>
          <label>Code: </label>
          <label th:text="${compte.codeCompte}">Code: </label>
     </div>
.... // omit other code

</div>


推荐阅读