You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 43 Next »

This application will make you understand how Model(M), View(V), Controller(C) architecture is implemented in Java Server Faces. This application will make use of UI components, Validator, Navigation and Bean component available with JSF. This application has the following pre-requisites:

  • WTP 2.0.1 - Download for wtp-all-in-one-sdk-win32

Briefly describing the application, this application will take a user First Name and Last Name. Later these fields will be validated by JSF and using the controller bean and Navigation rule the output will be displayed. This application will also introduce a UI component which is a submit button.

The application development will take you through the following

  1. Setting Eclipse for Application development 
  2. Define and implement the application Model(M)
  3. Define and implement Model(M) objects to Controller
  4. Define and implement View(V) in application
  5. Define and implement the Validator component
  6. Define and implement the View navigation by Controller(C)

Once you have all the pre-requisites installed follow the following steps to create a project with Eclipse

  1. Setting Eclipse for Application development
  • Launch Eclipse and Create a dynamic web project as shown in the figure

Unable to render embedded object: File (CreateWebProject.gif) not found.

  • Give the fields for the Web Project as shown in the following figure
    Unable to render embedded object: File (WebProjectFields.GIF) not found.
  • Check the box for JavaServerFaces and under the version tab select 1.2 as the version
    Unable to render embedded object: File (ProjectFileds.GIF) not found.
  • Once done you can give default values for web module and Geronimo Deployment Plan. On the JSF capabilities window check the box and select new as shown in the figure
    Unable to render embedded object: File (JSFCapabilities.GIF) not found.
  • The next window suggests to create JSF Implementation library. Give the library name as JSFCustomLibrary and add the following jars. Select Finish once done. See the figure below
  • <GERONIMO_HOME>\repository\commons-beanutils\commons-beanutils\1.6.1\commons-beanutils-1.6.1.jar
  • <GERONIMO_HOME>\repository\commons-collections\commons-collections\3.1\commons-collections-3.1.jar
  • <GERONIMO_HOME>\repository\commons-digester\commons-digester\1.8\commons-digester-1.8.jar
  • <GERONIMO_HOME>\repository\commons-logging\commons-logging\1.0.4\commons-logging-1.0.4.jar
  • <GERONIMO_HOME>\repository\org\apache\myfaces\core\myfaces-api\1.2.2\myfaces-api-1.2.2.jar
  • <GERONIMO_HOME>\repository\org\apache\myfaces\core\myfaces-impl\1.2.2\myfaces-impl-1.2.2.jar!JSFCustomLibrary.GIF!
    Unable to render embedded object: File (JSFCustomLibrary.GIF) not found.
  • Check Deploy and modify the URL pattern to *.jsf as shown in the figure. Select Finish.

       Unable to render embedded object: File (EclipseSetUpFinal.GIF) not found.
This finishes the setting up of Eclipse IDE for application development

    2. Define and Implement the application Model(M)

Model as suggested by MVC architecture handles data and logic of the application. In an enterprise application, Java Beans are used to represent collection of data and operation on that data. In JSF we use Java Beans to define the Model.

  • Under the project explorer right click on the SimpleJSF project and create a new class

Unable to render embedded object: File (Model1.GIF) not found.

  • Fill the New Java Class form with jsf as the package name and FirstName as the bean class name. Select Finish once done.

Unable to render embedded object: File (Model2.GIF) not found.

  • Add the following code to the bean class.

String username;
public String getName()

Unknown macro: { return username ; }

public void setName(String name)

Unknown macro: { username=name; }

* Create a second Bean class LastName and add the following code to the class String lastname; public String getLName()

Unknown macro: { return lastname; }

   
    public void setLName(String lname)
   

Unknown macro: { lastname=lname; }

This completes the Model definition and implementation of bean class. 

    3. Define and implement Model(M) objects to Controller

  • In an JSF application controller is implemented by a configuration file WebContent/WEB-INF/faces-config.xml. Double Click on the file. This will open a Faces Configuration Editor.

Unable to render embedded object: File (ModelController.GIF) not found.

  • Select the ManagedBean tab in the editor. Choose Managed Bean of scope request and select Add. 

Unable to render embedded object: File (ModelController2.GIF) not found.

  • Using the existing java class option, select Browse. Give the search element as FirstName and select ok.

Unable to render embedded object: File (ModelController3.GIF) not found.

  •  Select Finish on the next window. Similarly add the other bean LastName. Now select the Source tab in the Faces configuration Editor. It displays the bean components(Model) in the controller.
    Unable to render embedded object: File (ModelController4.GIF) not found.

This completes the description of Model to Controller.
    4. Define and implement View(V) in application

  • Right Click on WebContent and create a New Folder with the name pages
    Unable to render embedded object: File (View.GIF) not found.
  • Right Click on pages folder and create a jsp page login.jsp. Select Finish.
  • No labels