Introduction and use of JavaBeans

Source: Internet
Author: User

Brief introduction

JavaBeans is a Java class that conforms to a particular specification. The advantage of using JavaBeans is to solve the code duplication, the function is clear, improve the maintainability of the code.

is an ordinary Java class, but conforms to a specific specification.

Follow a design principle

Is the common sense of the physical class

Example:

JSP actions

The JSP action element provides information for the request processing phase. The action element follows the syntax of the XML element, with a start tag that contains the element name, an end tag that can have attributes, optional content, and matches the start tag.

Contains the following categories, the first class of three tags, focus!

    • <jsp:userBean>
    • <jsp:setProperty>
    • <jsp:getProperty>

JavaBeans are usually used in JSP pages.

Two ways of using JavaBeans

The first way

After creating the JavaBeans class in the SRC directory, and then in the JSP page,

Import class

<%@ page import= "The package name where the user class is located"%>

After importing the package, add the following to the HTML tag:

There is no difference between creating a Java class with normal.

The second method of creating JavaBeans

JSP action tags are commonly used in JSP pages to use JavaBean

    • Usebeans Action
    • SetProperty action
    • GetProperty Action

<jsp:useBeans>

Function: Instantiate in a JSP page or use JavaBean within a specified range

<jsp:usebean id= "" class= "Java class name" scope= "Scope"/>

Instead of importing the package and then creating the corresponding instance object, you can use the

After running,

Because we are not initialized, all are null

So how do you initialize it, using setProperty

<jsp:setProperty>

Function: Assign a value to the attribute of an JavaBean object that has already been instantiated, in a total of four different forms.

The first way to match all of the properties based on the form

The name here is the ID of the JavaBeans, and the * number is based on the form to match.

In other words, when the user fills out the form, for example, the username column is Wu, then it automatically corresponds to the username attribute in the JavaBeans class.

The second method is used when the attribute part matches

This time only matches the username, and there is no match password

The third way to manually assign values is not related to what the form fills out.

The fourth kind does not introduce, the feeling does not use.

GetProperty

Function: Gets the property value of the specified JavaBean object.

With GetProperty, you don't need to use this method to get property values.

To get Properties using GetProperty:

JavaBeans Four Scope scopes

......

Without JavaBeans, the JSP page interacts directly with the data layer, which makes the maintainability of the code very poor, and the presence of a large number of business logic code in the JSP is not good.

Introduction and use of JavaBeans

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.