繁体   English   中英

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

[英]Access custom Java Object from JSF XHTML file

我创建了一个简单的XHTML文件和一个对应的Java Bean。 在Java Bean内,我用自定义类“ FilePreview”的对象生成了一个ArrayList,该对象在另一个包中定义(当然,我将其导入Bean中)。 在我的XHTML文件中,我使用ui-repeat遍历列表并显示每个元素。 我尝试使用get-Methods访问对象的属性。

我的XHTML文件

    <?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:

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:

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
}

一切似乎都可以找到,但是当我运行应用程序并访问新页面时,出现以下错误:

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项目,当我尝试对一个新项目做同样的事情时,一切都很好。

有人知道是什么原因造成的吗?

您必须生成一个faces-config.xml文件,在其中声明您的托管bean是FileBean。 您必须执行以下操作:

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM