Monday 26 March 2012

SIMPLE QUERY DEMO USING JSF-2 ANNOTATION

SIMPLE QUERY DEMO  IN JSF-2
  (using Annotation)
---------------------------

   We have already seen echo example.
Almost the same code can be easily
used for a query example. The only 
change will be which helper bean we
will be using. Instead of my name in
text1, I will send query string. The
result will be Vector which can be 
suitably displayed in a dataTable(spcial 
JSF control). Instead of echo bean, 
I will use querybean. We can use either 
hiberbean or ordianary JDBC bean. I am
using JDBC bean. I am giving the code now.


    index.jsp
--------------
<%@ 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 bgcolor='gold' text='blue'>
<center>



<f:view>
     <h:form  id="queryform">
     <h:outputText  value="query"/>


      <h:inputText id="text1" value="#{backing.query}" /><br/>


    <h:commandButton   id="button1"
                          action="#{backing.showrecords}"
                          value="getrecords"/>


   <h:dataTable value="#{backing.players}" var="ob">
      <h:column>
           <f:facet name="header">
              <h:outputText  value="ID"/>
           </f:facet>   


            <h:outputText  value="#{ob.id}"/>
      </h:column>

      <h:column>
           <f:facet name="header">
              <h:outputText  value="NAME"/>
           </f:facet>


           <h:outputText  value="#{ob.name}"/>
       </h:column>

       <h:column>
           <f:facet name="header">
              <h:outputText  value="PLACE"/>
           </f:facet>


            <h:outputText  value="#{ob.place}"/>
       </h:column>

    </h:dataTable>






    </h:form>
</f:view>


 </center>
    </body>
</html>
==============================================



 The corresponding backing bean is given below.


// backing.java


package mypack;

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

@ManagedBean(name="backing")
@SessionScoped


public class backing
{


Vector players ;
String  query;




        public  void setQuery(String a){query=a;}
        public  String  getQuery(){return query;}


        public   void  setPlayers(Vector v){players=v;}
        public Vector getPlayers() {   return players; }


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


        public  String  showrecords()
        { 


         String  a=getQuery();


         querybean   bean1= new querybean();




         Vector  v  = bean1.getrecords(a);


         setPlayers(v);


         return  "success";
        }


}
---------------------------------------------------------------------------------------------------------------------------------------
Note the  Annotation. And i did not change either web.xml  or
faces-config.xml. Everything is taken care by annotation.
compiling and deployment procedure is as before.


















In the next post I will give a demo for the usual  CRUD  operations.