The combination of JavaBeans and JavaServer Pages
Through the above learning, we should have a basic understanding of JavaBean. For calling JavaBeans in the assumerver pages, we also need to know some specific JSP content. Let's take a look.
JavaBean-related labels in JavaServer Pages
In assumerver pages, calling JavaBean has three standard labels: <JSP: usebean>, <JSP: setproperty>, and <JSP: getproperty>.
<JSP: usebean> tag
<JSP: usebean> you can define a JavaBean instance with a certain survival range and a unique ID. In this way, assumerver pages identifies JavaBean by ID or by ID. method similar statements to operate on JavaBean.
During execution, <JSP: usebean> first tries to find an existing JavaBean instance with the same ID and scope value. If not, a new instance is automatically created.
The syntax is as follows:
<JSP: usebean id = "name"
Scope = "Page | request | session | application"
Typespec>
Body
</Jsp: usebean>
The typespec is defined as follows:
Typespec: = Class = "classname"
| Class = "classname" type = "typename"
| Type = "typename" class = "classname"
| Beanname = "beanname" type = "typename"
| Type = "typename" beanname = "beanname"
| Type = "typename"
The following table lists the meanings of related attributes in the <JSP: usebean> tag.
Attributes and definitions
ID
The ID attribute is the unique identifier of a JavaBean object and represents an instance of a JavaBean object. It has a specific scope of existence (page | request | session | application ). The ID is used to identify the JavaBean in the assumerver pages.
Scope
The scope attribute represents the survival time of the JavaBean object, which can be page, request, session, or application.
Class
Indicates the Class Name of the JavaBean object. Note that the case sensitivity must be the same.
Beanname
The beanname attribute represents the bean name, which is usually initialized using the instantiate () method of Java. Beans. Beans class ..
Type
The Type attribute specifies the type of the script variable definition. By default, the script variable definition is consistent with the class attribute. Generally, the default value is used.
<JSP: setproperty> tag
Another standard tag is the <JSP: setproperty> tag, which is mainly used to set the bean property value. The syntax used in assumerver pages is as follows:
<JSP: setproperty name = "beanname" last_syntax/>
The name attribute represents an existing JavaBean instance with a certain scope. Last_syntax indicates the following syntax: Property = "*" |
Property = "propertyname" |
Property = "propertyname" Param = "parametername" |
Property = "propertyname" value = "propertyvalue"
The basic attributes and meanings of the <JSP: setproperty> tag are as follows:
Attributes and definitions
Name
Name indicates the JavaBean object instance defined by the <JSP: usebean> label.
Property
This is a very important property, representing the name of the property you want to set. If property = "*" is used, the program repeatedly searches for all the current servletrequest parameters and matches the property with the same name in the JavaBean, the Set Method of the attribute in Javabean is used to assign value to this attribute. If the value attribute is empty, the attribute value in the JavaBean is not modified.
Param
The param attribute represents the parameter name of the page request. The <JSP: setproperty> label cannot use both PARAM and value.
Value
The Value Attribute represents the specific value of the property assigned to the bean.
<JSP: getproperty> tag
The last tag is the <JSP: getproperty> tag. it can get the attribute values of the JavaBean instance and convert them to Java. lang. string, and finally placed in the hidden out object. the JavaBean instance must be defined before <JSP: getproperty>.
<JSP: getproperty> the tag syntax is as follows:
<JSP: getproperty name = "name" property = "propertyname"/>
The basic attributes and meanings of the <JSP: getproperty> tag are as follows:
Attributes and definitions
Name
The name attribute indicates the bean instance for which the attribute value is to be obtained. The bean instance must be defined using the <JSP: usebean> label before.
Property
The property represents the name of the property to obtain the value.
Author's blog:Http://blog.csdn.net/liujun999999/