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.

[4766 byte] By [zjw2112a] at [2007-11-27 6:28:31]
# 1

See this:

public boolean isAmount2()

and this:

<c:if test="${myConverter.amount2 > 0.0}">

the amount2 property is fetched from your isAmount2() method, which returns a boolean. You are comparing a boolean to a double now, and that is not possible. You should change the name of your isAmount2() method so EL will call the getAmount2() method in stead.

gimbal2a at 2007-7-12 17:51:48 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

I tried changing the name of isAmount2() (made it sillyAmount2()) and I also tried commenting out the method. Both cases resulted in the following exception:

java.lang.NoSuchMethodError: com.caci.CurrencyConverter2.CurrencyConverter.isAmount2()Z

I do not call isAmount2() in my JSP file. It seems that the Expression language or Tomcat or someone is requiring me to have the isAmount2() method declared even though the amount2 property is a double, not a boolean. Why is the software expecting an isXXX method to be there for a non-boolean type?

zjw2112a at 2007-7-12 17:51:48 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
Also, as an FYI, I am using Tomcat 5.0.28 and the Jakarta JSTL v1.1.2 (jakarta-taglibs-standard-1.1.2).
zjw2112a at 2007-7-12 17:51:48 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Your initial analysis was correct. You confused the bean introspector by providing both getAmount2 and isAmount2 which are both accessors for the property amount2.

Changing the name of the method should fix it though. At a guess, you haven't recompiled everything that needs to be recompiled.

1 - Recompile the java class CurrencyConverter.

2 - Deploy it into Tomcat

3 - Change something on the jsp page (so it will get recompiled)

4 - restart Tomcat.

At that point things should start acting correctly.

If that doesn't work try stopping Tomcat and deleting the directories tmp and work from the [TOMCAT_HOME] folder.

Cheers,

evnafets

evnafetsa at 2007-7-12 17:51:48 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
Evnafets was correct. Tomcat was using old versions of files. I'm not sure why Eclipse wasn't updating correctly. Thanks for the help.
zjw2112a at 2007-7-12 17:51:48 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...