我使用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


LiveDataController

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


我如何处理此错误。数据确实存在,但我什至可以在视图中查看数据。
谢谢您阅读此篇。

最佳答案

您要发送以查看的liveDataLiveDataSet的实例。在视图中,您以${row.LiveDataSet}的身份对其进行访问。这没有道理。您的LiveDataSet中没有任何LiveDataSet字段!这就是为什么您会收到此错误。

07-24 22:08