JavaBean + [use JavaBean in JSP]: < jsp:useBean >, < jsp:setProperty >, < jsp:getProperty >

Posted by mise_me_fein on Tue, 25 Jan 2022 23:29:16 +0100

0.JavaBean features

JavaBean is a special Java class, which is written in Java language and complies with JavaBean API specification.

What follows is the unique features of JavaBean s compared with other Java classes:

  • Provide a default parameterless constructor.
  • It needs to be serialized and implements the Serializable interface. (this is to be determined)
  • There may be a series of private read-write properties.
  • There may be a series of getter or setter methods.

1.JavaBean properties: getPropertyName(), setPropertyName()

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 JavaBean objects are accessed through two methods provided in the JavaBean implementation class:

A read-only property only provides the getPropertyName() method, and a write only property only provides the setPropertyName() method.

If the field is XYZ, the name of the read-write method starts with get and set respectively, followed by the field name XYZ starting with an uppercase letter. Therefore, the names of the two read-write methods are getXyz() and setXyz() respectively

2.JavaBean program example: studentbean java

package com.runoob;

public class StudentsBean implements java.io.Serializable
{
//-----------------------------Is a private property--------------------------------------------------
   private String firstName = null;
   private String lastName = null;
   private int age = 0;
//------------------------------Nonparametric structure-------------------------------------------------
   public StudentsBean() {
   }
//--------------------------------getXxxxx()-----------------------------------------------   
   public String getFirstName(){
      return firstName;
   }
   public String getLastName(){
      return lastName;
   }
   public int getAge(){
      return age;
   }
//-----------------------------setXxxxx()--------------------------------------------------
   public void setFirstName(String firstName){
      this.firstName = firstName;
   }
   public void setLastName(String lastName){
      this.lastName = lastName;
   }
   public void setAge(int age) {
      this.age = age;
   }
}



3. Use JavaBean in JSP

JSP technology provides three action elements about JavaBean components, namely JSP tags, which are respectively:

  • < jsp: usebean > tag: used to find or instantiate a JavaBean component in a JSP page.
  • Jsp: setproperty > tag: used to set the properties of a JavaBean component in a JSP page.
  • Jsp: getproperty > tag: used to get the properties of a JavaBean component in a JSP page.

3.1. < jsp: usebean > tag

The < jsp: usebean > tag is used to find the JavaBean object with the specified name within the specified domain. If it exists, it will directly return the reference of the JavaBean object. If it does not exist, it will instantiate a new JavaBean object and store it in the specified domain with the specified name.

Common syntax:

<jsp:useBean   id="beanName"    class="package.class"     scope="page|request|session|application"/>

The "id" attribute is used to specify the reference name of the JavaBean instance object and its name stored in the domain scope.
The "class" attribute is used to specify the full class name of the JavaBean (that is, it must have a package name).
"scope" attribute is used to specify the domain range stored by JavaBean instance object. Its value can only be one of page, request, session and application. Its default value is page.

Usage example:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<jsp:useBean id="person" class="gacl.javabean.study.Person" scope="page"/>
 <%
     //The person object has been instantiated with jsp:useBean tag above, so you can directly use the person object here
    
     person.setName("Lonely Wolf");
    
     person.setSex("male");
    
     person.setAge(24);

     person.setMarried(false);
 %>
 <!DOCTYPE HTML>
 <html>
   <head>
     <title>jsp:useBean Example of label usage</title>
   </head>
   
   <body>
     <%--use getXxx()Method to get the property value of the object --%>
     <h2>full name:<%=person.getName()%></h2>
     <h2>Gender:<%=person.getSex()%></h2>
     <h2>Age:<%=person.getAge()%></h2>
     <h2>Married:<%=person.isMarried()%></h2>
   </body>
 </html>

The operation results are as follows:



3.2 < jsp: usebean > tag with tag body

Syntax:

<jsp:useBean ...>  
Body  
</jsp:useBean>  

Function:
The content of the Body section is executed only when the < jsp: usebean > tag creates an instance object of a JavaBean. This method is not used much. Just learn about it

3.3 execution principle of < jsp: usebean > (skip)

We're on the index JSP uses < jsp: usebean id = "person" class="gacl.javabean.study.Person"scope = "page" / > to instantiate a GaCl javabean. study. The object of the person class. How is this person object instantiated? index.jsp will first be translated into a servlet during execution, so we can check the index The java code of the servlet generated by the JSP page to view the instantiation process of the Pearson object

Find the directory "work\Catalina\localhost \ project name \ org\apache\jsp" under the tomcat server, and you can see the index The JSP page is translated into the java source code of the servlet, as follows:

Open index using a text editor_ jsp. Java file, in_ The creation process of the person object can be seen in the jspService method, as shown below:

1      gacl.javabean.study.Person person = null;
2       synchronized (_jspx_page_context) {
3         person = (gacl.javabean.study.Person) _jspx_page_context.getAttribute("person", PageContext.PAGE_SCOPE);
4         if (person == null){
5           person = new gacl.javabean.study.Person();
6           _jspx_page_context.setAttribute("person", person, PageContext.PAGE_SCOPE);
7         }
8       }

Let's analyze the code generated above:

The first is to define a person object whose value is null

gacl.javabean.study.Person person = null;//Define an empty person object

Then use the getAttribute method of pageContext object to get the information stored in pageContext PAGE_ Person object in scope domain

person = (gacl.javabean.study.Person) _jspx_page_context.getAttribute("person", PageContext.PAGE_SCOPE);

If in pageContext PAGE_ If the person object in the scope field cannot find the person object, create a new person object, and then use the setAttribute method of the pageContext object to store the newly created person in the pageContext PAGE_ In scope domain

if (person == null){
          person = new gacl.javabean.study.Person();
          _jspx_page_context.setAttribute("person", person, PageContext.PAGE_SCOPE);
}

That is, in index JSP uses < jsp:useBean id = "person" class="gacl.javabean.study.Person"scope = "page" / > to instantiate the person object. In fact, the above java code is executed to instantiate the person object. This is the implementation principle of jsp:useBean tag: "first, find the JavaBean object with the specified name within the specified domain. If it exists, directly return the reference of the JavaBean object. If it does not exist, instantiate a new JavaBean object and store it in the specified domain with the specified name


3.4. < jsp: setproperty > tag

The < jsp: setproperty > tag is used to set and access the properties of JavaBean objects.
Syntax format 1:

< jsp:setProperty   name="beanName"   property="propertyName"   value="string character string"/>

Syntax format 2:

< jsp:setProperty    name="beanName"   property="propertyName"  value="<%=expression %>" />

Syntax format 3: use request parameters to assign values to bean attributes

< jsp:setProperty    name="beanName"    property="propertyName"   param="parameterName"/>

Syntax format 4: assign values to bean attributes with all request parameters

< jsp:setProperty    name="beanName"    property= "*" />

Example 1 of syntax formats 1 and 2:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<jsp:useBean id="person" class="gacl.javabean.study.Person" scope="page"/>

<%--
use jsp:setProperty Label settings person Property value of the object
jsp:setProperty When setting the property value of the object, the string will be automatically converted into 8 basic data types
 however jsp:setProperty Cannot convert automatically for composite data types
--%>
<jsp:setProperty property="name" name="person" value="White tiger Emperor"/>
<jsp:setProperty property="sex" name="person" value="male"/>
<jsp:setProperty property="age" name="person" value="24"/>
<jsp:setProperty property="married" name="person" value="false"/>
<%--
birthday Property is a Date Type, which is a composite data type, so the string cannot be automatically converted to Date ,If you write it in the following way, you will report an error
<jsp:setProperty property="birthday" name="person" value="1988-05-07"/>
--%>
<jsp:setProperty property="birthday" name="person" value="<%=new Date()%>"/>
<!DOCTYPE HTML>
<html>
  <head>
    <title>jsp:setProperty Example of label usage</title>
  </head>
  
  <body>
    <%--use getXxx()Method to get the property value of the object --%>
    <h2>full name:<%=person.getName()%></h2>
    <h2>Gender:<%=person.getSex()%></h2>
    <h2>Age:<%=person.getAge()%></h2>
    <h2>Married:<%=person.isMarried()%></h2>
    <h2>date of birth:<%=person.getBirthday()%></h2>
  </body>
</html>

The operation effect is as follows:

Syntax format 3 example 2: use request parameters to assign values to bean properties

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<jsp:useBean id="person" class="gacl.javabean.study.Person" scope="page"/>

<%--
    jsp:setProperty Tags can use request parameters as bean Attribute assignment of
    param="param_name"The parameter name used to receive is param_name And then assign the received value to name attribute
--%>
<jsp:setProperty property="name" name="person" param="param_name"/>

<!DOCTYPE HTML>
<html>
  <head>
    <title>jsp:setProperty Example of label usage</title>
  </head>
  
  <body>
      <%--use getXxx()Method to get the property value of the object --%>
       <h2>full name:<%=person.getName()%></h2>
  </body>
</html>

The operation results are as follows:

Syntax format 4 Example 3: assign values to bean attributes with all request parameters

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<jsp:useBean id="person" class="gacl.javabean.study.Person" scope="page"/>

<%--
    jsp:setProperty The tag uses all request parameters as bean Attribute assignment of
    property="*"representative bean All properties of
--%>
<jsp:setProperty property="*" name="person"/>

<!DOCTYPE HTML>
<html>
  <head>
    <title>jsp:setProperty Example of label usage</title>
  </head>
  
  <body>
    <%--use getXxx()Method to get the property value of the object --%>
    <h2>full name:<%=person.getName()%></h2>
    <h2>Gender:<%=person.getSex()%></h2>
    <h2>Age:<%=person.getAge()%></h2>
  </body>
</html>

The operation results are as follows:


3.5. < jsp: getproperty > tag

The < jsp: getproperty > tag is used to read the properties of the JavaBean object, that is, call the getter method of the JavaBean object, and then convert the read property value into a string and insert it into the output response body.
Syntax:

<jsp:getProperty   name="beanInstanceName"     property="PropertyName" />  
  • The name attribute is used to specify the name of the JavaBean instance object, and its value should be the same as the id attribute value of the < jsp: usebean > tag.
  • The property property is used to specify the property name of the JavaBean instance object. If the value of a property of a JavaBean instance object is null, the result of outputting the property using the < jsp: getproperty > tag will be a string with "null" content.

Example: use jsp:getProperty to get the property value of the bean object

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<jsp:useBean id="person" class="gacl.javabean.study.Person" scope="page"/>

<%--
use jsp:setProperty Label settings person Property value of the object
jsp:setProperty When setting the property value of the object, the string will be automatically converted into 8 basic data types
 however jsp:setProperty Cannot convert automatically for composite data types
--%>
<jsp:setProperty property="name" name="person" value="White tiger Emperor"/>
<jsp:setProperty property="sex" name="person" value="male"/>
<jsp:setProperty property="age" name="person" value="24"/>
<jsp:setProperty property="married" name="person" value="false"/>
<%--
birthday Property is a Date Type, which is a composite data type, so the string cannot be automatically converted to Date ,If you write it in the following way, you will report an error
<jsp:setProperty property="birthday" name="person" value="1988-05-07"/>
--%>
<jsp:setProperty property="birthday" name="person" value="<%=new Date()%>"/>
<!DOCTYPE HTML>
<html>
  <head>
    <title>jsp:getProperty Example of label usage</title>
  </head>
  
  <body>
    <%--use jsp:getProperty Tag gets the property value of the object --%>
    <h2>full name:<jsp:getProperty property="name" name="person"/></h2>
    <h2>Gender:<jsp:getProperty property="sex" name="person"/></h2>
    <h2>Age:<jsp:getProperty property="age" name="person"/></h2>
    <h2>Married:<jsp:getProperty property="married" name="person"/></h2>
    <h2>date of birth:<jsp:getProperty property="birthday" name="person"/></h2>
  </body>
</html>

The operation results are as follows:

Topics: javabean