Enterprise web programming

Modules ENTWA (Level 6) and APSW (Level 7)

Step by step JPA/JSF webapp 1

Set up database

  1. From the Services tab in NetBeans, right-click "Java DB" and create a new database. For convenience, use the project name for the database, username and password.
  2. You should be able to connect to the newly created database from the same window.
  3. Do this first for each new project because some of the later steps require the database connection to exist.

Set up package

  1. According to the structure we covered in the lectures, your application will be in 5 "buckets":
    1. The View will be represented by Facelets in the "Web Pages" section of the project.
    2. Create source packages for the controller, business logic, persistence layer and business objects (entity classes). Examples:
      1. jim.project.ctrl
      2. jim.project.bus
      3. jim.project.pers
      4. jim.project.ents

Create entity class(es)

  1. In your entity package, create a business object class. Use the New File | Persistence | Entity Class wizard in NetBeans. Normally use a surrogate key of type Long as the primary key.
  2. Note @Entity, @Id and @GeneratedValue annotations.
  3. Add properties to the entity and create setters/getters for each.

Create persistence class(es)

  1. Create a peristence unit for the project using the New File | Persistence | Persistence Unit wizard.
    1. For the Data Source, you should usually select "New data source"
      1. The JNDI name will normally be "jdbc/project" (but ensure it has a different JNDI name than any other database you have previously used).
      2. You should find the correct database connection appears on the menu (if you set up the database above).
    2. Normal table generation strategy is "Create" unless you need to throw away (drop) your tables every time the application restarts. If the database already exists, choose "None".
    3. This step will create:
      1. the configuration file "persistence.xml"
      2. the server resources file "glassfish-resources.xml" - it is this that contains the database connection details
  2. Use the New File | Persistence | Session beans for Entity Classes wizard to create the Facade classes.
    1. Specify your persistence layer package as the package into which the classes should be generated.
    2. Normally, you want the wizard to create a facade class for each entity class in the application.
    3. Normally, you don't need it to create interfaces.
    4. The first time you run the wizard, it will create a class AbstractFacade. You will need to fix the "bug" in this by:
      1. changing the return type of edit from void to T
      2. returning the value returned by the merge function
  3. The wizard should have created a declaration for an entity manager object (with the @PersistenceContext annotation). The application engine will inject a suitable one when an object of this class is used.

Create business class(es)

  1. In your business logic package, create a business logic class. Use the New File | Enterprise JavaBeans | Session Bean wizard.
  2. Normally, the session bean needs to be "Stateless" and needs no interface.
  3. Add business functions as required to the class. Each business function will normally:
    1. return an object (or a collection of objects) of a relevant business object class
    2. have passed into it relevant objects from the user interface (i.e. controller layer)
    3. throw a BusinessException if an error is detected
    4. (if necessary) prepare business objects for use (e.g. by fetching other ones from store)
    5. check that the function being performed is valid to carry out
    6. if so, carry out the operation, calling persistence layer functions as necessary (typically this is only necessary if new objects have been created and need to be "managed" by the persistence layer)
    7. return the relevant object(s)
  4. Add declarations for an object of each persistance layer class you will need to use. Annotate these with @EJB. These will injected by the application engine when an object of this class is used.

Create controller(s)

  1. In your controller package, create a controller class. Use the New File | JavaServer Faces | JSF Managed Bean wizard.
  2. Normally, the scope of the bean will need to be "request".
  3. Add properties to this class reflecting the information you want to share (input or output) with the View layer. Create setters and getters for each.
  4. Add event methods to this class. Normally there will be one for each commandButton (or commandLink) in the View. Each method must:
    1. be "public"
    2. return a String
    3. if the returned String is "" then the previous View will be redisplayed; if you want a different View, return its name
    4. the body of the method will need to call at least one business logic method
    5. note that the method may need to ask the business layer to fetch/update the values of any controller variables that may be used to produce the next view
  5. Add declarations for an object of each business logic class you will need to use. Annotate these with @EJB. These will injected by the application engine when an object of this class is used.

Create view(s)

  1. In your "Web pages" section of the project, create a facelet as a view. Use the New File | JavaServer Faces | JSF page wizard.
  2. In your facelet, you will need (among whatever layout you choose):
    1. at least one <h:form> tag encompassing everything will need to be passed to a controller
    2. interface controls (e.g. <h:inputText>) for each data item you wish to communicate to the controller
    3. some sort of submit button/link, naming as its action the controller event method you wish to invoke - remember to use <h:commandButton> NOT <h:button>)

Part 2 of the step-by-step

 

Last updated by Prof Jim Briggs of the School of Computing at the University of Portsmouth

 
The enterprise web programming modules include some material that was formerly part of the WEB1P and WEB2P units.