我在 Spring boot 应用程序中使用 Microsoft SQL。我已经弄清楚了一切,现在我正在尝试将数据从 SQL 数据库传递到前端。但是,当我通过 thymeleaf 传递信息时,它不起作用。我有一个应用程序存储库以及一个实际传递数据的 DTO 类。尽管我不断收到同样的错误。很抱歉用这么多代码轰炸大家,但我已经尝试找到解决方案一段时间了,但仍然想不出解决方法。我相信它与 TestApp 类有关,但不确定在哪里。
错误:
org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'dtoTiername' cannot be found on object of type 'com.John.model.TestApp' - maybe not public or not valid?
应用程序存储库:
@Query("SELECT new com.John.model.TestApp(t.tiername, t.system) FROM AppSelectorTier t")
List<TestApp>getServerInfo();
服务层:
public List<TestApp> getServerInfo() {
List<TestApp> myList = appRepository.getServerInfo();
System.out.println(myList.size());
return myList;
}
Controller :
@RequestMapping("/edit")
public ModelAndView editTab(Model model) {
ModelAndView modelAndView = new ModelAndView();
List<TestApp>ary4 = new ArrayList<TestApp>();
try {
System.out.println("Hello Code");
ary4 = joinQueryService.getServerInfo();
modelAndView.setViewName("create");
} catch (Exception e) {
e.printStackTrace();
modelAndView.setViewName("test");
}
modelAndView.addObject("ary4",ary4);
return modelAndView;
}
HTML:
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Tiername</th>
<th>System</th>
</tr>
</thead>
<tbody>
<tr th:each="ary4 : ${ary4}">
<td th:text="${ary4.dtoTiername}"></td>
<td th:text="${ary4.dtoSystem}"></td>
</tr>
</tbody>
</table>
测试应用:
package com.John.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
public class TestApp implements java.io.Serializable {
private String dtoTiername;
private String dtoSystem;
private static final long serialVersionUID = 1L;
//Constructor
public TestApp(String dtoTiername, String dtoSystem) {
super();
this.dtoTiername = dtoTiername;
this.dtoSystem = dtoSystem;
}
//Default Constructor
public TestApp() {
}
//Getter
public String getName() {
return dtoTiername;
}
public String getSystem() {
return dtoSystem;
}
//Setter
public void setName(String dtoTiername) {
this.dtoTiername = dtoTiername;
}
public void setSystem(String dtoSystem) {
this.dtoSystem = dtoSystem;
}
}
实体:
package com.John.model;
import java.util.*;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "tier")
public class AppSelectorTier implements java.io.Serializable {
@Id
@Column(name = "tiername")
private String tiername;
@Column(name = "system")
private String system;
private static final long serialVersionUID = 1L;
//Constructor
public AppSelectorTier(String tiername, String system) {
super();
this.tiername = tiername;
this.system = system;
}
//Default Constructor
public AppSelectorTier() {
}
//Getter
public String getName() {
return tiername;
}
public String getSystem() {
return system;
}
//Setter
public void setName(String tiername) {
this.tiername = tiername;
}
public void setSystem(String system) {
this.system = system;
}
}
最佳答案
属性由 getter 和 setter 映射,因此您可以执行其中任一操作
第一:
保持 getter setter 与变量名称对齐
public class TestApp implements java.io.Serializable {
private String dtoTiername;
private String dtoSystem;
public String getDtoTiername() {
return dtoTiername;
}
public void setDtoTiername(String dtoTiername) {
this.dtoTiername = dtoTiername;
}
public String getDtoSystem() {
return dtoSystem;
}
public void setDtoSystem(String dtoSystem) {
this.dtoSystem = dtoSystem;
}
}
第二:
将 JSP 变量更改为
<tr th:each="ary4 : ${ary4}">
<td th:text="${ary4.name}"></td>
<td th:text="${ary4.system}"></td>
</tr>
关于java - Spring Boot中如何解决 "Exception evaluating SpringEL expression"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59231579/