JSP - JavaBeans
JavaBean 是一个按照 JavaBeans API 规范编写的特殊构造的 Java class。
以下是使 JavaBean 与其他 Java class 区别开来的独特特性 −
它提供了一个默认的无参 constructor。
它应该是可序列化的,并实现 Serializable interface。
它可能具有多个可以读写 properties。
它可能具有多个用于 properties 的 "getter" 和 "setter" methods。
JavaBeans Properties
JavaBean property 是一个命名属性,可以被对象的使用者访问。该属性可以是任何 Java 数据类型,包括您定义的 classes。
JavaBean property 可以是 可读、可写、只读 或 只写。JavaBean properties 通过 JavaBean 实现 class 中的两个 methods 进行访问 −
| 序号 | Method & 描述 |
|---|---|
| 1 | getPropertyName() 例如,如果 property name 是 firstName,则您的 method name 应该是 getFirstName() 来读取该 property。此 method 称为 accessor。 |
| 2 | setPropertyName() 例如,如果 property name 是 firstName,则您的 method name 应该是 setFirstName() 来写入该 property。此 method 称为 mutator。 |
只读属性只有 getPropertyName() method,只写属性只有 setPropertyName() method。
JavaBeans 示例
考虑一个具有几个 properties 的学生 class −
package com.;
public class StudentsBean implements java.io.Serializable {
private String firstName = null;
private String lastName = null;
private int age = 0;
public StudentsBean() {
}
public String getFirstName(){
return firstName;
}
public String getLastName(){
return lastName;
}
public int getAge(){
return age;
}
public void setFirstName(String firstName){
this.firstName = firstName;
}
public void setLastName(String lastName){
this.lastName = lastName;
}
public void setAge(Integer age){
this.age = age;
}
}
访问 JavaBeans
useBean action 在 JSP 中声明一个 JavaBean 用于使用。一旦声明,该 bean 就成为一个 scripting variable,可以被 JSP 中的 scripting elements 和其他 custom tags 访问。useBean tag 的完整语法如下 −
<jsp:useBean id = "bean's name" scope = "bean's scope" typeSpec/>
这里 scope attribute 的值可以是 page、request、session 或 application,根据您的需求而定。id attribute 的值可以是任何值,只要它是同一 JSP 中其他 useBean declarations 中的唯一名称即可。
以下示例展示了如何使用 useBean action −
<html>
<head>
<title>useBean Example</title>
</head>
<body>
<jsp:useBean id = "date" class = "java.util.Date" />
<p>The date/time is <%= date %>
</body>
</html>
您将收到以下结果 − −
The date/time is Thu Sep 30 11:18:11 GST 2010
访问 JavaBeans 属性
除了 <jsp:useBean...> 动作之外,您还可以使用 <jsp:getProperty/> 动作来访问 get 方法,以及 <jsp:setProperty/> 动作来访问 set 方法。以下是完整的语法 −
<jsp:useBean id = "id" class = "bean's class" scope = "bean's scope">
<jsp:setProperty name = "bean's id" property = "property name"
value = "value"/>
<jsp:getProperty name = "bean's id" property = "property name"/>
...........
</jsp:useBean>
name 属性引用了之前通过 useBean 动作引入到 JSP 中的 JavaBean 的 id。property 属性是要调用的 get 或 set 方法的名称。
以下示例展示了如何使用上述语法访问数据 −
<html>
<head>
<title>get and set properties Example</title>
</head>
<body>
<jsp:useBean id = "students" class = "com..StudentsBean">
<jsp:setProperty name = "students" property = "firstName" value = "Zara"/>
<jsp:setProperty name = "students" property = "lastName" value = "Ali"/>
<jsp:setProperty name = "students" property = "age" value = "10"/>
</jsp:useBean>
<p>Student First Name:
<jsp:getProperty name = "students" property = "firstName"/>
</p>
<p>Student Last Name:
<jsp:getProperty name = "students" property = "lastName"/>
</p>
<p>Student Age:
<jsp:getProperty name = "students" property = "age"/>
</p>
</body>
</html>
让我们将 StudentsBean.class 放入 CLASSPATH 中。访问上述 JSP,将显示以下结果 −
Student First Name: Zara Student Last Name: Ali Student Age: 10