Sunday, August 21, 2016

Fill Multi language Select One Choice from Table using java Bean

In this article i will show how to fill Select one Choice component from database based on Default browser language .
  • Create Table with name "Lov" that contains two languages columns for display text . as show below :
   

  • Create jspx page and create java bean for it
  • inside the java bean define Selecitems object as follow:                                                                             private List listOfItems = new ArrayList();
  • drag drop selectonechoice component to your page the following dialog will appear to determine the list that your SOC will reference to
  • choose First option and click Bind and map to listOfItems in your java bean then click next . in the next form fill the label text and requiredmessagedetails



  • Generate the accessors
  • i suppose that Lov List will be changed based on Browser language , to catch the current language i have added Resource key with name "language"  inside the available Bundles then i will read the value of this key every time i need to check the language .                                                    
                                                                                                                                                 
  • in getListOfItems add the following Code                                                                                   public List getListOfItems() throws SQLException                                                                                                                {                                                                                                                                                                                        Connection DBConn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","user","password");
                          Statement statement = null;                                                                                                                        
                          statement = DBConn.createStatement();                                                                                                    
                          FacesContext context = FacesContext.getCurrentInstance();                                                                        
                          String Lang = context.getApplication().evaluateExpressionGet(context, "#{bundle['language']}", String.class);
                          // or use  Locale browserLocale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
                          // browserLocale.getDisplayLanguage() ;
                          String strQuery =null ;
                          if (Lang=="en")
                             {                                                                                                                                                                
                                  strQuery = "select ID, EN_Name Lov_Name from Lov" ;
                              }
                          else
                             {
                                  strQuery = "select ID, AR_Name Lov_Name from Lov" ;
                              }
                         ResultSet rs = statement.executeQuery(strQuery);
                       
                         while (rs.next())
                            {
                               SelectItem si = new SelectItem();
                               si = new SelectItem();
                               si.setLabel(rs.getString("ID"));
                              si.setValue(rs.getString("Lov_Name"));
                              listOfItems.add(si);
                            }
                return listOfItems;
    }



  • to test your code change the language of Browser to see the effect

No comments:

Post a Comment