The first step of javaee [7]

Source: Internet
Author: User
J2EE Technology
Lab 7. Create a JavaBean
What this exercise is about
In this exercise, you will continue to build the ILS library system. Our
System needs a way to package data it will obtain from the application's
Datastore (such as a list of checked out items for a patron) so that data can
Be moved to the view layer to be displayed by a JSP as a table on an page.
The exercise will address this need by building a value object, which is
Object that is used to carry data from one object or application layer
Another.
You will implement this value object as a JavaBean and develop setter
Methods that can be used to populate the bean's properties and getter
Methods that can be used to retrieve those properties. These getter and
Setter methods provide a standardized interface to access the bean's
Properties.
Optionally, you will build a JavaBean that will be used to move data in
The form of various error and status messages between components of
System.
What you should be able to do
At the end of the lab, you should be able:
• Build a JavaBean value object to hold a list of objects.
• Optionally, build a JavaBean value object to hold a scalar object.
Introduction
The JavaBean value object you will be building will be used as
Container for the list of checked out items for a patron. A single checked out
Item is represented in the model layer of the application by the loanedcopy
Class. The loanedcopy class has various getter and setter Method
Properties such as the item's ID, author, title, and due date. You will build
JavaBean that will serve to hold lists of these loanedcopy objects.
In later labs, you will write application business logic to populate
Lab 7. Create a JavaBean-1-
J2EE Technology
Bean's list of checked out items and then pass this JavaBean onto the view
Layer. a jsp in the view layer will extract the list of items for display in a table
On a HTML page.
To ensure maximum flexibility of the JavaBean, you shoshould develop it
Set and get both the entire list as an entity and any element (I. e. a single
Loaned copy) in the list. For this exercise, you will implement the list as
Object of Type java. util. arraylist.
An optional JavaBean value object that you develop will be used as
Container for text messages that are sent from one part of the system
Another. Typically, such a bean cocould be used to store error messages that
Were set by a servlet processing an input form. The input processing Servlet
Stores the bean in a request attribute and forwards it to the JSP that manages
The input form. The JSP obtains the bean from the request attribute and uses
The bean's getter methods to access the message and display it on the form
To inform the user of any processing errors.
V1.0
Exercise instructions
This exercise builds upon work done in previous labs. If you did not
Complete the previous lab, you can import the Library Enterprise Application
Archive file that contains the cumulative solution code through the end of
Previous lab. The ear file is located on your system
C:/frontendexercises/solutions/librarysession6.ear.
Step 1 create the loanedcopylistbean JavaBean
In this step you will create a new JavaBean called loanedcopylistbean.
This bean will hold the list of checked out items for a patron. Each item in the list
Will be modeled by a loanedcopy object, which is available in
Libraryjavaproject for your use.
_ 1. Create a Java class named loanedcopylistbean in librarywebproject.
The class will be part of the COM. IBM. Ils. Library. servlets package. Since this
Class will be a JavaBean, it shocould implement the java. Io. serializable
Lab 7. Create a JavaBean-2-
J2EE Technology
Interface.
_ A. From the Web perspective, locate the/librarywebproject/Java
Source folder, then select the com. IBM. Ils. Library. servlets package then
Right-click --> New --> class and click Next.
_ B. Make sure the folder text field contains the text
/Librarywebproject/Java source and that the Java package is
Com. IBM. Ils. Library. servlets.
_ C. Enter the class name of loanedcopylistbean.
_ D. Uncheck the main () method and check constructors from
Superclass. Press the Add button to implement the java. Io. serializable
Interface and click Finish.
_ 2. Code Two getter methods named getloanedcopylist () and two setter
Methods named setloanedcopylist () to access the arraylist property named
Loanedcopylist, as well as individual elements in that list. In accordance with he
JavaBeans coding conventions, the signatures of the methods are as follows:
-The getter method to process the list has no parameters and returns
Arraylist.
-The getter method to process an element of the list that has an int
Parameter of an index of the list and returns a loanedcopy object.
-The setter method to process the list has a parameter of an arraylist.
-The setter method to process an element of the List has an int Parameter
Of an index of the list and a loanedcopy object to be set at that index.
_ A. Here is our solution:
Public class loanedcopylistbean implements serializable {
Public loanedcopylistbean (){
Super ();
}
Private arraylist loanedcopylist;
Public void setloanedcopylist (arraylist aloanedcopylist ){
Loanedcopylist = aloanedcopylist;
}
Lab 7. Create a JavaBean-3-
J2EE Technology
Public void setloanedcopylist (loanedcopy aloanedcopy, int index ){
Loanedcopylist. Set (index, aloanedcopy );
}
Public arraylist getloanedcopylist (){
Return loanedcopylist;
}
Public loanedcopy getloanedcopylist (INT index ){
Return (loanedcopy) loanedcopylist. Get (INDEX );
}
}
Step 2 create the messagebean JavaBean (optional)
In this step you will create a new JavaBean called messagebean. This bean
Will hold a text message.
_ 1. Create a Java class named messagebean in librarywebproject.
Class will be part of the COM. IBM. Ils. Library. servlets package. Since this
Class will be a JavaBean, it shocould implement the java. Io. serializable
Interface.
_ A. From the Web perspective, locate the/librarywebproject/JAVA Source
Folder, then select the com. IBM. Ils. Library. servlets package then
Right-click --> New --> class and click Next.
_ B. Make sure the folder text field contains the text
/Librarywebproject/Java source and that the Java package is
Com. IBM. Ils. Library. servlets.
_ C. Enter the class name of messagebean.
_ D. Uncheck the main () method, check constructors from superclass.
Press the Add button to implement the java. Io. serializable interface and
Click Finish.
_ 2. Code a getter method named gettext () and a setter methods named
Settext () to access the string property named text. In accordance with
JavaBean coding conventions, the signatures of the methods are as follows:
Lab 7. Create a JavaBean-4-
J2EE Technology
-The getter method has no parameters and returns a String object.
-The setter method has a parameter of a string object.
_ A. One way to do this with the Wizards is as follow:
-At the top of the class declare and initialize the text variable:
Private string text = "";
-In the outline pane, locate the new text variable then
Right-click --> Generate getter and setter..., keep all methods selected and
Click OK.
_ B. Here is our solution:
V1. Public class messagebean implements serializable {
Private string text = "";
Public messagebean (){
Super ();
}
Public void settext (string atext ){
TEXT = atext;
}
Public String gettext (){
Return text;
}
}
End of lab
Exercise review/wrap-up
In this lab:
You created JavaBean value objects for both single-valued and list properties.
These objects provide a standardized way of holding data to be moved from
One object or layer to another and provide a standardized interface to extract
Or set the data they contain. In later labs, you will use these components in
The business logic and view layers and see how the properties contained in
The beans can be extracted in JSPs without writing Java code.
Lab 7. Create a JavaBean-5-
 

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.