无法在我的视图上显示数据:计算springel表达式时出现异常

mnemlml8  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(253)

我正在使用spring boot和thymeleaf创建一个bankin应用程序,当我试图显示帐户的数据时遇到以下错误:由: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;
    }

}

提前谢谢。

wpx232ag

wpx232ag1#

因为当您得到request“/operation”或发生异常时,您没有在模型中设置属性“compte”。
有两种解决方案。你应该根据需要选择一个。
在get请求中添加属性

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

        }


在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>

相关问题