jsf - javax.el.E​​LException : Error reading 'foo' on type com. 示例.Bean

标签 jsf properties el getter

我正在阅读 the following tutorial :

The expression used in the h:dataTable/@value normally specifies a property name for which a getter was defined, which means that in the controller BookController class a property books is defined (optional) as well as a method named getBooks (this is mandatory). In this particular case it is just sufficient to define the getBooks method since there is no need of the books property in the controller class

我一直在尝试在我的 eclipse 和 tomcat 7 中实现这样的想法。但它一直困扰着我:

javax.el.ELException: /views/books/listAll.xhtml @9,60 value="#{bookController.books}": Error reading 'books' on type pl.ctrl.BookController

我的问题是,是否有可能:

<h:dataTable value="#{bookController.books}" var="b">

#{bookController} 托管 bean 中没有 books 属性,只有 getBooks() getter 方法?

最佳答案

你的问题和书上说的不一样。如果 JSF/EL 无法找到完整的 getter 方法,您将得到以下异常:

javax.el.PropertyNotFoundException: Property 'books' not found on type pl.ctrl.BookController

或者如果它无法完整地找到 bean 本身:

javax.el.PropertyNotFoundException: Target Unreachable, identifier 'bookController' resolved to null

但是你却得到了:

javax.el.ELException: Error reading 'books' on type pl.ctrl.BookController

这意味着找到了 bean 和 getter 方法,但是调用 getter 方法抛出了异常。基本上,以下是在 JSF/EL 的掩护下发生的:

try {
    Object result = bookController.getBooks();
} catch (Exception e) {
    throw new ELException("Error reading 'books' on type pl.ctrl.BookController", e);
}

注意 e 作为 ELException 的原因被传递。因此,原始异常必须在您没有在问题的任何地方发布的堆栈跟踪中进一步显示为“由”引起。最底层的是一切的根本原因,是您具体问题的答案。如果您无法解释它,只需将异常类型和消息复制粘贴到合适的搜索引擎中即可找到答案和线索。


与具体问题无关,从 getter 方法抛出的异常反过来表明代码有问题。 getter 方法不应该执行任何异常敏感的业务逻辑。即,它可以在每个 bean 的生命周期中被多次调用,并且在 bean 的整个生命周期中一遍又一遍地重复完全相同的业务逻辑显然是低效的。停止这样做并将业务逻辑移动到一次性初始化或操作/事件监听器方法。 getter 方法必须只返回已经准备好的属性。另见 Why JSF calls getters multiple timesHow and when should I load the model from database for h:dataTable .

关于jsf - javax.el.E​​LException : Error reading 'foo' on type com. 示例.Bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34196075/

相关文章:

java - Java JSF 的 Facebook 点赞按钮?

cocoa - bool 值作为 Cocoa 中的属性

java - 双向bean同步

iphone - 将 Obj-C 2.0 属性与可变对象(例如 NSMutableArray)一起使用的最佳方式是什么?

jsf - EL 条件方法表达式

java - 什么时候销毁数据库连接?

jsf - JSF 中的重复 ID

jsp - ${fn :escapeXml(var)} does not display the value of var

sql - 在jsp中以表格形式显示数据

jsf - PrimeFaces 自定义错误页面