2010-05-31 104 views
3

我有一個JSF頁面的託管bean,它在構造函數中執行JPA調用來填充bean中的字段。我在繼續調用一個實體(填充數據以進行測試)方面遇到了一些麻煩。我期待它拋出某種異常,因爲它不起作用,但我沒有得到任何東西。只是它的赫克我試過如下:JSF隱藏異常?

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

我期望的IllegalArgumentException在這裏,因爲查詢字符串是完全無效,但頁面仍然加載,我看不出有任何異常的任何地方。

我是否期待這種例外?如果是這樣,爲什麼我沒有看到它?

+1

您是否嘗試過在查詢執行後查看「newList」的值是什麼? – pakore 2010-06-01 09:32:42

回答

0

那麼,NetBeans似乎存在某種問題,無法正確重新部署所有內容。當我開始排除故障時,我一直在運行乾淨的版本,但在讓事情坐了幾天回去之後,我現在看到了我期望看到的內容。

所致:java.lang.IllegalArgumentException異常: 異常描述::語法錯誤解析查詢[壞語法],第1行,第0列:意外的標記[同時創造在EntityManager的查詢時發生異常壞]。

我不知道什麼教訓,我真的從中學到,但它肯定刪除了一些我信任的NetBeans。

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> 

和我同在網頁瀏覽器下面的跟蹤一個不錯的默認錯誤頁:

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) 

所以你的問題可能在別的地方。你運行了一個調試器嗎?

+0

我已經運行了一個調試器,但我不確定要查找什麼,所以目前我沒有任何有用的信息。我注意到的一件事是,它似乎沒有停止在創建查詢的行上。 – bshacklett 2010-06-01 19:08:56

+0

然後你沒有運行你認爲你正在運行的代碼。 – BalusC 2010-06-01 19:18:15

+0

如果一條線在它周圍沒有分支時可以跳過這條線嗎?我想,我必須發佈這個bean的內容。今晚我會盡力做到這一點。 – bshacklett 2010-06-02 16:45:45