|
This page is displayed when an error occurs anywhere within the mortgage calculator
JavaServer(tm) page and is not caught there. Notice that the errorPage attribute near the top of that page links to this one.
We will display the calculator HTML form and fill each field with info from the
corresponding request object parameter and write an error message in red above each field that contains an error.
This allows the user to see and correct just the field that caused the error
without entering all the other information again.
If we had not specified an error page, information from the exception object would be displayed by default. While this
may be useful for debugging, it is not acceptable for most viewers
|
mortgageErrors.jsp
<%-- ------------------------------------------------------------------
Declare this page as an error page. The exception object is only
available to pages with isErrorPage set to true.
------------------------------------------------------------------ --%>
<%@ page language="java" isErrorPage="true" %>
<%-- ------------------------------------------------------------
This custom tag library contains actions to set and format
BigDecimal numbers and other actions specific to the TVM bean.
------------------------------------------------------------- --%>
<%@ taglib
uri="http://www.getobjects.com/taglibs/go"
prefix="go"
%>
<%-- ------------------------------------------------------------
Create an instance of the TVM bean. This bean contains all of
the financial calculator logic and stores the state (presentValue,
futureValue, interestRate, etc.) for a particular session.
This bean was instantiated in the mortgage.jsp page.
-------------------------------------------------------------- --%>
<jsp:useBean
id="tvm"
class="com.cedarspring.tvm.TVMu"
scope="session">
</jsp:useBean>
<go:press name="tvm" button="clearMessage" />
<%-- ------------------------------------------------------------------
Display the calculator HTML form and fill each field with info from the
corresponding request object parameter. Parse and validate each parameter
and write an error message in red above each field that contains an error.
The present and future value parameters are valid if they can
be parsed to any BigDecimal value. The setDecimalProperty tag
will throw an exception if they are not.
<p>The remaining parameters must also be checked to see if they are
within a specified range (i.e. interestRate is < 0). The TVM bean will
set the message property if they are not.
------------------------------------------------------------------- --%>
<html>
<head>
<title>Error Page for Mortgage Payment Calculator</title>
</head>
<body>
<div align="center"><center>
<form method="post" action="mortgage.jsp">
<p>
<h2>Mortgage Payment Calculator</h2>
<table border="0" cellpadding="5" cellspacing="0">
<%-- -----------------------------------------------------------------
Validate the presentValue field
----------------------------------------------------------------- --%>
<% try { %>
<go:setDecimalProperty
name="tvm"
property="presentValue" />
<% } catch (Exception e) { %>
<tr>
<td colspan="2">
<font color="red"><%= "Value is not numeric:" %></font>
</td>
</tr>
<% } %>
<tr>
<td><b>Beginning Balance:</b></td>
<td><input
name="presentValue"
type="text" size="20"
value='<%= request.getParameter("presentValue") %>'
</td>
</tr>
<%-- -----------------------------------------------------------------
Validate the futureValue field
----------------------------------------------------------------- --%>
<% try { %>
<go:setDecimalProperty
name="tvm"
property="futureValue" />
<% } catch (Exception e) { %>
<tr>
<td colspan="2">
<font color="red"><%= "Value is not numeric:" %></font>
</td>
</tr>
<% } %>
<tr>
<td><b>Ending Balance:</b></td>
<td><input
name="futureValue"
type="text" size="20"
value="<%= request.getParameter("futureValue") %>"
</td>
</tr>
<%-- -----------------------------------------------------------------
Validate the interestRate field
----------------------------------------------------------------- --%>
<% try { %>
<go:setDecimalProperty
name="tvm"
property="interestRate" />
<% } catch (Exception e) { %>
<tr>
<td colspan="2">
<font color="red"><%= "Value is not numeric:" %></font>
</td>
</tr>
<% }
if (!tvm.getMessage().equals("")) { %>
<tr>
<td colspan="2">
<font color="red"><%= tvm.getMessage() %></font>
</td>
</tr>
<% tvm.clearMessage();
} %>
<tr>
<td><b>Interest Rate:</b></td>
<td><input
name="interestRate"
type="text" size="5"
value="<%= request.getParameter("interestRate") %>"
</td>
</tr>
<%-- -----------------------------------------------------------------
Validate the numberOfPeriods field
----------------------------------------------------------------- --%>
<% try { %>
<go:setDecimalProperty
name="tvm"
property="numberOfPeriods" />
<% } catch (Exception e) { %>
<tr>
<td colspan="2">
<font color="red"><%= "Value is not numeric:" %></font>
</td>
</tr>
<% }
if (!tvm.getMessage().equals("")) { %>
<tr>
<td colspan="2">
<font color="red"><%= tvm.getMessage() %></font>
</td>
</tr>
<% tvm.clearMessage();
} %>
<tr>
<td><b>Number Of Periods:</b></td>
<td><input
name="numberOfPeriods"
type="text" size="5"
value="<%= request.getParameter("numberOfPeriods") %>"
</td>
</tr>
<tr>
<%-- -----------------------------------------------------------------
Validate the paymentsPerYear field
----------------------------------------------------------------- --%>
<% try { %>
<jsp:setProperty
name="tvm"
property="paymentsPerYear" />
<% } catch (Exception e) { %>
<tr>
<td colspan="2">
<font color="red"><%= "Value is not numeric:" %></font>
</td>
</tr>
<% }
if (!tvm.getMessage().equals("")) { %>
<tr>
<td colspan="2">
<font color="red"><%= tvm.getMessage() %></font>
</td>
</tr>
<% tvm.clearMessage();
} %>
<tr>
<td><b>Payments Per Year:</b></td>
<td><input
name="paymentsPerYear"
type="text" size="5"
value="<%= request.getParameter("paymentsPerYear") %>"
</td>
</tr>
<%-- -----------------------------------------------------------------
Validate the compoundingPerYear field
----------------------------------------------------------------- --%>
<% try { %>
<jsp:setProperty
name="tvm"
property="compoundingPerYear" />
<% } catch (Exception e) { %>
<tr>
<td colspan="2">
<font color="red"><%= "Value is not numeric:" %></font>
</td>
</tr>
<% }
if (!tvm.getMessage().equals("")) { %>
<tr>
<td colspan="2">
<font color="red"><%= tvm.getMessage() %></font>
</td>
</tr>
<% tvm.clearMessage();
} %>
<tr>
<td><b>Compounding Per Year:</b></td>
<td><input
name="compoundingPerYear"
type="text" size="5"
value="<%= request.getParameter("compoundingPerYear") %>"
</td>
</tr>
<tr>
<td colspan="2"><hr></td>
</tr>
</table>
<p>
<input type="submit" value="Calculate Payment">
<%-- -----------------------------------------------------------------
Finally, test for any errors that occur when payment is calculated.
----------------------------------------------------------------- --%>
<go:press name="tvm" button="computePayment" />
<p>
<center><font color="red"><%= tvm.getMessage() %></font></center>
<go:press name="tvm" button="clearMessage" />
</form>
<p>
</center></div>
</body>
</html>
|