What am I missing in regards to understanding the EL
Hello. I am trying to learn JSP and when I try to access a bean property using the EL, it is being evaluated as a boolean type when it is actually a double. First, here is the bean code:
package com.caci.CurrencyConverter2;
/*
* CurrencyConverter.java
*
* Created on 04 Nov 2002, 15:51
*/
/**
* A simple JavaBean to convert US Dollar amounts into Euros.
*/
public class CurrencyConverter {
private double amount2 = 0.0;
//conversion rate as of 04 Nov, 2002
private final static double rate = 1.0056;
/**
* Accessor method to obtain the converted amount
* @return the converted amount
*/
public double getAmount2() {
return amount2;
}
/**
* Accessor method to calculate the converted amount
* @param double the converted amount
*/
public void setAmount2(double amount) {
this.amount2 = amount * rate;
System.out.println("The amount: " + this.amount2);
}
/**
* Boolean test to check that amount has been set
* @return true or false indicating whether an amount has been set
*/
public boolean isAmount2()
{
if (amount2 > 0.0)
{
return true;
}
else
{
return false;
}
}
}
Next, here is my JSP:
%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>U.S.-to-Euro Currency Converter</title>
<jsp:useBean id="myConverter" class="com.caci.CurrencyConverter2.CurrencyConverter" scope="session"/>
</head>
<body>
<h1>U.S.-to-Euro Currency Converter</h1>
<form method="GET">
Value to convert: <input type="text" name="amount2">
<input type="submit">
<jsp:setProperty name="myConverter" property="*"/>
</form>
<c:if test="${myConverter.amount2 > 0.0}">
<jsp:getProperty name="myConverter" property="amount2"/>
</c:if>
</body>
</html>
If I try to run this, I get the following exception:
javax.servlet.jsp.el.ELException: Attempt to coerce a boolean value "false" to type "java.lang.Double"
org.apache.commons.el.Logger.logError(Logger.java:481)
org.apache.commons.el.Logger.logError(Logger.java:498)
org.apache.commons.el.Logger.logError(Logger.java:566)
org.apache.commons.el.Coercions.coerceToPrimitiveNumber(Coercions.java:422)
org.apache.commons.el.Coercions.applyRelationalOperator(Coercions.java:906)
org.apache.commons.el.RelationalOperator.apply(RelationalOperator.java:85)
org.apache.commons.el.GreaterThanOperator.apply(GreaterThanOperator.java:119)
org.apache.commons.el.BinaryOperatorExpression.evaluate(BinaryOperatorExpression.java:170)
org.apache.commons.el.ExpressionEvaluatorImpl.evaluate(ExpressionEvaluatorImpl.java:263)
org.apache.commons.el.ExpressionEvaluatorImpl.evaluate(ExpressionEvaluatorImpl.java:190)
org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:899)
org.apache.jsp.index_jsp._jspx_meth_c_if_0(index_jsp.java:116)
org.apache.jsp.index_jsp._jspService(index_jsp.java:90)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
Based on the stack trace, this expression occurs when the if-condition is being evaluated. It appears that when the EL evaluates ${myConverter.amount2}, it calls the isAmount2() method of the bean, which returns a boolean. It appears that I have to have this method declared, because if I comment out isAmount2(), I will receive the following exception after clicking on the Submit button:
java.lang.NoSuchMethodError: com.caci.CurrencyConverter2.CurrencyConverter.isAmount2()Z
I must not be understanding something about the EL. Can someone fill me in on what I am missing or point me to some online documentation that provides a good explanation of the EL? Thanks.

