显示JSF表中的对象列表

时间:2013-03-20 13:31:41

标签: java jsf jpa jsf-2

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

我创建了这个对象:

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

我从DB获取结果作为对象列表。我如何将这些对象显示到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相同,因为我有相同的对象属性。

1 个答案:

答案 0 :(得分:2)

显然,返回的对象是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);
}