【问题标题】:spring thymeleaf error EL1008E Property or field cannot be found on object of type 'LiveDataController$LiveDataSet' maybe not public or not valid?spring thymeleaf error EL1008E 在“LiveDataController$LiveDataSet”类型的对象上找不到属性或字段可能不是公共的或无效的?
【发布时间】: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


【解决方案1】:

您发送给查看的liveDataLiveDataSet 的一个实例。在视图中,您以${row.LiveDataSet} 访问它。这没有意义。您的LiveDataSet 中没有任何LiveDataSet 字段!这就是您收到此错误的原因。

【讨论】:

  • 嗨@Minar,但我已经尝试过像 [[${row.LiveDataSet.getActive}]]、[[${row.getActive}]] 等多种方式,但仍然不起作用.此外,即使在javascript中也无法访问 /*/ var liveData = [[${ liveData }]]; /]]>*/
【解决方案2】:

您应该访问 Object 行的属性(这是我相信的 LiveDataSet 的一个实例)

<thead>
<tr>
<td>Col1</td>
<td>Col2</td>
<td>Col3</td>
</tr>
</thead>
<tbody>
    <tr th:each="row : ${liveData}">
        <td th:text="${row.attr1}"></td>
        <td th:text="${row.attr2}"></td>      
        <td th:text="${row.attr3}"></td>            
    </tr>
</tbdoy>

【讨论】:

  • 你好 Pablo 我已经尝试过很多次了。但是还是不行谢谢。
【解决方案3】:

[[${liveData}]]

public class LiveDataSet
{

    String getActive; -> dActive;
    String getApparent; -> dApparent;
    String getCurrent; -> dCurrent;
    String getEnergy; -> dEnergy
}

问题是,变量命名。如果我声明变量 get + Active ,它会返回 get, property 所以这会导致 java 变量检测故障,所以我更改了变量名并安全地返回值。

【讨论】:

    【解决方案4】:

    我遇到了类似的问题,从长远来看,我意识到我在他们的父类中没有 getter/setter(不使用 lombok 的另一个缺点)。在 getter setter 和重命名之后,它起作用了。也许你应该调查一下。

    【讨论】:

      最近更新 更多