首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从JSF XHTML文件访问自定义Java对象

从JSF XHTML文件访问自定义Java对象
EN

Stack Overflow用户
提问于 2017-02-08 17:42:57
回答 1查看 2.2K关注 0票数 0

我已经创建了一个简单的XHTML文件和相应的Java Bean。在Java Bean内部,我用我的自定义类"FilePreview“的对象生成了一个ArrayList,它是在另一个包中定义的(当然,我将它导入到了Bean中)。在我的XHTML-File中,我使用ui-repeat来迭代列表并显示每个元素。我尝试使用get-Methods访问我的对象的属性。

我的XHTML-文件

代码语言:javascript
复制
    <?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:b="http://bootsfaces.net/ui"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
    <h:head>        
        <link href="css/stylesheet.css" rel="stylesheet" type="text/css" />
    </h:head>
    <body>
        <ui:composition template="./template.xhtml">
            <ui:define name="title">
                Dateien
            </ui:define>
            <ui:define name="content">
                <b:container>
                                <ui:repeat value="#{FileBean.files}" var="files">
                                    <b:panel title="#{files.headline}" look="primary">
                                        <h:outputText value="#{files.description}" />
                                    </b:panel>
                                    <b:alert class= "file" severity="success"><strong>#{files.headline}</strong><br></br> <span>#{files.description}</span></b:alert>
                                </ui:repeat>                              
                </b:container>
            </ui:define>
        </ui:composition>
    </body>
</html>

我的JavaBean:

代码语言:javascript
复制
import de.unibremen.st.gradelog.model.FilePreview;
import java.util.ArrayList;

@ManagedBean(name = "FileBean")
@SessionScoped
public class FileBean implements Serializable {
    // private DBHandler handler;
    private ArrayList<FilePreview> files;

    public void create() {
        files = new ArrayList();
        files.add(new FilePreview("Hausordnung",
                "Anbei findet Ihr die aktualisierte Hausordnung. Bitte gründlich lesen!",
                null, null, null));
       }

    public ArrayList getFiles() {
        /**
         * DBHandler.getFiles(userID);
         */
        System.out.println("Lese die Dateien aus.");
        create();
        return files;
    }

}

最后是FilePreview类:

代码语言:javascript
复制
package de.unibremen.st.gradelog.model.FilePreview
public class FilePreview {
    private String headline, description, authorID, fileID;
    private File file;

    public FilePreview(String headline, String description, String authorID,
            String fileID, File file) {
        this.headline = headline;
        this.description = description;
        this.authorID = authorID;
        this.fileID = fileID;
        this.file = file;
    }

    public String getHeadline() {
        return headline;
    }

   //... more simple getters and setters
}

看起来一切正常,只要找到,但当我运行应用程序并访问我的新页面时,我得到了以下错误:

代码语言:javascript
复制
Schwerwiegend:   Error Rendering View[/files.xhtml]
javax.el.ELException: /files.xhtml @51,82 value="#{FileBean.files}": java.lang.NoClassDefFoundError: de/unibremen/st/gradelog/model/FilePreview
    at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:114)
    at com.sun.faces.facelets.component.UIRepeat.getValue(UIRepeat.java:279)
    at com.sun.faces.facelets.component.UIRepeat.getDataModel(UIRepeat.java:255)
...
Caused by: javax.el.ELException: java.lang.NoClassDefFoundError: de/unibremen/st/gradelog/model/FilePreview
    at javax.el.BeanELResolver.getValue(BeanELResolver.java:368)

我必须以某种方式让JSF知道这个类吗?我正在处理一个现有的JSF项目,当我尝试对一个新项目做同样的事情时,一切都运行得很好。

有没有人知道这是什么原因?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-08 18:19:10

您必须生成一个faces-config.xml文件,在该文件中声明您的托管bean为FileBean。你必须这样做:

enter image description here

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42109546

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档