JSP JavaBean
JavaBean is a special Java class, written in Java language, and complies with the JavaBean API specification.
The following are the unique characteristics of JavaBean compared with other Java classes:
Provide a default no-argument constructor.
Needs to be serialized and implements the Serializable interface.
There may be a range of readable and writable properties.
There may be a series of "getter" or "setter" methods.
JavaBean Properties
The properties of a JavaBean object should be accessible. This attribute can be any legal Java data type, including custom Java classes.
The properties of a JavaBean object can be read-write, read-only, or write-only. The properties of a JavaBean object are accessed through the two methods provided in the JavaBean implementation class:
Method | Description |
---|---|
getPropertyName() | For example, if the name of the attribute is myName, then the name of the method should be written as getMyName() to read the attribute. This method is also called an accessor. |
setPropertyName() | For example, if the name of the attribute is myName, then the name of the method should be written as setMyName() to write the attribute. This method is also called a writer. |
A read-only property only provides the getPropertyName() method, and a write-only property only provides the setPropertyName() method.
JavaBean program example
This is the StudentBean.java file:
package com.php; 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(int age) { this.age = age; } }
Compile the StudentBean.java file (will be used in the last example):
$ javac StudentsBean.java
After compilation, obtain the StudentBean.class file and copy it to <JSP Project>/WebContent/WEB-INF/classes/com/php, as shown in the figure below :
Access JavaBean
<jsp:useBean> tag can declare a JavaBean in JSP and then use it. After declaration, JavaBean objects become script variables and can be accessed through script elements or other custom tags. The syntax format of the <jsp:useBean> tag is as follows:
<jsp:useBean id="bean 的名字" scope="bean 的作用域" typeSpec/>
Among them, depending on the specific situation, the value of scope can be page, request, session or application. The id value can be arbitrary as long as it is not the same as the id value in other <jsp:useBean> in the same JSP file.
What follows is a simple usage of the <jsp:useBean> tag:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <html> <head> <title>useBean 实例</title> </head> <body> <jsp:useBean id="date" class="java.util.Date" /> <p>日期为:<%= date %> </body> </html>
It will produce the following results:
日期为:Tue Jun 28 15:22:24 CST 2016
Access The properties of the JavaBean object
are used in the <jsp:useBean> tag body using the <jsp:getProperty/> tag to call the getter Method, use the <jsp:setProperty/> tag to call the setter method, the syntax format is as follows:
<jsp:useBean id="id" class="bean 编译的类" scope="bean 作用域"> <jsp:setProperty name="bean 的 id" property="属性名" value="value"/> <jsp:getProperty name="bean 的 id" property="属性名"/> ........... </jsp:useBean>
The name attribute refers to the id attribute of the Bean . The property attribute refers to the getter or setter method you want to call.
The following is a simple example of using the above syntax for attribute access:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <html> <head> <title>get 和 set 属性实例</title> </head> <body> <jsp:useBean id="students" class="com.php.StudentsBean"> <jsp:setProperty name="students" property="firstName" value="小强"/> <jsp:setProperty name="students" property="lastName" value="王"/> <jsp:setProperty name="students" property="age" value="10"/> </jsp:useBean> <p>学生名字: <jsp:getProperty name="students" property="firstName"/> </p> <p>学生姓氏: <jsp:getProperty name="students" property="lastName"/> </p> <p>学生年龄: <jsp:getProperty name="students" property="age"/> </p> </body> </html>
Access the above JSP, and the running results are as follows:
学生名字: 小强 学生姓氏: 王 学生年龄: 10