Thursday, June 6, 2013

How to Convert date in SelectOneMenu in jsf or primefaces

I found in internet many developers face same problem with the SelectOneMenu of Primefaces.Especialy when using date conversion.
The date is showen in the SelectOneMenu like this  :

Thu May 30 00:00:00 WET 2013

when using this code :

<p:selectOneMenu id="Dateplanif" value="#{ gestionduplanning.dateplanificationnew}"> 
      <f:selectItems value="#{gestionduplanning.datelist}" var="da" itemValue="#{da}" itemLabel="#{da}"  />  
 </p:selectOneMenu> 

The Solution that i found for this problem is to convert my date list to String then convert it back to date.
I will show you a sample example where you need to chose a date from a list of dates ,and after clicking on the button you will be redirected to the second jsf page that have the date that you chosed :

jsf managedbean :
public class Testbean {
    @EJB
    private ManageOfPlanifieLocal manageOfPlanifie;
    List<Date> listdate = new ArrayList<Date>();
    List<String> listdatestring = new ArrayList<String>();
    String newdate="";
    Date dateplanification;     
 @PostConstruct
    public void initialize() {
        listdate=manageOfPlanifie.retournerdatedesplanif();
        Format formatter = new SimpleDateFormat("dd/MM/yyyy");     
        for(int i=0;i<listdate.size();i++)
        {listdatestring.add(formatter.format(listdate.get(i)));}     
    }  
    public String gototest2(String datedate) throws ParseException  
    {dateplanification= new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH).parse(datedate);
        return "test2.xhtml?faces-redirect=true";
    }
The first jsf page that show the list of dates :
<h2>Choix de l'equipe</h2>  
         <h:outputLabel for="dateplanif" value="date de planification : " />
         <p:selectOneMenu id="dateplanif"  value="#{testbean.newdate}" >             
             <f:selectItems value="#{testbean.listdatestring}" var="da" itemValue="#{da}" itemLabel="#{da}" />  
    </p:selectOneMenu>           

      <p:commandButton value="suivant"  style="color:black;" action="#{testbean.gototest2(testbean.newdate)}" update="@form" />

The second jsf page show the value selected :

 <h:outputText value="Date : "/>   
 <h:outputText value="#{testbean.dateplanification}" />  

Friday, May 24, 2013

How to set the default value of Selectonemenu Jsf Primefaces

This is an example to set the default value of Selectonemenu in Jsf Primefaces :

we have a user that we need change his group from a list of groups.


User.group : Administrateur

Application.groups : {Administrator,Moderator,guest}


<p:selectOneMenu value="#{User.group}">  
<f:selectItems value="#{Application.groups}" />  
</p:selectOneMenu>


The Result :

How to include any Customed jQuery script in Primefaces Jsf pages

As you know primefaces include by default jquery libs but I noticed that many primefaces developers need to add Customed jQuery in there Primefaces Jsf pages but they don't know how  .

to use jQuery script with Primefaces Jsf you need to import it like that in the <h:head></h:head> :


<script type="text/javascript" src="./js/ui3.js"/>

and in your jQuery script you need to start it with //<![CDATA[ and to end it with //]]>

Example :

//<![CDATA[ 
 $( init ); 
    function init() {
       (function($){
          var sortOpts = {
             connectWith: ["#sortablesA", "#sortablesB"]
          };
          $("#sortablesA, #sortablesB").sortable(sortOpts);
       })(jQuery);
    }
//]]>