Статьи

Создание приложений JavaServer Faces в Eclipse

In this article we will investigate  how to create a simple application using JavaServer Faces (JSF) framework in the  Eclipse IDE. First let us see what are the tools required to create our Hello World JSF application.

  1. JDK 1.5 above (download)
  2. Tomcat 5.x above or any other container (Glassfish, JBoss, Websphere, Weblogic etc) (download)
  3. Eclipse 3.2.x above (download)
  4. Sun Reference Implementation of JSF: (download). Following are the list of JAR files required for this application.
    • jsf-impl.jar
    • jsf-api.jar
    • jstl.jar
    • common-logging.jar
    • common-beanutils.jar
    • common-collections.jar
    • common-chain.jar
    • common-digester.jar

We will implement a JSF application with an Add User screen containing two fields, ID and User Name. Once the  user has entered these values and pressed submit, she will be redirected to a welcome page displaying the user name.

Let us start with our first JSF based web application.

Step 1: Create Dynamic Web project

Open Eclipse and goto File -> New -> Project and select Dynamic Web Project in the New Project wizard screen.
динамическое затмение веб-проекта

Select Dynamic Web application and click Next.
создать динамический веб-проект JSF

Write the name of the project HelloWorldJSF. Once this is done, select the target runtime environment (e.g. Apache Tomcat v6.0). This is to run the project inside Eclipse environment. In configuration select JavaServer Faces v1.2 Project and press Next.



JSF-новый проект

On Project Facets window, select Java 5 and JSF 1.2 and press Next.

Skip Web module window and press Next.

JSF-возможности-лицо-сервлет

Select JSF component library. Click New in Component Libraries and add jstl.jar, jsf-api.jar and jsf-impl.jar. In URL Mapping Patterns add /faces/* and then click Finish.

Once the project is created, you can see its structure in Project Explorer.
JSF-проект-исследователь-просмотр

Step 2: Create Package and Managed bean

Create a package net.viralpatel.jsf.helloworld in the source folder and create a Java file UserBean.java. Copy following content into UserBean.java.

Above is the Java class for a User bean that we will use to store our user’s information. This class acts like a form bean and action class. The addUser() method will get called when we click Add button on our Add User page.

Step 3: Create JSP files

Create two JSP files: AddUser.jsp and ListUser.jsp in WebContent folder. Copy following content in each of these files.

AddUser.jsp


We have added a validation rule for ID using f:validateLongRange tag and required=”true” attribute. The ID must be between 1 and 500.

ListUser.jsp

Step 4: Modify faces-config.xml file

Откройте файл face-config.xml из папки WebContent -> WEB-INF и скопируйте в него следующий контент.

В конфигурации Faces мы определили управляемый компонент UserBean с сеансом области действия и отображением из AddUser.jsp в ListUser.jsp.

Шаг 5: Выполнить и запустить проект

Последний шаг — выполнить проект и просмотреть его в браузере.
Для этого щелкните правой кнопкой мыши Имя проекта в Project Explorer -> Запуск от имени -> Запуск на сервере (сочетание клавиш Alt + Shift + X, R).



добавление нового-пользовательский JSF-проект-затмение

Как только вы введете ID и имя пользователя и нажмете «Добавить пользователя», появится следующий экран успеха.

добавить пользователей неуспеха-JSF-проект-затмение

Скачать полный файл WAR с источником

Скачать WAR с исходным кодом (1,48 МБ)

С http://viralpatel.net/blogs/