MVC

An often posted question is which parts represent model, view and controller in JSF. After reading some different opinions to this topic, i show here the division and naming convention used on this site:

Convention

First the explanation of some important terms:

Java Bean
Reusable software component which can be configured without programming
Managed Bean
Registered Java Bean which will be automatically instantiated and initialized
Backing Bean
Any Bean which provides attributes and components for a web form

A bean can be managed bean and at the same time backing bean, if it is registered in Faces Config and additionally provides for example style class information.

Model

The model contains POJO classes, which only store the data used in the view. I do not use any suffix for the model classes, so its better readable in the pages.

Package:
de.icoding.model.forum
Classes:
Forum, ForumEntry

For management of master data i use normally two classes:

Forum:
Contains all data of an entity, used for creating/editing
ForumEntry:
Represents a single list entity with only the essential data

View

The view includes the JSF page inclusive of the components and the backing beans, which contain view depending code to control the components. I choose suffix View for this classes because its more readable than Backing inside the pages.

Package:
de.icoding.view.forum
Classes:
ForumView, ForumListView, ForumListViewEntry

For management of master data i use normally three classes:

ForumView:
Detail view to create or edit a single entity
ForumListView:
Listing the entities
ForumListViewEntry:
Single list entity

Controller

The controller part is done by several classes like Filter, JSF Servlet, ViewHandler and also the Business Logic.

Package:
de.icoding.controller
Classes:
ServiceFilter, ViewHandler

The business logic is separated but belong also to the controller, because the logic methods control the navigation with special IDs. For this classes i use the suffix Action.

Package:
de.icoding.action.forum
Classes:
ForumAction

Best Practice

View

Its the best you create for all JSF pages with dynamic content also an associated View Bean. Hence you have a uniform structure in the total application and you can add a backing method fast.

For a page with a form the View Bean will just be registered as Managed Bean and the processing Action methods work with this one class, which contains all Model references.

All View Bean are derived of class BasicView, which provides common methods used in all JSF pages.

JSF pages with static content, like an imprint with hard coded company data, no own view class will be programed. Instead only the Basic View will be used.

Controller

Normally it is not neccessary to program an special Controller class for the View. The JSF framework already take over validation and update of the Model data. Own Validator and Converter would be an extension of the framework in my eyes.

One exception could be, if the Model data should be changed specially due to an user action and this should not be coded in the Action class, because you want to separate it from business logic.

Johannes Hammoud MVC 08.11.2008

I Coding : Community about Java programing

Language German+-

Java JSF JavaScript HTML CSS NetBeans GlassFish MySQL

Imprint

Visits
6279755
Today
7077