blob: e08c920e2e5ce003ba6f6afebb0ff804843fe0d4 [file] [log] [blame]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!--
Copyright 1999-2004 The Apache Software Foundation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- Content Stylesheet for Site -->
<!-- start the processing -->
<!-- ====================================================================== -->
<!-- GENERATED FILE, DO NOT EDIT, EDIT THE XML FILE IN xdocs INSTEAD! -->
<!-- Main Page Section -->
<!-- ====================================================================== -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<meta name="author" value="Jon S. Stevens">
<meta name="email" value="jon@latchkey.com">
<title>Velocity - You make the decision - JavaBeans</title>
</head>
<body bgcolor="#ffffff" text="#000000" link="#525D76">
<table border="0" width="100%" cellspacing="0">
<!-- TOP IMAGE -->
<tr>
<td align="left">
<a href="http://jakarta.apache.org"><img src="http://jakarta.apache.org/images/jakarta-logo.gif" border="0"/></a>
</td>
<td align="right">
<a href="http://jakarta.apache.org/velocity/"><img src="../images/logo.gif" alt="Velocity" border="0"/></a>
</td>
</tr>
</table>
<table border="0" width="100%" cellspacing="4">
<tr><td colspan="2">
<hr noshade="" size="1"/>
</td></tr>
<tr>
<!-- LEFT SIDE NAVIGATION -->
<td width="20%" valign="top" nowrap="true">
<!-- ============================================================ -->
<p><strong>About</strong></p>
<ul>
<li> <a href="../index.html">Overview</a>
</li>
<li> <a href="../getting-started.html">Getting Started</a>
</li>
<li> <a href="http://jakarta.apache.org/builds/jakarta-velocity/">Download</a>
</li>
<li> <a href="../install.html">Install</a>
</li>
<li> <a href="../design.html">Design</a>
</li>
<li> <a href="../contributors.html">Contributors</a>
</li>
<li> <a href="../changes.html">ChangeLog</a>
</li>
<li> <a href="../code-standards.html">Coding Standards</a>
</li>
<li> <a href="../license.html">License</a>
</li>
<li> <a href="../todo.html">TODO</a>
</li>
<li> <a href="http://issues.apache.org/bugzilla/enter_bug.cgi?product=Velocity">Report Issues</a>
</li>
</ul>
<p><strong>Community</strong></p>
<ul>
<li> <a href="../powered.html">Powered By Velocity</a>
</li>
<li> <a href="http://jakarta.apache.org/site/getinvolved.html">Get Involved</a>
</li>
<li> <a href="http://jakarta.apache.org/site/mail.html">Mailing Lists</a>
</li>
<li> <a href="http://jakarta.apache.org/site/cvsindex.html">CVS Repositories</a>
</li>
</ul>
<p><strong>Docs</strong></p>
<ul>
<li> <a href="../user-guide.html">User's Guide (English)</a>
</li>
<li> <a href="../user-guide_fi.html">User's Guide (Finnish)</a>
</li>
<li> <a href="../user-guide_fr.html">User's Guide (French)</a>
</li>
<li> <a href="../user-guide_es.html">User's Guide (Spanish)</a>
</li>
<li> <a href="../developer-guide.html">Developer's Guide</a>
</li>
<li> <a href="../vtl-reference-guide.html">VTL Reference Guide</a>
</li>
<li> <a href="../specification.html">Specification</a>
</li>
<li> <a href="../api/index.html">Javadoc</a>
</li>
</ul>
<p><strong>Tools</strong></p>
<ul>
<li> <a href="../tools/index.html">Velocity Tools</a>
</li>
<li> <a href="../anakia.html">Anakia : XML->doc tool</a>
</li>
<li> <a href="../texen.html">Texen : text generation</a>
</li>
<li> <a href="../dvsl/index.html">DVSL : XML xformation</a>
</li>
<li> <a href="../veltag.html">Veltag : JSP taglib</a>
</li>
<li> <a href="../migration.html">Migration to Velocity</a>
</li>
<li> <a href="../devtools.html">Editors and IDEs</a>
</li>
</ul>
<p><strong>Comparisons</strong></p>
<ul>
<li> <a href="../ymtd/ymtd.html">YMTD</a>
</li>
<li> <a href="../differences.html">VM/WM Differences</a>
</li>
<li> <a href="../casestudy1.html">JSP vs. Velocity</a>
</li>
<li> <a href="../casestudy2.html">XMLC vs. Velocity</a>
</li>
</ul>
<p><strong>Site Translations</strong></p>
<ul>
<li> <a href="http://jakarta.apache.org/velocity/">English</a>
</li>
<li> <a href="http://www.ingrid.org/jajakarta/velocity/velocity-1.2-rc2/docs-ja/index.html">Japanese</a>
</li>
</ul>
</td>
<td width="80%" align="left" valign="top">
<table border="0" cellspacing="0" cellpadding="2" width="100%">
<tr><td bgcolor="#525D76">
<font color="#ffffff" face="arial,helvetica,sanserif">
<a name="JavaBeans"><strong>JavaBeans</strong></a>
</font>
</td></tr>
<tr><td>
<blockquote>
<p>
JavaBeans are the way to use Java objects from JSP pages in order to
follow the MVC design pattern. The point of doing this is to implement
something similar to the <a href="http://jakarta.apache.org/turbine/turbine-2/pullmodel.html">Pull
methodology</a>. For example:
</p>
<div align="left">
<table cellspacing="4" cellpadding="0" border="0">
<tr>
<td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td>
<td bgcolor="#023264" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td>
<td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td>
</tr>
<tr>
<td bgcolor="#023264" width="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td>
<td bgcolor="#ffffff"><pre>
&lt;jsp:useBean id=&quot;name&quot; scope=&quot;page|request|session|application&quot;
class=&quot;className&quot; type=&quot;typeName&quot;&gt;
</pre></td>
<td bgcolor="#023264" width="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td>
</tr>
<tr>
<td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td>
<td bgcolor="#023264" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td>
<td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td>
</tr>
</table>
</div>
<p>
Examining the syntax of the above code, the first thing that pops up
right away is the use of the scope attribute. How many HTML designers
understand the programming concepts of scope? It is safe to suggest that
a good portion of web designers barely understand the concept of how a
CGI works. By stating this, we are not trying to slight people. Instead,
we are simply pointing out that design and software engineering are
distinct skill sets. You wouldn't expect a Java programmer to select a
print and web safe color palette, would you?
</p>
<p>
The common response to an argument like this is that the designers
should simply ignore these tags and let others define and implement
them. The problem with that is that you have now given them the power to
accidentally wreck your entire application in such a way that it is
very difficult to debug because a complex scope issue might not show up
right away.
</p>
<div align="left">
<table cellspacing="4" cellpadding="0" border="0">
<tr>
<td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td>
<td bgcolor="#023264" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td>
<td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td>
</tr>
<tr>
<td bgcolor="#023264" width="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td>
<td bgcolor="#ffffff"><pre>
The Java code:
public class HelloBean {
private String name = &quot;World&quot;;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
The JSP code:
&lt;jsp:useBean id=&quot;hello&quot; class=&quot;HelloBean&quot;&gt;
&lt;jsp:setProperty name=&quot;hello&quot; property=&quot;*&quot; /&gt;
&lt;/jsp:useBean&gt;
&lt;HTML&gt;
&lt;HEAD&gt;&lt;TITLE&gt;Hello&lt;/TITLE&gt;&lt;/HEAD&gt;
&lt;BODY&gt;
&lt;H1&gt;
Hello, &lt;jsp:getProperty name=&quot;hello&quot; property=&quot;name&quot; /&gt;
&lt;/H1&gt;
&lt;/BODY&gt;
&lt;/HTML&gt;
</pre></td>
<td bgcolor="#023264" width="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td>
</tr>
<tr>
<td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td>
<td bgcolor="#023264" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td>
<td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td>
</tr>
</table>
</div>
<p>
Above, we have a very simple example of using a bean in a page. Pass it
some properties and then retrieve the results. This is the right way to
do things when using JSP. However, if we look at an example of doing the
same exact thing in Velocity, the extra amount of needless typing that
one needs to perform to simply retrieve a property seems a just bit
absurd. Of course there are always GUI based drag and drop tools to make
typing a thing of the past. Really.
</p>
<p>
There are several commercial solutions available today which provide a
nice drag and drop view for doing development with JSP and Struts.
However many of these tools are still first generational tools. They
typicially only address parts of the problem and require digging down
into the nitty gritty stuff when things become difficult or even
impossible to do with the GUI (anyone remember a product called Tango?).
Often these tools also produce code that is not optimized for heavily
hit sites and getting an existing application to scale sometimes
requires a complete rewrite. Again, this is not our decision, it is
yours. Another item to note here is that these are costly (&gt;$1000/seat)
development tools. In this .bomb economy, who really has the money to
spend on these tools?
</p>
<div align="left">
<table cellspacing="4" cellpadding="0" border="0">
<tr>
<td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td>
<td bgcolor="#023264" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td>
<td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td>
</tr>
<tr>
<td bgcolor="#023264" width="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td>
<td bgcolor="#ffffff"><pre>
The Java code:
context.put (&quot;hello&quot;, new HelloBean());
The Velocity code:
$hello.setName(&quot;*&quot;)
&lt;HTML&gt;
&lt;HEAD&gt;&lt;TITLE&gt;Hello&lt;/TITLE&gt;&lt;/HEAD&gt;
&lt;BODY&gt;
&lt;H1&gt;
Hello, $hello.Name
&lt;/H1&gt;
&lt;/BODY&gt;
&lt;/HTML&gt;
</pre></td>
<td bgcolor="#023264" width="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td>
</tr>
<tr>
<td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td>
<td bgcolor="#023264" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td>
<td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td>
</tr>
</table>
</div>
<p>
The example shows the creation of the HelloBean() object and then
placing it into the Context. Then, during runtime execution of the
template, that object is available as a $variable which uses the
JavaBean specification to do introspection on the object. For example,
Velocity uses Bean style introspection to permit the method call to be
shortened from <code>$hello.getName()</code> to simply typing what is
shown above.
</p>
<p>
When Velocity is combined with Turbine, the HelloBean object can be
added into the Context as a configuration option or it can be added at
any point of the processing. This is what provides the "scope" of the
object in the Context.
</p>
<p>
Another "gotcha" with using JavaBeans in JSP is again quoted from Jason's
book:
</p>
<div align="left">
<table cellspacing="4" cellpadding="0" border="0">
<tr>
<td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td>
<td bgcolor="#023264" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td>
<td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td>
</tr>
<tr>
<td bgcolor="#023264" width="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td>
<td bgcolor="#ffffff"><pre>
One thing to watch out for: On some servers (including Tomcat 3.2) if
you have a bean with a scope of &quot;session&quot; or &quot;application&quot; and you
change the bean class implementation, you may get a ClassCastException
on a later request. This exception occurs because the generated servlet
code has to do a cast on the bean instance as it's retrieved from the
session or application, and the old bean type stored in the session or
application doesn't match the new bean type expected. The simplest
solution is to restart the server.
</pre></td>
<td bgcolor="#023264" width="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td>
</tr>
<tr>
<td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td>
<td bgcolor="#023264" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td>
<td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td>
</tr>
</table>
</div>
<p>
You make the decision.
</p>
<p>
<strong>[ <a href="ymtd-error-handling.html">Error Handling</a> &lt;- Previous |
Next -&gt; <a href="./ymtd-sampleapp.html">Sample Application</a> ]
</strong></p>
</blockquote>
</p>
</td></tr>
<tr><td><br/></td></tr>
</table>
</td>
</tr>
<!-- FOOTER -->
<tr><td colspan="2">
<hr noshade="" size="1"/>
</td></tr>
<tr><td colspan="2">
<div align="center"><font color="#525D76" size="-1"><em>
Copyright &#169; 1999-2004, The Apache Software Foundation
</em></font></div>
</td></tr>
</table>
</body>
</html>
<!-- end the processing -->