显示JSF表中对象的数组列表



我想在JSF表中显示对象的ArrayList:

我创建了这个对象:

public List<TData> td = new ArrayList<TData>();
    public class TData {
        private long id;
        private String key;
        private String language;
        private String translation;
        public TData(){};
        public TData(long id, String key, String language, String translation) {
            // super();
            this.id = id;
            this.key = key;
            this.language = language;
            this.translation = translation;
        }
        public long getId() {
            return id;
        }
        public void setId(long id) {
            this.id = id;
        }
        public String getKey() {
            return key;
        }
        public void setKey(String key) {
            this.key = key;
        }
        public String getLanguage() {
            return language;
        }
        public void setLanguage(String language) {
            this.language = language;
        }
        public String getTranslation() {
            return translation;
        }
        public void setTranslation(String translation) {
            this.translation = translation;
        }
    }
    public List<TData> getDataObj() {
        return td;
    }
    @PostConstruct
    public void loadData() {
        String query = "SELECT c FROM TranslationTestEntitie c";
        List<Object> result =  dao.jpqlQuery(query, 1, 0);
        for(int i =0; i < result.size(); i++){
            TranslationTestEntitie test = (TranslationTestEntitie) result.get(i);
            td.add((TData) result.get(i));
        }
    }

然后我使用这个数据表来显示结果;

<h:dataTable value="#{languageBeanTest.dataObj}" var="o">
        <h:column>
            <f:facet name="header">Id</f:facet>
                    #{o.id}
                </h:column>
        <h:column>
            <f:facet name="header">key</f:facet>
                    #{o.key}
                </h:column>
        <h:column>
            <f:facet name="header">language</f:facet>
                    #{o.language}
                </h:column>
        <h:column>
            <f:facet name="header">translation</f:facet>
                    #{o.translation}
                </h:column>
    </h:dataTable>

我得到这个错误:

java.lang.ClassCastException: callflow.cfdbc_common.entities.TranslationTestEntitie cannot be cast to callflow.ccadmin.beans.LanguageBeanTest$TData

我得到的结果从数据库的对象列表。如何将这些对象显示到JSF页面中?

p。S这是实体的源代码

@Entity
@Table(name = "Translations")
public class TranslationTestEntitie extends AbstractEntityWithId {
    private static final long serialVersionUID = 2029240552230401080L;
    @Column(name = "id", insertable=false, updatable=false)
    private long id;
    @Column(name = "key", insertable=false, updatable=false)
    private String key;
    @Column(name = "language", insertable=false, updatable=false)
    private String language;
    @Column(name = "translation", insertable=false, updatable=false)
    private String translation;
    public TranslationTestEntitie() {
    }
    // Getters and setters
    public Long getId() {
        return id;
    }
    //@Transient    Field will not be saved in database
    public void setId(long id) {
        this.id = id;
    }
    public String getKey() {
        return key;
    }
    public void setKey(String key) {
        this.key = key;
    }
    public String getLanguage() {
        return language;
    }
    public void setLanguage(String language) {
        this.language = language;
    }
    public String getTranslation() {
        return translation;
    }
    public void setTranslation(String translation) {
        this.translation = translation;
    }
}

我认为JPA返回的Object将与TData相同,因为我具有相同的对象属性。

显然,返回的对象类型是TranslationTestEntitie,而不是TData。您需要编写代码将TranslationTestEntitie转换为TData类型的对象。

TranslationTestEntitie test = (TranslationTestEntitie) result.get(i);
TData newTData = new TData(test.getId(), test.getKey(), test.getLanguage(),  test.getTranslation());
td.add(newTData);

如果它有相同的字段,为什么你不能使用TranslationTestEntitie本身?

List<TranslationTestEntitie> td=  new ArrayList<TranslationTestEntitie>();
for(int i =0; i < result.size(); i++){
        TranslationTestEntitie test = (TranslationTestEntitie) result.get(i);
        td.add(test);
}

相关内容

  • 没有找到相关文章

最新更新