DZone
Big Data Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Big Data Zone > JSF Versus JSP, Which One Fits Your CRUD Application Needs? (Part 2)

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

Ana-Maria Mihalceanu user avatar by
Ana-Maria Mihalceanu
·
Nov. 07, 14 · Big Data Zone · Interview
Like (0)
Save
Tweet
9.27K Views

Join the DZone community and get the full member experience.

Join For Free
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

  •  Head First Servlets and JSP, Second Edition by Oreilly
  •  Java Server Faces Technology
  •  Best Practices  from IBM

Database connection application

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Cross-Functional Team Management
  • Five Tips to Fasten Your Skewed Joins in Apache Spark
  • How to Build Microservices With Node.js
  • API Security Tools: What To Look For

Comments

Big Data Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo