Jsp learning-Definition of JavaBean and use of JavaBean in Jsp

Source: Internet
Author: User

I. javaBean1. what is JavaBeanJavaBean is a Java class that follows a specific writing method. It generally has the following features:> this Java class must have a no-argument constructor> the property must be privatized> the private property must be exposed to other programs through the public type method, in addition, methods must follow certain naming rules. Although Sun allows Java developers to design a JavaBean as powerful as a Swing component when defining the JavaBean specification, in actual J2EE development, generally, only the most basic features of the above JavaBean are used: JavaBean is usually used to encapsulate data in J2EE development. For the JavaBean Component that follows the preceding writing, other programs can instantiate the JavaBean object through the reflection technology, in addition, by reflecting those methods that comply with naming conventions, you can learn the attributes of JavaBean and call its attributes to save data. 2. The attributes of JavaBean can be of any type, and a JavaBean can have multiple attributes. Each property usually requires the corresponding setter and getter methods. The setter method is called the attribute modifier, and the getter method is called the property accesser (like write and read ). The property modifier must start with a lower-case set prefix, followed by the property name, and the first letter of the property name must be changed to uppercase. For example, the modifier name of the name attribute is setName, the modifier name of the password attribute is setPassword. The property accessors usually start with a lower-case get prefix, followed by the property name, and the first letter of the property name should also be changed to uppercase. For example, the name of the property accessors is getName, the accessor name of the password attribute is getPassword. A property of a JavaBean can only contain the set method or get method. Such an attribute is generally called a write-only or read-only attribute. II. in Jsp, The JavaBeanJSP technology provides three action elements about the JavaBean Component, namely, JSP labels, which are: <jsp: useBean> labels: it is used to find or instantiate a JavaBean Component on the JSP page. <Jsp: setProperty> tag: Used to set attributes of a JavaBean Component on the JSP page. <Jsp: getProperty> tag: used to obtain the attributes of a JavaBean Component on the JSP page. 1. The <jsp: userBean> tag <jsp: useBean> tag is used to search for the JavaBean object of the specified name within the specified domain range. If yes, the reference of the JavaBean object is directly returned. If it does not exist, instantiate a new JavaBean object and store it in the specified domain range 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 the name stored in the domain. The class attribute is used to specify the complete class Name of the JavaBean (that is, the package name must be included ). The scope attribute is used to specify the domain range stored by the JavaBean instance object. The value can only be one of four values, including page, request, session, and application. The default value is page. <Jsp: useBean> execution principle: <jsp: useBean id = "currentDate" class = "java. util. date "/> translated Servlet Source Code: java. util. date currentDate = null; synchronized (_ jspx_page_context) {currentDate = (java. util. date) _ jspx_page_context.getAttribute ("currentDate", PageContext. PAGE_SCOPE); if (currentDate = null) {currentDate = new java. util. date (); _ jspx_page_context.setAttribute ("currentDate", currentDate, PageContext. PAGE_SCOPE) ;}} <Jsp: useBean> tag syntax with a tag body: <jsp: useBean...> body </jsp: useBean> function: the Body content is only executed when the <jsp: useBean> tag creates an instance object for JavaBean. 2. The <jsp: setProperty> tag is used to set and access attributes of a JavaBean object. Syntax format: <jsp: setProperty name = "beanName" {property = "propertyName" value = "{string | <% = expression %>}" | property = "propertyName" [param = "parameterName"] | property = "*"}/> name attribute is used to specify the name of the JavaBean object. The property attribute is used to specify the attribute name of the JavaBean instance object. The value attribute is used to specify the value of an attribute of a JavaBean object. The value can be a string or expression. If the value is a string, it is automatically converted to the corresponding type of the JavaBean attribute. If the value is an expression, the calculation result of this expression must be consistent with the type of the JavaBean attribute to be set. The param attribute is used to set a property value of a JavaBean instance object to a request parameter value. The property value is also automatically converted to the type of the JavaBean attribute to be set. 3. <jsp: getProperty> tag is used to read attributes of a JavaBean object, that is, to call the getter method of a JavaBean object, then, convert the read property value to 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. The value must be the same as <jsp: useBean> the id attribute value of the tag is the same. The property attribute is used to specify the attribute name of the JavaBean instance object. If the value of a property of a JavaBean instance object is null, the result of using the <jsp: getProperty> label to output this property is a string with the content "null. 4. example: setproperty. jsp copy Code <% @ page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %> <% @ page import = "java. util. * "%> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN "" http://www.w3.org/TR/html4/loose.dtd "> <Html>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.