遇到 Field or property ‘id’ cannot be found on object of type ‘***’该怎么办?

问题描述

在为页面添加自定义数据集的时候,用到了roo框架本身自带的table.tagx,参考roo自动生成的list.xml代码来编写自己的列表视图时,却出现了Field or property 'id' cannot be found on object of type '***'的提示。

解决办法

分析 Field or property 'id' cannot be found on object of type '***' 一句的内容,意思是数据对象缺少 id 属性。

打开 table.tagx 文件,观察到如下参数定义:

<jsp:directive.attribute name="typeIdFieldName" type="java.lang.String" required="false" rtexprvalue="true" description="The identifier field name for the type (defaults to 'id')" />

由此可得,该table tag需要数据对象有identifier属性,默认属性名为 id,如下所示:

<c:if test="${empty typeIdFieldName}">
    <c:set var="typeIdFieldName" value="id" />
</c:if>

观察 typeIdFieldName 的调用情况,注意到以下几行代码:

这里对itemId变量进行了赋值

 <c:set var="itemId"><spring:eval expression="item.${typeIdFieldName}"/></c:set>

可以看出itemId的作用是为 show、update、delete这些操作提供正确的访问路径。

<spring:url value="${path}/${itemId}" var="show_form_url" />
...
<spring:url value="${path}/${itemId}" var="update_form_url" />
...
<spring:url value="${path}/${itemId}" var="delete_form_url" />

由于传到此页面的数据对象没有添加roo的注解,所以没有自动生成id属性。所以修改 * 类如下即可:


@RooJavaBean
@RooToString
public class CurrentSensorDataObject {
    private SensorType type;

    private String description;

    private StateTypeEnum state;

    private float data;

    private Long id; //添加这一行
}

并且要对其进行赋值:


CurrentSensorDataObject newData = new CurrentSensorDataObject();
newData.setData(lastestData.getData());
newData.setDescription(sensor.getDescription());
newData.setState(lastestData.getState());
newData.setType(sensor.getType());
newData.setId(sensor.getId());

tomcat成功运行!

antibiotici-acquista.com