|
Primer : Using JSP Beans |
|
|
The cornerstone of the J2EE model is the ability to make use of classes stored within the
Smalltalk image. The method by which you have access to the objects in the image is via a
mechanism called JavaBeans, or just simply Beans.
|
|
This lesson describes how to use beans to access objects.
|
|
1.
If the Web Toolkit is not already running and your sandbox site not configured, please do
so now.
2. Below is an over-simplified graphic that exemplifies what beans are all about.
Figure 1. Accessing objects in the Smalltalk image from a web page JavaBeans, or just beans, in 25 words or less, allow us to create and access objects from web pages. They are the J2EE version of the session variables in the ASP model (23 words!). 3. For this example, we will create a simple class (StringBean) and use J2EE techniques to manipulate an instance of the StringBean class.
Figure 2. The StringBean class and the initialize method 4. Using the System Browser in VisualWorks, create the StringBean class in the category of ZZZ (we chose the category of ZZZ because it would be easy to find - at the very bottom of the category list).
Smalltalk.VisualWave defineClass: #StringBean
superclass: #{Core.Object}
indexedType: #none
private: false
instanceVariableNames: 'quote quotation '
classInstanceVariableNames: ''
imports: ''
category: 'ZZZ'
5. In your favorite text editor, enter the following and save this file as bean1.ssp in the sandbox directory (just copy from here and paste it into your editor).
<HTML>
<HEAD> <TITLE>Using JavaBeans with SSP</TITLE> </HEAD> <BODY> <H1>Using JavaBeans with SSP</H1> <jsp:usebean id="stringBean" class="VisualWave.StringBean" scope="request"/> <LI>Initial value (getProperty): <I><jsp:getproperty name="stringBean" property="getQuote" /></I> <LI>Same value (ASP expression): <I><%= stringBean getQuote %></I> <br><br> Now changing the quote...using jsp:setproperty <br><br> <jsp:setproperty name="stringBean" property="setQuote" value="Best string bean is Fortex" /> <LI>Initial value (getProperty): <I><jsp:getproperty name="stringBean" property="getQuote" /></I> <LI>Same value (ASP expression): <I><%= stringBean getQuote %></I> <br><br> Now changing the quote...using ASP expression <br><br> <% stringBean setQuote: 'My favorite is Kentucky Wonder'. %> <LI>Initial value (getProperty): <I><jsp:getproperty name="stringBean" property="getQuote" /></I> <LI>Same value (ASP expression): <I><%= stringBean getQuote %></I> </BODY> </HTML> |
|
There is a lot going on here so we will break this down line by line.
<jsp:usebean id="stringBean" class="VisualWave.StringBean" scope="request"/>
The "tag" <jsp:usebean /> is one of many JSP expressions that use an XML syntax. It is equivalent <jsp:usebean > </jsp:usebean >, but since all the action happens with the attributes of this tag, the XML syntax is preferred in the JSP camp. You will see this with all other expressions that use the jsp: tag.
<jsp:getproperty name="stringBean" property="getQuote" />
The "tag" <jsp:getproperty /> is used to execute the method specified by the property attribute for the object (bean) specified by the name attribute. The expression above would be equivalent to:
<jsp:setproperty name="stringBean" property="setQuote" value="Best string bean is Fortex" />
The "tag" <jsp:setproperty /> is used to execute the method specified by the property attribute for the object (bean) specified by the name attribute. The parameter to be passed to the method is specified by the value attribute. The expression above would be equivalent to:This page shows an example of using both ASP and JSP techniques for "setting and getting" properties from our object. Even though we used a JSP convention to create our object (jsp:usebean), we could still make reference to is using ASP expressions. Next we will change the scope attribute to session to see if our object persists across multiple pages. |
|
6.
In your favorite text editor, enter the following and save this file as bean2.ssp
in the sandbox directory (just copy from here and paste it into your editor).
<HTML>
7.
In your favorite text editor, enter the following and save this file as bean3.ssp
in the sandbox directory (just copy from here and paste it into your editor).
<HEAD> <TITLE>Using JavaBeans with SSP</TITLE> </HEAD> <BODY> <H1>Using JavaBeans with SSP</H1> <jsp:useBean id="stringBean" class="VisualWave.StringBean" scope="session"/> <LI>Initial value (getProperty): <I><jsp:getProperty name="stringBean" property="quotation" /></I> <LI>Same value (ASP expression): <I><%= stringBean quotation %></I> <br><br> Now changing the quote...using jsp:setproperty <br><br> <jsp:setProperty name="stringBean" property="quotation" value="Best bean is Fortex" /> The quote should now have been changed <br><br> <LI>Initial value (getProperty): <I><jsp:getProperty name="stringBean" property="quotation" /></I> <LI>Same value (ASP expression): <I><%= stringBean quotation %></I> <br><br> Click <a href=Bean3.ssp>here</a> to see if it's true on the next page. </BODY> </HTML>
<HTML>
8.
Let's fire these pages up in our browser.
<HEAD> <TITLE>Using JavaBeans with SSP</TITLE> </HEAD> <BODY> <H1>Using JavaBeans with SSP</H1> <jsp:useBean id="stringBean" class="VisualWave.StringBean" scope="session"/> <LI>Initial value (getProperty): <I><jsp:getProperty name="stringBean" property="quotation" /></I> <LI>Same value (ASP expression): <I><%= stringBean quotation %></I> <br><br> </BODY> </HTML>
Figure 3. The beans2.ssp page
Figure 4. The beans3.ssp page Sure enough, the scope attribute of session allowed us to set the instance variable of quote on one page to a different string than its default and value and have it persist on a second page. |
|
SummaryUsing beans is really quite simple. Use the <jsp:usebean /> expression to instanciate the object, <jsp:getproperty /> to retrieve the value of an instance variable of that object (bean) and <jsp:setproperty /> to set the value of an instance variable of that object (bean). |