JDK版本:jdk1.6.0
Tomcat 版本:apache-tomcat-6.0.18
<error-page>
<error-code>401</error-code>
<location>/401.htm</location>
</error-page>
<error-page>
<error-code>403</error-code>
<location>/403.htm</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/404.htm</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/500.htm</location>
</error-page>
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/errorProcess.jsp</location>
</error-page>
模拟出错场景:客户发送请求在先
客户端得到的新闻链接存在 恰好后台管理员客户单击连接前删除了该新闻
向服务器端请求的路径如下:detail.jsp?i=402881e41e743103011e7447faa90005
新闻详细页detail.jsp中处理了该请求 会调用service类处理 在不存在该记录时 hibernate会抛出NotObject异常
疑惑:异常本应该本tomcat截获并处理 跳转到/errorProcess.jsp 进行友好提示 但是事实是:仍会跳转到detail.jsp页面 可想而知这个页面的遭遇是面目全非
期待大家的回复
[quote]load
public Object load(Class theClass,
Serializable id)
throws HibernateException
Return the persistent instance of the given entity class with the given identifier, assuming that the instance exists. This method might return a proxied instance that is initialized on-demand, when a non-identifier method is accessed.
You should not use this method to determine if an instance exists (use get() instead). Use this only to retrieve an instance that you assume exists, where non-existence would be an actual error.
Parameters:
theClass - a persistent class
id - a valid identifier of an existing persistent instance of the class
Returns:
the persistent instance or proxy
Throws:
HibernateException[/quote]
这是从hibernate api 文档中session的load方法的介绍,人家告诉你了:你不能用这个方法来判定实体是否存在,而应该用get()替代。
我试验了一下,
get()的话,如果不存在,返回的是null
load()则返回的是一个代理对象,如果你不使用这个对象,不会有任何问题,如果访问这个对象,就会有一个异常,如:org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [test.UserTable#920]
所以,我建议你使用get。
我怀疑你是把hibernate抛出的异常catch了,但是在catch块里面什么处理都没做,所以还是返回到了deail.jsp,建议你检查一下。
还有,像这类常见的异常,还是应该自己捕获,然后forward到友好的界面。
的防守打法的防守打法
阿凡达阿打发