Use javaBean to encapsulate data

Source: Internet
Author: User

Use 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 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:

  

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:

  

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();%>

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

Syntax:

   

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: The Order class created by the tag uses setProperty to set its name to Michael Jacob and its age to 18 years old.

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" %>          My JSP 'index. jsp 'starting page         
   
   
   <% = Order. name %>
<% = Order. age %>

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"%>          My JSP 'MyJsp.jsp' starting page          

View the processing page

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>       My JSP 'userlogin.jsp' starting page          
       
       
       <%=login.getName() %>    
<%=login.getPassword() %>

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:

    

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"%>       My JSP 'userlogin.jsp' starting page          
        
        
        <%=login.getName() %>    

Achieve reuse of pages Label.

For example:

     

You can also use:

<%@include file="url"%>

Differences between the two:

To dynamically include 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.

Achieve the same effect as the jump. Page is the page to jump.

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.