Monday 26 March 2012

SIMPLE DEMOS IN JSF 2 USING ANNOTATION

Today, I did 3 nice demos in JSF 2. I have done
similar work in SRTUTS1.1, STRUTS2 and JSF 1.1.
I think we get a clear picture when we do the same experiments in all the technologies.
I find that JSF-2 is the best and easiest to understand and execute.


 First of all,


     I googled 
"JSF2 tutorial by marty hall".
 I found a link
   "www.coreservlets.com\JSF-tutorials\JSF2".
There I found a tab "Installation, Setup and getting started" as third item.


 In that block, You found a link 
      "JSF-blank.zip"
Then I downloaded it to my desktop and then I unzipped. got a folder "JSF-blank".

  I opened the folder and found "webcontent" folder. 
  This webcontent folder has the required directory structure.

 In lib folder you will be having following jars
jsf-api
jstl-api-1.2
jsf-impl
jstl-impl-1.2
Now Iam ready to develop a JSF2 application


   I copied it to desktop and renamed it "jsf2echoapp" and copied to tomcat7.


  My first experiment is the usual echo program. All JSF  programs have just 2 main files. One file is meant for GUI(input as well as output). The other file is java source file,
which combines the function of ActionForm and Action class in STRUTS1.1.


  The JSP page is named index.jsp. It makes use of special JSF tags. I provided text1 for entering some name and text2 for displaying the echoed result. I have given the code below,



<%@ page  language="java"%>
<%@  taglib  uri="http://java.sun.com/jsf/html"  prefix="h"%>
<%@  taglib  uri="http://java.sun.com/jsf/core"  prefix="f"%>

<html>
<body>

<f:view>
     <h:form  id="echoform">
     <h:outputText  value="name"/>
     <h:inputText id="text1"
               value="#{backing.name}" /><br/>

     <h:outputText  value="result"/>
     <h:inputText id="text2"
               value="#{backing.result}"   /> 
        <br/>

       <h:commandButton   id="button1"
          action="#{backing.echo}"
          value="click"/>
  <br/>
     </h:form>
</f:view>

</body>
</html>
-----------------------------
This means that whatever value we give for text1 is attached as attribute value of an object of backing bean(we have named it as backing.java). I am giving the code below.



package mypack;

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.*;


@ManagedBean(name="backing")
@SessionScoped






public class backing 
{


String  name;
String result;






  public void setName(String a){   name=a;  }
  public void setResult(String a){result=a;}




  public  String getName(){return name;}
  public  String getResult(){return result;}


  //------------------------------------------


  public String   echo()
        {


        String s=getName();
        echobean  bean1 =  new  echobean();


        String r=bean1.echothis(s);




        setResult(r);
        return  "success";


        }


}
//------------------------------
echobean.java


I am in g drive.
My folder is named 'g:\jsf2echoapp\mypack'
we have to include jsf-api.jar(for jsf2).
I compiled backing.java.


As usual create context named jsf2echoapp.
Create WEB-INF.place the following web.xml file in WEB-INF as usual.Also faces-config.xml.
web.xml
------

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
      http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  version="2.5">
  <!-- The bare minimum needed for JSF 2.0 is a servlet 2.5
      declaration and the mapping for the FacesServlet.
      Setting PROJECT_STAGE to Development is highly recommended
      during initial development so that you get more helpful
      error messages.
      
      From JSF 2.0 tutorial at http://www.coreservlets.com/JSF-Tutorial/jsf2/
  -->
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.jsf</url-pattern>
  </servlet-mapping>
  <context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
  </context-param>
  <welcome-file-list>
    <welcome-file>index.jsf</welcome-file>
  </welcome-file-list>
</web-app>
----------------------------------
faces-config.xml
---------------

<?xml version="1.0"?>
<faces-config xmlns="http://java.sun.com/xml/ns/javaee" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
      http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
   version="2.0">
  <!--  Empty for now. Your entries will go here. But even
        if you have no entries in faces-config.xml, you are required
        to have a valid faces-config.xml file with legal 
        start and end tags.
        
        From JSF 2.0 tutorial at http://www.coreservlets.com/JSF-Tutorial/jsf2/ -->







</faces-config>
-----------------------------------
Create classes folder under WEB-INF.
Create mypack folder under classes.
Place backing.class in mypack folder
place index.jsp in context root jsf2echoapp.
In lib folder under WEB-INF we have the following jar files.


jsf-api.jar
jstl-impl-1.2.jar
jsf-impl.jar
jstl-api-1.2.jar
-----------------------------
our deployment work is over.
I am using tomcat7.
To start tomcat
set java_home=c:\jdk1.6
startup
now type the url as 
"http://localhost:8080/jsf2echoapp"
-------------------------------
we need not mention index.jsp.
 It is default.


OUTPUT