Trouble with AssertionFailure and bean:write
Hello. Can anybody help me?
I'm developing a global error page for struts web application. There is a code snippet:
<logic:present name="<%=org.apache.struts.Globals.EXCEPTION_KEY%>" scope="request">
<bean:define id="ex" name="<%=org.apache.struts.Globals.EXCEPTION_KEY%>"
scope="request" type="java.lang.Exception"/>
<bean:write name="ex" property="message" scope="page"/>
</logic:present>
It work fine until exception is instance of AssertionFailure. If it is an AssertionFailure, I got an error:
"No getter method for property message of bean ex"
But <%= ex.getMessage ()%>
works fine for any type of exception.
What's wrong?
no. look:
<logic:present name="<%=org.apache.struts.Globals.EXCEPTION_KEY%>" scope="request">
- here I look up for Globals.EXCEPTION_KEY request attribute;
<bean:define id="ex" name="<%=org.apache.struts.Globals.EXCEPTION_KEY%>"
scope="request" type="java.lang.Exception"/>
- here I define a new bean name "ex" into page scope and assign it value of Globals.EXCEPTION_KEY request attribute;
<bean:write name="ex" property="message" scope="page"/>
- here I use bean define on previous step.
I guess problem is somewhere else..
Quite right. I guess I'm not quite awake yet.
Ok, next question: I don't know the class AssertionFailure.
Its not a standard java API class: There is java.lang.AssertionError, but that would produce a ClassCastException as it is an Error, not an Exception.
Is it the Hibernate class you are referring to?org.hibernate.AssertionFailure?
Or some other class?
Yes, it is.
java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
org.hibernate.exception.NestableRuntimeException
org.hibernate.AssertionFailure
There is no ClassCastException, but struts bean:write and jstl c:out couldn't find property message. By the way, they find property class.name perfectly. And scriplet <%= ex.getMessage () %>
works fine.
I've found a workaround defining a special wrapper-class for java.lang.Throwable with String properties className and message. I works, but it's ugly a bit.
[nobr]I threw together the following test page, to try and duplicate the error:
<%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean"%>
<%@ taglib prefix="logic" uri="http://struts.apache.org/tags-logic"%>
<html>
<h1>Test page</h1>
<h2> Server Info </h2>
Server info = <%= application.getServerInfo() %> <br>
Servlet engine version = <%= application.getMajorVersion() %>.<%= application.getMinorVersion() %><br>
Java version = <%= System.getProperty("java.vm.version") %><br>
Java home = <%= System.getProperty("java.home") %><br>
Session id = <%= session.getId() %><br>
<%
AssertionError assertionError = new AssertionError("This is a test Assertion Error");
org.hibernate.AssertionFailure assertionFailure = new org.hibernate.AssertionFailure("Testing hibernate one");
Exception myException = new Exception("Testing an exception");
request.setAttribute(org.apache.struts.Globals.EXCEPTION_KEY, assertionFailure);
%>
<logic:present name="<%=org.apache.struts.Globals.EXCEPTION_KEY%>" scope="request">
<bean:define id="ex" name="<%=org.apache.struts.Globals.EXCEPTION_KEY%>"
scope="request" type="java.lang.Exception"/>
<bean:write name="ex" property="message" scope="page"/>
</logic:present>
</BODY>
</HTML>
It runs fine for me on Tomcat5.0.28, with Hibernate 3.1[/nobr]