Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

JSF Versus JSP, Which One Fits Your CRUD Application Needs? (Part 2)

DZone's Guide to

JSF Versus JSP, Which One Fits Your CRUD Application Needs? (Part 2)

Free Resource

Effortlessly power IoT, predictive analytics, and machine learning applications with an elastic, resilient data infrastructure. Learn how with Mesosphere DC/OS.

In every project plan there is a stage when functional tasks are extrapolated from the customer requirements. These tasks will produce the  structure of  the implementation plan for the project. In practice, the more capable you are of identifying tasks and the work they entail, the better you'll be able to follow the project schedule.

CRUD (Create Read Update Delete) applications are high demanded by clients and usually require addressing  functionalities like:

    • Easy handling of insert/update/delete of records (as their naming states)
    •  Privilege control system,  a  role based one.
    • Input validation
    • Means to support handling the standard set of entity relantionships: one-to-many, many-to-many.
    •  Internationalization
    • Provide a rapid response after a request was sent etc.

Stage 1 - Setup the development environment

This phase is common to both projects and only the dependencies included in the MAVEN pom files differ.

  • Register the development server in the IDE
  • Create a Maven web project in the IDE
  • Run the web project from the IDE (test compilation, deployment, run capabilities, get the necessary dependencies and ensure interoperability between IDE, server and browser)
  • Register the database server in the IDE (I attached an sql file as a data model to this article). I used Apache Derby database.
  • Establish a connection to the database server from the IDE.
  • Create a database instance on the database server.

Stage 2 - Prepare data model

In this stage the following activities are included :

  • Create an entity-relationship diagram (use a visual database design tool)
    • Identify objects
    • Create a schema
    • Create entities
    • Add entity properties
    •  Identify relationships between entities
  • Forward-engineer the entity-relationship diagram into an SQL script
  • Run the script on the database server to generate the schema

For the two Proof of Concept projects there is already an sql script attached to this article. You can run that script in order to test the code attached.

Stage 3 - Connect the application to the database

  • Add sample data to the database (inserts from the attached sql)
  • Create data source and connection pool  on server
  • Test data source (ping connection pool)
  • Ensure that views can access data from the database (if any views are defined)
  • Set any necessary application-wide parameters

Stage 4 - Develop the business logic

CRUDwithJSF2.2 project

CRUDwithJSP project

Create entities from database tables.

  • com.tutorial.crudwithjsf2.entities.Department
  • com.tutorial.crudwithjsf2.entities.Employee

Attention:

1. do not forget to add your entities to persistence.xml

2. even if your IDE automatically generates mapping pay attention to the OneToMany or ManyToOne relationship mapping

Create data objects based on  database tables.

  • com.tutorial.crudwithjsp.model.Department
  • com.tutorial.crudwithjsp.model.Employee

Create AbstractFacade and its children DeparmentFacade and EmployeeFacade. These 3 classes will ease the invocation of changes on database.

Create EmployeeDAO and DepartmentDAO that manipulate the database transactions for each of the data objects. Also, realise the database connection with an util class DatabaseUtil since that connection is shared for every type of object.


Stage 5 - Create controllers

CRUDwithJSF2.2 project

CRUDwithJSP project

  • Each of the JSF controllers have an injection of the corresponding Facade classes.
  •  Controllers are further used in the UI to accomodate the model.

  • Create mappings for views in deployment descriptor
  • Create skeleton code in servlet to handle client requests

  • com.tutorial.crudwithjsf2.control.DepartmentController is a ManagedBean with SessionScope
  • com.tutorial.crudwithjsf2.control.EmployeeController is a ManagedBean with SessionScope

  • com.tutorial.crudwithjsp.controller.DepartmentController extends a HttpServlet
  • com.tutorial.crudwithjsp.controller.EmployeeController extends a HttpServlet


Stage 6 -  Organize the application front-end

CRUDwithJSF2.2 project

CRUDwithJSP project

The JSFs can be generated, using templates. I used Create template .

Place JSP pages in the application's WEB-INF directory

Define page header and footer inside template.xhtml

Create page header and footer

When you using templates there is little chance of code duplication.

Remove instances of code duplication (header and footer code from JSP pages)

By using templates it is much easier to customize the style of the application and offer an uniform look to it.

Unfortunatelly, for pure JSPs the UI customization is available by adding css files as for any other client web page.


Stage 7 - Achieve Internationalization: add language support

CRUDwithJSF2.2 project

CRUDwithJSP project

In face-config.xml define your properties file that contains the labels. Place the Bundle under resources area of the web application.

<application>

  <resource-bundle>

  <base-name>/Bundle</base-name>

  <var>bundle</var>

  </resource-bundle>

  </application>

Place the localization file in web.xml and define it under WEB-INF folder in order to access it easily.

<context-param>

  <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>

  <param-value>lang</param-value>

  </context-param>

Learn

Learn to design and build better data-rich applications with this free eBook from O’Reilly. Brought to you by Mesosphere DC/OS.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}