java - 无法在我的视图上显示数据:评估 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;
}
}
提前致谢。
解决方案
因为当你得到请求“/操作”或发生异常时,你没有在模型中设置属性“compte”。
有两种解决方案。您应该根据需要选择一个。
- 在获取请求中添加属性
@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>
推荐阅读
- reactjs - React 应用程序在对话框中显示表单会导致“不推荐使用 findDOMNode”错误
- python - 在 Python 上使用 NLTK 创建自己的命名实体
- excel - 如何根据其他工作表上的列名和值查找数据
- c# - SqlException 无法绑定多部分标识符“A.Column”
- floating-point - Z3 验证器中的浮点溢出指示
- python - 模块'pandas'没有属性'stats' Python
- python - 在 Numba 中,以 CUDA 为目标时,如何将数组复制到常量内存中?
- keras - 使用 Kears 开发自动编码器时形状不匹配?
- git - 在 git for-each-ref 中,%(refname) 和 %(upstream:remoteref) 有什么区别?
- node.js - 我在 mongo db atlas 中看不到我的收藏