Java Reference
In-Depth Information
Use the import pop-up
. If you have import pop-ups turned on (the default
setting), the editor will realize that the
Currency
class isn't currently in
scope. It will note the error and pop up a small intention window asking
where the class comes from. Pressing
Alt+Enter
lets you choose which
Currency
class on the module's Classpath should be imported and inserts
an appropriate
insert
statement for you.
■
Use refactoring
. While your cursor is still on the
getCurrencies()
method,
invoke the
Introduce Variable
refactoring (
Ctrl+Alt+V
).
IDEA
will cor-
rectly identify the variable type as an
Iterator
and suggest a variable name
based on its code style configuration. Either change the name or accept
the default, but click
OK
to accept the variable.
■
Use live templates
. With the
Iterator
defined, move your cursor to the next
new line and invoke the
Insert Live Template
function (
Ctrl+J
). Choose
the
itit
live template, which iterates through a list of objects defined by an
Iterator
(you can choose the option by using the arrow keys to navigate
the list or by typing the letters
itit
and thereby narrowing the list to a sin-
gle entry). The shortest way, however, is to type
itit
in the editor and
press the
Ta b
key. The live template walks you through each of the vari-
ables to ensure they're contextually sound. The suggested iterator is likely
correct; the object being cast to should not be a generic
Object
but rather a
Currency
, and the variable name can be anything you wish.
■
Listing 11.3
A complete JSP with dynamic elements implemented in scriptlets
<%@ page import="com.acme.conversion.currency.Currency,
java.util.Iterator"%>
<html>
<head>
<title>ACME Currency Converter</title>
</head>
<body>
<h1>ACME Currency Converter</h1>
<p>
Welcome to the ACME Currency converter! Please fill in the
form below and submit it to perform a mock currency exchange.
</p>
<form method="get" action="results.jsp">
Starting currency:
<select name="startingCurrency">
<%
Iterator startIter = Currency.getCurrencies();
while (startIter.hasNext()) {
String currency = (String) startIter.next();
out.println("<option value=\"" + currency + "\">" +