JavaBeans components are Java classes that can be easily reused and composed together into applications. Any Java class that follows certain design conventions can be a JavaBeans component.
DQM technology directly supports using JavaBeans components with dqm language elements. You can easily create and initialize beans and get and set the values of their properties. This chapter provides information about JavaBeans components and the dqm language elements for accessing beans in your dqm pages.
JavaBeans are usual Java classes which adhere to certain coding conventions. Following are the coding conventions I am talking about:
1. Must public class
2. Implements java.io.Serializable interface
3. Provides no argument constructor
4. Provides getter and setter methods for accessing it's properties
Use JavaBean has two advantages:
1. Causes HTML and the java procedure separates, can advantageous for the maintenance code.
2. DQM scripting language focused on generating dynamic web page, JavaBean focused on transaction processing, like this can fully use the reusability of JavaBean component, improve the efficiency of the development web site.
We give an example of JavaBean, “mybean.Calculate.java” is a JavaBean, it’s used to calculate the data, it’s source code:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package mybean;
public class Calculate { public Calculate() { }
//Adder calculation public double add() { return num1 + num2; }
//Subtraction calculation public double minus() { return num1 - num2; }
public void setNum1(double inum1) { num1 = inum1; }
public void setNum2(double inum2) { num2 = inum2; }
private double num1; private double num2; } |
In this JavaBean we can set two float numbers use “setNum1” and “setNum2” methods, execute “add” and “minus” methods can get calculated results.
Please compile above java file and put it’s class file into “/WEB-INF/classes” folder (notice the file’s package name, full class path is “/WEB-INF/classes/mybean/Calculate.class”)
In dqm file we can use java code or DQM JavaBean tags to declare and instantiate the JavaBean class. We recommend use DQM JavaBean tags, this can reduce the java code in dqm file. Next we will introduce DQM JavaBean tags.
DQM JavaBean tags syntax:
<%@bean id="object-name" class="classname" scope="object scope"%>
Let us now see what are the different attributes :
id - name of the object e.g.
String myName = null;
In the above code, “myName” is the 'id' we are talking about.
scope - an optional attribute by which you can control when your JavaBean object will be destroyed. Default is page, which means every page view will create a new JavaBean. About scope we will introduce in the next section.
class - a class name e.g.
Date d = new java.util.Date();
In the above code, “java.util.Date” is the class we are talking about. If not import package path then need to write fully qualified class name.
We now give an example, “useJavaBean.dqm” used the above “mybean.Calculate” JavaBean, source code as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
<%@bean id="myBean" class="mybean.Calculate" scope="page"%>
<% //set two float numbers which need to calculate myBean.setNum1(12.23); myBean.setNum2(0.88); %>
<pre> Result of add: <%=myBean.add()%> Result of subtration: <%=myBean.minus()%> </pre> |
At source code 1 program used DQM JavaBean tag to declare and instantiate the “mybean.Calculate” JavaBean. Next we set two float numbers which need to calculate (source code line: 5 ,6), then calculate these two numbers and display result (source code line: 10, 11).
|
Result of add: 13.110000000000001 Result of subtration: 11.35 |
Although this JavaBean looks like very not necessary, such a simple calculation program we can write directly in the dqm file, why use javaBean? Certainly I also have such idea, but here just an example. But if you need to process the massive complicated calculations then this has the advantage when using JavaBean. It can causes HTML and the java procedure separates, can advantageous for the maintenance code. If the calculation needs to change that basically only revises the code in JavaBean, in dqm file's code only needs very little change or even without any change.
Every JavaBean class object or any other class object we create will have a scope. Scope means the length of time this object will remain in memory. There are three kinds of scopes :
1. page - it means a new object will be created and destroyed for every page view. This is the default for DQM JavaBean tag when you don't explicitly give it any.
2. session - the newly created object will be bound to the session object. What this means is that every visitor coming to your site will have a separate session for it, so you will not have to create a new object every time for it. You can just retrieve that object later again from the session object when you want it.
3. application - an object bound to application object means that your object will stay as long as the application remains loaded. This can be useful when for instance you want to count page views or daily sessions for your site.
For example, first we create a javaBean: “mybean.CounterBean”, in this bean we declare a value, it’s name is “value”, and we create methods of set and get this value. Source code:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package mybean;
public class CounterBean { public CounterBean() { }
public int getCount() { return count; }
public void setCount(int count) { this.count = count; }
private int count = 0; } |
Next we create a new dqm file “counterBean.dqm”, this dqm file will use “CounterBean, it’s source code:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<%@page import = "mybean.CounterBean"%> <%@ bean id="myBean" scope="page" class="mybean.CounterBean"%>
<pre> The current counter's value is: <%=myBean.getCount()%>
<%//Increase count value myBean.setCount(myBean.getCount() + 1);
//Next judgment myBean object scope CounterBean obj = null; String scope = null;
//If object is session scope obj = (CounterBean)session.getAttribute("myBean"); if (obj != null) scope = "session";
//If object is application scope obj = (CounterBean)application.getAttribute("myBean"); if (obj != null) scope = "application";
//If object scope is not session or application then must is page if (scope == null) scope = "page"; %>
scope=<%=scope%> </pre> |
At above source code line 2, you can seen that JavaBean object scope is “page”, we visit this dqm file you will see result:
|
The current counter's value is: 0 scope=page |
You can try to refresh this page, but you will find “count” variable’s value always display 0. Because JavaBean object will be created and destroyed for every page view.
Next we change JavaBean object scope to “session”, change source code line 2 to
<%@ bean id="myBean" scope="session" class="mybean.CounterBean"%>
We visit it again, first you will see “count” variable’s value is 0, but if you refresh it then “count” variable’s value will plus 1 every time. But if you close browser and open browser visit it again then “count” variable’s value become 0 again. Because created JavaBean object will be bound to the session object. By the way if use session scope then we can directly get JavaBean object from session too (source code line 15).
Finally we change JavaBean object scope to “session”, change source code line 2 to
<%@ bean id="myBean" scope="application" class="mybean.CounterBean"%>
If you refresh this dqm file then “count” variable’s value will plus 1 every time. If you close browser and open browser visit it again then “count” variable’s value not will reset to 0 and it will continue to increase. Because created JavaBean object will be bound to application object. By the way if use application scope then we can directly get JavaBean object from application too (source code line 20).