【发布时间】:2025-12-28 04:50:07
【问题描述】:
我使用 thymeleaf 将 index.html 传递给包含 LiveDataSet 类的模型属性。但我一直遇到这个错误。
***org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'LiveDataSet' cannot be found on object of type 'com.ex.excom.controller.LiveDataController$LiveDataSet' - maybe not public or not valid?***
at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:217)
PropertyOrFieldReference.java:217
at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:104)
PropertyOrFieldReference.java:104
at org.springframework.expression.spel.ast.PropertyOrFieldReference.access$000(PropertyOrFieldReference.java:51)
PropertyOrFieldReference.java:51
at org.springframework.expression.spel.ast.PropertyOrFieldReference$AccessorLValue.getValue(PropertyOrFieldReference.java:406)
PropertyOrFieldReference.java:406
at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:90)
CompoundExpression.java:90
at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:109)
SpelNodeImpl.java:109
at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:328)
SpelExpression.java:328
at org.thymeleaf.spring5.expression.SPELVariableExpressionEvaluator.evaluate(SPELVariableExpressionEvaluator.java:263)
SPELVariableExpressionEvaluator.java:263
at org.thymeleaf.standard.expression.VariableExpression.executeVariableExpression(VariableExpression.java:166)
VariableExpression.java:166
at org.thymeleaf.standard.expression.SimpleExpression.executeSimple(SimpleExpression.java:66)
SimpleExpression.java:66
实时数据控制器
@RestController
@Controller
public class LiveDataController extends BaseController
{
public class LiveDataSet
{
String getActive;
String getApparent;
String getCurrent;
String getEnergy;
}
public LiveDataSet getLiveDatas(){
...
LiveDataSet liveDatas = new LiveDataSet();
liveDatas.getCurrent = "1234"
liveDatas.getEnergy = "92.1"
...
return liveDatas
}
}
索引控制器
@RequestMapping("/")
@PreAuthorize("hasAnyAuthority('ROLE_USER','ROLE_ADMIN')")
public String index(Model model, HttpSession session)
{
LiveDataSet liveData = liveDataController.getLiveDatas();
Optional<LiveDataSet> listOptLiveData = Optional.of(liveData);
listOptLiveData.ifPresent(LiveDataSet -> model.addAttribute("liveData", liveData));
return "index";
}
index.html
<tr th:each="row : ${liveData}">
<td th:text="${row.LiveDataSet}"></td>
</tr>
我该如何处理这个错误。数据确实存在,但我什至可以在视图中看到数据。 感谢您阅读本文。
【问题讨论】:
-
[[${liveData}]] 这样打印。 -> com.ex.excom.controller.LiveDataController$LiveDataSet@18b9fc7
标签: java spring spring-boot spring-mvc thymeleaf