Use javaBean to encapsulate data and javabean to encapsulate data

Source: Internet
Author: User

Use javaBean to encapsulate data and javabean to encapsulate data

JavaBean is a Java class and an important component that can be used for cross-platform development in java.

Function of javaBean: encapsulate business logic and data.

JSP action Tag: through the action tag, the programmer can encapsulate the page display function in the JSP page to make the whole page more concise and easy to maintain.

<Jsp: useBean>

Jsp: useBean action is used to load a javaBean that will be used on the JSP page. This function is very useful because it enables us to take advantage of the important advantages of java components while avoiding the convenience of JSP.

The syntax for jsp: useBean actions is as follows:

<jsp:useBean id="name" type="package.class" scope="scope"/>

The id is used to create the reference name of the javaBean. The class is used to specify the class of the javaBean. The scope is used to specify the scope of the javaBean. The default value is page.

The scope value can be the following range:

1. page and javaBean can only be used on the current page. When a new page is loaded, it will be destroyed.

2. request and javaBean are used when users send requests.

3. session and javaBean remain in the session.

4. application and javaBean can be used in the entire application.

For example:

<jsp:useBean id="order" type="com.Order" scope="request"/>

This Code indicates that an object specified by id exists in the range specified by the request, and this object is returned. If not, a Java class object specified by the class attribute is created, and saved in the variable specified by the id. In a JSP file, it is equivalent:

<%@page import="com.Order" %><%Order order = (Order)request.getAttribute("order");if(order == null)order = new Order();%>
<Jsp: setProperty>

The setProperty action is used to set the javaBean attribute value specified in useBean.

Syntax:

<jsp:setProperty property="PropertyName" name="BeanName" value="Value" param="Parameter"/>

Specifically, name specifies the id of the usean used in useBean; property is used to specify the name of the property to be set. Value specifies the value to be set for the attribute. Param specifies the value of the form Element Used to input data.

Note: The value and param attributes cannot be used at the same time.

Example 1: In the Order class created using the <jsp: useBean> label above, set its name to Michael and age to 18 years old using setProperty.

First, let's look at the Order class:

package com;public class Order {public String name;public int age;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}

Then look at the JSP page:

<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 

Example 2: application of the param attribute

First, let's look at the Login class:

package com;public class Login {private String name;private String password;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}}

Then, go to the logon page.

<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 

View the processing page

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><Jsp: getProperty>

After setProperty is assigned a value to the property specified by useBean, you can use the getProperty action to obtain the property value specified in javaBean.

Syntax:

<jsp:getProperty property="PropertyName" name="BeanName"/>

Specifically, name specifies the id of the usean used in useBean; property specifies the attribute name of the javaBean to be obtained.

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><Jsp: include>

Use the <jsp: include> label to reuse pages.

For example:

<jsp:include page="index.jsp"/>

You can also use:

<%@include file="url"%>

Differences between the two:

<Jsp: include> contains the results of the contained page. That is, it is processed first in the include.

% @ Include % is a static include. The contained content is included first and processed.

<Jsp: forward>

<Jsp: forward page = "url"> achieves the same effect as redirection. Page is the page to jump.

 


What is the role and advantage of javabean in Data encapsulation?

First of all, this is a programming idea, that is, the question of 'object-oriented. Javabean is a java object that encapsulates data. We only need to access this object when accessing data.

Second, if your form data is relatively small, if there are more, such as 10, then you can call the servlet method, such as calling the database insert operation, do you want to create a method with 10 parameters? Or is it easy to create a method with only one javabean as a parameter?

In your example, for example, when page A is submitted and redirected to page B, the form data of page A is encapsulated into A javabean, you only need to access this javabean on page B, then you can call the relevant get method. If javaean is not used, you will not bother with the request. setParameter () in the servlet ().

In the future, you will surely be exposed to some open-source frameworks of the database persistence layer. Currently, hibernate is the most used. Its addition, deletion, modification, and query of databases are all based on javabean, for example, you want to save a student ID, name, and gender. The original practice is
Insert into t_student (id, name, sex) value (1, 'jack', 1 ).
To use frameworks such as Hibernate, you only need to call one method.
Save (student ).

In this example, the sutdent is a javabean with three member variables: id, name, and sex. Is it convenient?

Far away.

How can I use a lot of beans to encapsulate many tables in a database with Javabean?

Yes ~ Alternatively, you can write a method to generate a class, for example
Package com. auto. fanwei. comm;

Import com. auto. fanwei. model. ColModel;
Import com. auto. fanwei. model. TableModel;

Public class ConstantModel
{
Private static StringBuffer strBuffer;
Private static ColModel col;

Public static String createModel (TableModel table)
{
StrBuffer = new StringBuffer ("");
StrBuffer. append ("package" + Constant. BAG_PATH_MODEL + ";"). append ("\ n ");
StrBuffer. append ("public class" + table. getNick () + "Model extends PageModel"). append ("\ n ");
StrBuffer. append ("{"). append ("\ n ");
For (int I = 0; I <table. getColList (). size (); I ++)
{
Col = (ColModel) table. getColList (). get (I );
If (col. getType () = 1)
{
StrBuffer. append ("private int" + col. getName () + "= 0;"). append ("\ n ");
}
Else if (col. getType () = 2)
{
StrBuffer. append ("private String" + col. getName () + "= \" \ ";"). append ("\ n ");
}
}
For (int I = 0; I <table. getColList (). size (); I ++)
{
Col = (ColModel) table. getColList (). get (I );
If (col. getType () = 1)
{
StrBuffer. append ("public int get" + col. getName (). substring (0, 1 ). toUpperCase () + col. getName (). substring (1, col. getName (). length () + "()"). append ("\ n ");
StrBuffer. append ("{"). append ("\ n ");
StrBuffer. append ("return" + col. getName () + ";"). append ("...... the remaining full text >>>

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.