The first step of JavaEE[7]

来源:互联网 发布:js 下拉框选中 编辑:程序博客网 时间:2024/06/07 08:46
                                     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 an
    object that is used to carry data from one object or application layer to
    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 the
    system.
What You Should Be Able to Do
    At the end of the lab, you should be able to:
         • 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 a
    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 for
    properties such as the item’s id, author, title, and due date. You will build a
    JavaBean that will serve to hold lists of these LoanedCopy objects.
         In later labs, you will write application business logic to populate the
                                                          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 should develop it to
      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 an
      object of type java.util.ArrayList.
           An optional JavaBean value object that you develop will be used as a
      container for text messages that are sent from one part of the system to
      another. Typically, such a bean could 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 the
      previous lab. The EAR file is located on your system at
      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 the
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 should 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 an
       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. The
      class will be part of the com.ibm.ils.library.servlets package. Since this
      class will be a JavaBean, it should 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 the
      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-
 
原创粉丝点击