JSF隐藏异常?

时间:2010-05-31 23:39:07

标签: java jsf jpa

我有一个JSF页面的托管bean,它在构造函数中执行JPA调用以填充bean中的字段。我在另一个持久化实体的调用(填充数据以进行测试)时遇到了一些麻烦。我期待它会抛出某种异常,因为它不起作用,但我没有得到任何东西。我刚刚尝试了以下几点:

Query newQuery = em.createQuery("Bad Syntax");
List newList = newQuery.getResultList();

我希望这里有IllegalArgumentException,因为查询字符串完全无效,但页面仍然加载,我在任何地方都看不到任何例外。

我是否正确期待这个例外?如果是这样,为什么我没有看到它?

2 个答案:

答案 0 :(得分:1)

默认情况下不会禁止例外。作为快速测试,我尝试了以下内容:

package com.example;

import javax.faces.bean.ManagedBean;

@ManagedBean
public class Bean {

    public Bean() {
        throw new IllegalArgumentException("No!");
    }

}

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" 
      xmlns:h="http://java.sun.com/jsf/html">
    <h:head>
        <title>test</title>
    </h:head>
    <h:body>
        #{bean}
    </h:body>
</html>

我在webbrowser中得到了一个很好的默认错误页面,其中包含以下跟踪:

javax.el.ELException: /test.xhtml: Cant instantiate class: com.example.Bean.
    at com.sun.faces.facelets.compiler.TextInstruction.write(TextInstruction.java:83)
    at com.sun.faces.facelets.compiler.UIInstructions.encodeBegin(UIInstructions.java:75)
    at com.sun.faces.facelets.compiler.UILeaf.encodeAll(UILeaf.java:176)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1616)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1616)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:380)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:126)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:127)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:313)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
    at java.lang.Thread.run(Thread.java:619)
Caused by: com.sun.faces.mgbean.ManagedBeanCreationException: Cant instantiate class: com.example.Bean.
    at com.sun.faces.mgbean.BeanBuilder.newBeanInstance(BeanBuilder.java:193)
    at com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:102)
    at com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:405)
    at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:267)
    at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:86)
    at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:54)
    at com.sun.faces.el.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:72)
    at org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:61)
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186)
    at com.sun.faces.facelets.el.ELText$ELTextVariable.writeText(ELText.java:217)
    at com.sun.faces.facelets.el.ELText$ELTextComposite.writeText(ELText.java:141)
    at com.sun.faces.facelets.compiler.TextInstruction.write(TextInstruction.java:78)
    ... 25 more
Caused by: java.lang.IllegalArgumentException: No!
    at com.example.Bean.<init>(Bean.java:9)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at java.lang.Class.newInstance0(Class.java:355)
    at java.lang.Class.newInstance(Class.java:308)
    at com.sun.faces.mgbean.BeanBuilder.newBeanInstance(BeanBuilder.java:188)
    ... 36 more
1-jun-2010 13:51:06 org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet Faces Servlet threw exception
java.lang.IllegalArgumentException: No!
    at com.example.Bean.<init>(Bean.java:9)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at java.lang.Class.newInstance0(Class.java:355)
    at java.lang.Class.newInstance(Class.java:308)
    at com.sun.faces.mgbean.BeanBuilder.newBeanInstance(BeanBuilder.java:188)
    at com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:102)
    at com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:405)
    at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:267)
    at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:86)
    at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:54)
    at com.sun.faces.el.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:72)
    at org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:61)
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186)
    at com.sun.faces.facelets.el.ELText$ELTextVariable.writeText(ELText.java:217)
    at com.sun.faces.facelets.el.ELText$ELTextComposite.writeText(ELText.java:141)
    at com.sun.faces.facelets.compiler.TextInstruction.write(TextInstruction.java:78)
    at com.sun.faces.facelets.compiler.UIInstructions.encodeBegin(UIInstructions.java:75)
    at com.sun.faces.facelets.compiler.UILeaf.encodeAll(UILeaf.java:176)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1616)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1616)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:380)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:126)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:127)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:313)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
    at java.lang.Thread.run(Thread.java:619)

所以你的问题可能就在其他地方。你运行了调试器吗?

答案 1 :(得分:0)

好吧,似乎NetBeans没有正确地重新部署所有内容存在某种问题。在我最初进行故障排除时,我一直在运行干净的构建,但是在让事情停留了几天并回到它之后,我现在看到了我期望看到的内容。

  
    

引起:java.lang.IllegalArgumentException:在EntityManager中创建查询时发生异常:     异常描述:语法错误解析查询[错误语法],第1行,第0列:意外令牌[错误]。

  

我不确定我从中学到了什么教训,但它确实删除了我对NetBeans的一些信任。