Wrtiting a JavaServer Faces (JSF) Application with Eclipse & GlassFish Server


This blog shows you how to write a simple JavaServer Faces Application with Eclipse and GlassFish server. For a complete JSF application with JPA application, please visit the links at the end of this blog


1. Requirements:
-CoBundled (Eclipse 3.4.1 & GlassFish Servers) or
    -Install Eclipse 3.4.1 and GlassFish plugin


2. Configuration
-Set Web editor for JSP page default editor
         From Window menu, select Preferences > General > Editors > File Associations
         In the File types section, select \*.jsp
         In the Associated editors section, select Web Page Editor > Select the Default button


File References


-Register JSTL library
Select Windows->Preferences->Web->JavaServer FacesTools-> Libraries.
Click on the New.. button to add a new library
Create the JSF Libraries (name JSTL)
       (Note: you should be able to find jstl-impl.jar files under ..\\glassfishv3-prelude\\glassfish\\modues\\web directory)


JSTL


3. Create the JSF Project
      From File > New > select to create a New Dynamic Web Application names it as MyJSF.
      Set target runtime to GlassFish V3 Prelude  (you can also select GlassFish V2.1)
      Set Configuration to JavaServer Faces v1.2 Project > click Next button


Web Dynamic JSF


     On JSFCapabilities panel, select radio button Server Supplied JSF Implementation. Add JSTL library


Dynamic Web Project


-Add a JSP page to JSF project
           Right-click at myJSF project, select New > JSP to create a page called hello.jsp. In the Select Templates page, select the New Java Server Face (JSF) Page (html) template. Click Finish. The page will be
opened in the Web Page Editor as shown below


JSP Template



-To turn ON the palette to design the Canvas components
        From the Window menu, select Show View > Others > expand General > then select Palette > click Ok. The palette can appears at the IDE button but you can drag it to the top right as shown in the below figure


Palette


-Add a command button to the canvas
           Open the Properties View. Right-mouse click on the designer canvas and from thecontext menu, select Show->Properties
           In the Palette View, click on the section JSF HTML to display
the list of components
           Drag-and-drop the CommandButton to the canvas "drag and drop section"
           In the properties view, click on Quick Edit
           Set the Value attribute to ClickMe and the Action attribute to "#{myBean.action}"
          (Note: later you will write MyBean.java to process the actions your declare in hello.jsp file)



Click Me button


-Add a PanelGrid
              Drop the Panel Grid before the Command Button, but inside the Form tag, panel Grid is created with a predefined set of OutputText components
             At the end it should looks like the figure below (you can cut and paste the statement below to your hello.jsp file)


<h:form>
    <h:outputLabel for="input" value="Enter your name:"/>
    <h:inputText id="input" value="#{myBean.input}" required="true"/>
    <h:commandButton value="ClickMe" action="#{myBean.action}"/>
</h:form>

<h:outputText value="#{myBean.output}"/>


Hello.jsp code



-Create MyBean bean
In the Project Explorer, expand the node, MyJSF->WebContent. Double-click
on
faces-config.xml . This will launch the FacesConfiguration editor.


Face editor


      -Select the ManagedBean tab.
New Java Bean
         Click on the Add button. This will launch the New Managed Bean wizard.
Select the option, Create a new Java class. In the next wizard panel,
enter the package as, j2eetest and the Name ofthe class as MyBean . Click the Finish button.


My Bean









manage Bean


      -Edit the Java class, j2eetest.MyBean. Add thefollowing code and save


private String input;
private
String output;


Right-click at the MyBean.java > select Sources menu >Generate Setter&Getter.
Setter & Getter
At the end you should have the MyBean.java code look like this 
MyBean Code

Now you're ready to run your JSF Hello.jsp file.
Right-click at hello.jsp file > select Run As > Run on Server

RunTime 

-Choose GlassFish V3 Prelude

Server Runtime
Enter your name at the text field > click ClickMe button.

result
-For building a JSF application with JPA, please visit the links below:
-Using Oracle link
-Using DAO



Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

davisn

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today