Wednesday 21 March 2012

BEGINNING WITH JAVA SERVER FACES ( JSF1.2)

BEGINNING WITH JAVA SERVER FACES ( JSF1.2)
=========================================

 Today, I began experimenting with JSF.
 I  found a lot of similarities between Struts2   and JSF.


However, Struts2  is page-centric  but JSF is component-centric.


Any JSF  page needs a corresponding formbean.
( Struts1.1 also has this pattern).
( In Struts2  , the formbean code is within the action class).


Struts1.1  formbean is mostly for setter/getter.
In addition, we provide reset method and validate method.


But , in JSF, we can have our own business logic methods or use helper classes.
( This is the case in Struts2 also).


In JSF we can provide the 'result' also as an attribute and set its value within the formbean class
( backingbean /managed bean is the same as formbean)( just a case of terminology)


   Struts2 has a submit button.
JSF can have event-generating command buttons.


Though these are not submit buttons, when we click any button, the values in the page are loaded 
into the formbean instance.( equivalent to submitting).
===============================================
The directoty structure also is very much similar.
Say, if our application context is 'modelapp',
we have WEB-INF below this.


Inside WEB-INF, we have classes and lib folders.


Inside the classes folder, I created mypack folder.
All my formbeans and helper beans are placed in mypack.


The jsp, htm and img files are placed in modelapp itself.
===============================================
We need jsf-api.jar file in classpath to compile the formbean.




As usual we need  web.xml  and faces.xml


I had some difficulty in getting the correct web.xml file initially..
But, after getting it, this will be same for all the demos.
faces.xml will depend on the specific needs of the demo.


I am giving the web.xml file now.

web.xml
------------
<?xml version='1.0' encoding='UTF-8'?>


<!DOCTYPE web-app PUBLIC
  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  "http://java.sun.com/dtd/web-app_2_3.dtd">


<web-app>


    <context-param>
     <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
      <param-value>client</param-value>
    </context-param>


    <context-param>
    <param-name>com.sun.faces.validateXml
    </param-name>
    <param-value>true</param-value>


    </context-param>


    <context-param>
        <param-name>com.sun.faces.verifyObjects</param-name>
        <param-value>true</param-value>


    </context-param>


    <!-- Faces Servlet -->
    <servlet>
  <servlet-name>Faces Servlet </servlet-name>
   <servlet-class>javax.faces.webapp.FacesServlet
   </servlet-class>


   <load-on-startup> 1 </load-on-startup>
    </servlet>




    <!-- Faces Servlet Mapping -->


    <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
     <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>




</web-app>
===============================================







Now, I will give two demos.
a)  query
b) dbedit.


We have query.jsp, query.java and querybean.java
( that is all!) No action class or action-controller!


web.xml, faces.xml are the other needed files.
=======================================
Let me now explain how I did this.




I created a folder "jsfapps" in g:\drive


First I am going to give demo for querying.


I have given the essay in my website
==============================================