JSP Learning Notes (4)-javabean

Source: Internet
Author: User
Tags naming convention

According to Sun's definition, JavaBean is a reusable software component, and in fact JavaBean is a Java class that, by encapsulating properties and methods, becomes an object that has some functionality or processes a business, for short Bean,javabean based on the Java language, Nature does not depend on the platform, it has the characteristics of:

  1. Can be used to implement code reuse
  2. Easy to write, easy to maintain, easy to use
  3. Can be used on any platform where the Java Runtime environment is installed and does not require recompilation

JSP pages can assign data processing to one or several beans, that is, the JSP page invokes the bean to complete the processing of the data, and stores the results of the processing in the bean, and the JSP page is responsible for displaying the data in the bean. The main task of a JSP page is to display the data instead of the data.

1. Writing JavaBean

JavaBean is divided into visual components and non-visual components, which are mostly non-visual components in JSPs.

JavaBean is actually a class, and there are some rules for writing this class:

  1. If the member variable of the class is XXX, in order to get or change the value of the member variable, that is, to get or change the property, you must provide two methods: GetXxx () and setxxx ().
  2. For member variables of type Boolean, it is allowed to replace get and set
  3. The Access property of a method declared in a class must be public
  4. The constructor method in the class must be public, without a parameter

Non-visual components include the tool JavaBean and the value JavaBean.

  1. The value JavaBean strictly follows the JavaBean naming convention, which is typically used to encapsulate form data as a container for information.
  2. Tool JavaBean can not follow the JavaBean specification, usually used to encapsulate business logic, data manipulation, such as connection to the database, the database to increase, delete, change, check and solve the Chinese garbled and other operations. Tool JavaBean can realize the separation of business logic and page display, and improve the readability and maintainability of code.

Bean byte-code file directory

We all know that a Java program to run, it must be compiled into a bytecode file, and then run by the JVM interpretation, then how can tomcat find this bytecode file?

Tomcat provides a specific directory of files that can be found and executed by the Tomcat server as long as the bytecode file is saved to the directory.

Current project directory: \web-inf\classes, according to the class write the package name, under classes to establish the corresponding directory, the bytecode file in the corresponding directory.

As for the source directory, can be set or not, the general will set up related directories, easy to change the project, the source code storage directory is not what requirements. The source files in eclipse are typically under "project/src".

2. Create and use JavaBean
2.1. Use the Bean

JSP page to load the usage bean using the JSP's action tag Usebean,

The syntax format of the Usebean tag:

 <jsp:usebean id= Bean's name (actually a Bean class object) "class  =" Creates a bean's bytecode (equivalent to importing a bytecode file of the class to be used) "scope=" Bean's valid range "/> or  <jsp:usebean id=" Bean's name (actually a bean class one object) "class  = "Create the byte code of the bean (equivalent to the bytecode file of the class to be used)" scope= "The valid range of the Bean" ></JSP:USEBEAN> 
Loading principle of 2.2.Bean

The JSP page uses the JSP action tag Usebean to load a bean,jsp engine first, based on the ID of the bean given by Usebean and scope, in a synchronization block to find whether the JSP engine built-in PageContent object contains such a bean, If present, the JSP engine is assigned to the user, and if it does not exist, create a JSP action tag usebean the required bean according to the bytecode specified by class and add the created bean to the PageContent built-in object.

Draw a picture and see

Effective range of 2.3.bean (life cycle)

The value of scope determines the life cycle of the bean, and the values are page, session, request, Applicarion. The following is a description of the different values

  1. Page: Only valid on the current page until the page finishes executing. When two users access a JSP page at the same time, a user's changes to their bean properties will not affect another user, the value is session, request is also.
  2. Session: Each page of the Web service directory that the user accesses is valid, knowing that the user's session is gone.
  3. Request: Only valid on the current page until the end of the response. The Bean's request survived longer than the page, because the JSP engine believed that the response was finished after the page was executed.
  4. The application:jsp engine assigns a shared bean to all JSP pages under the Web Service directory, and when multiple users access a JSP page at the same time, any user changes to their bean properties will affect other users.
3. Get and Modify bean properties
3.1. Get the Bean's attribute value using the GetProperty action tag

When you use GetProperty to get the property value of a bean, the value is sent to the user's browser as a string. The syntax is as follows:

<jsp:getproperty name= "Bean ID name (that is, object name)" Property= "Bean property"/> or <jsp:getproperty name= " The name of the Bean's ID (that is, the object name) "Property=" Bean Properties "></jsp:getProperty>
The directive is equivalent to <%=bean.getxxx ()%>
3.2. Set the Bean's property value using the SetProperty action tag (1) Set the value of the Bean property to the value or string of an expression
<!--set the value of the Bean property to an expression--><jsp:setproperty name= "Bean's ID Name" property= "Bean's Property" value= "<%= expression%>"/ ><!--Set the value of the Bean property to a string--><jsp:setproperty name= "Bean's ID Name" property= "Bean's property" value= string/>

If you set the value of an expression to the value of a bean property, the data type of the expression value must be the same as the data type of the Bean property, and if the string is set to a Bean property, the string automatically converts the type of the long Bean's property.

Java converts a string to another data type method:

  • Convert to Int:Integer.parseInt (String s);
  • Convert to Long:Long.parseLong (String s);
  • Convert to Float:Float.parseFloat (String s);
  • Convert to Double:Double.parseDouble (String s);

NumberFormatException exceptions can occur in these methods.

(2) Set the value of the corresponding property of the bean by the value of the parameter of the HTTP form
  • Sets the property value corresponding to the bean with the value of all parameters of the HTTP form
    <jsp:setproperty name= "Bean ID Name" property= "*"/>
This method does not specifically specify the value of the bean attribute for which parameter in the form is specified, but the system automatically matches, but requires that the name of the bean attribute must correspond to the name of the same parameter in the form, and the JSP engine automatically converts the string value of the parameter to the value of the property corresponding to the bean.
  • Set a Bean property value with the value of one of the parameters of an HTTP form
    <jsp:setproperty name= "Bean ID name property=" attribute name "param=" parameter name "/>

This method specifies the value of the bean attribute that corresponds to which parameter name in the form, and does not require that the name of the Bean property given by the property be the same as the parameter name given by the Param.

When you set the Bean property value with an HTTP form, the corresponding setproperty tag is executed only if the form is submitted.

JSP Learning Note (4)-javabean

Related Article

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.