jsf - Dynamic dropdown is not coming -


while selecting dropdown,the second drop down values not coming properly.i attaching code here xhtml file:

<?xml version="1.0" encoding="utf-8"?> <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en"  "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"    xmlns:f="http://java.sun.com/jsf/core"        xmlns:h="http://java.sun.com/jsf/html">    <h:head>     <f:event listener="#{userdata.handleevent}" type="prerenderview" />      </h:head>    <h:body>  <f:view> <h:form > <table> <tr> <td> <h:messages showdetail="true" /> </td> </tr> <tr> <td><h:outputtext value="enter name : "/></td> <td><h:inputtext value="#{userdata.clientname}"/></td> </tr> <tr> <td><h:outputtext value="enter id : " /></td> <td><h:inputtext value="#{userdata.id}" /></td> </tr> <tr> <td></td> <td><h:commandbutton value="insert" action="#{userdata.add}"/></td> </tr>  <tr> <td></td> <td>#{userdata.data}</td> </tr> <tr> <td><h:selectoneradio value="#{authordata.favcolor1}" id="div2">     <f:selectitem itemvalue="red" itemlabel="color1 - red" />     <f:selectitem itemvalue="green" itemlabel="color1 - green"/>     <f:ajax event="click" binding="valbinding"  execute="@form" render=":div1" listener="#{authordata.setchangeval}"/> </h:selectoneradio></td> </tr> </table> </h:form>  <h:panelgroup id="div1" style="display: #{authordata.valdisplay};"> <span> have selected #{authordata.message} </span> </h:panelgroup> </f:view>      <br/>              <h:outputlabel value="choose car: *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" for="firstdropdwn" />              <h:selectonemenu id="firstdropdwn" value="#{authordata.favoritecar2}">                  <f:selectitems value="#{authordata.car4list}" var="d"                   itemlabel="#{d.carlabel}" itemvalue="#{d.carvalue}"/>                  <f:ajax event="click" binding="valbinding1"  execute="@this firstdropdwn" render=":seconddrpdown" listener="#{authordata.setdropdwnval}"/>               </h:selectonemenu>     <br/>        <h:outputlabel value="choose owner: *&nbsp;&nbsp;&nbsp;" for="seconddrpdown" />                    <h:selectonemenu id="seconddrpdown" value="">                  <f:selectitems value="#{authordata.car3list}" var="c"                     itemlabel="#{c.carlabel}" itemvalue="#{c.carvalue}"/>               </h:selectonemenu>     </h:body> </html>  

author.java:

import java.io.serializable; import java.sql.connection; import java.sql.drivermanager; import java.sql.preparedstatement; import java.sql.resultset; import java.sql.sqlexception; import java.util.arraylist; import java.util.collection; import java.util.iterator; import java.util.linkedhashmap; import java.util.linkedlist;  import java.util.list; import java.util.listiterator; import java.util.map;  import javax.faces.bean.managedbean; import javax.faces.bean.sessionscoped; import javax.faces.bean.viewscoped; import javax.faces.component.uicomponent; import javax.faces.event.ajaxbehaviorevent; import javax.faces.event.componentsystemevent;  @managedbean(name = "authordata", eager = true) @sessionscoped public class author implements serializable {    private static final long serialversionuid = 1l;     string clientname;     string id;     string favcolor1;     public string favoritecar2;      private uicomponent valbinding;     private uicomponent valbinding1;     public list<car> car3list;     /*public car[] getfavoritecar3value()          {              car3list = new car[3];             car3list[0] = new car("bmw series 4 - 316", "316");             car3list[1] = new car("bmw series 4 - 318", "318");             car3list[2] = new car("bmw series 4 - 320", "320");                return car3list;          }    */      public list<car> getcar3list() {         return car3list;     }      public void setcar3list(list<car> car) {         this.car3list = car;     }       public list<car> car4list;          public list<car> getcar4list() {          car4list = new arraylist<car>();         car4list.add(new car("bmw","116"));         car4list.add(new car("audi","118"));         car4list.add(new car("mercedes","120"));         return car4list;     }        public uicomponent getvalbinding1() {         return valbinding1;     }           public void setvalbinding1(uicomponent valbinding1) {         this.valbinding1 = valbinding1;     }     private string valdisplay="none";      private string message;      public string getfavoritecar2() {         return favoritecar2;     }           public void setfavoritecar2(string favoritecar2) {         this.favoritecar2 = favoritecar2;     }     private static map<string, string> favoritecar2value;          static          {              favoritecar2value = new linkedhashmap<string, string>();             favoritecar2value.put("bmw series 1 - 116", "116"); //label, value             favoritecar2value.put("bmw series 1 - 118", "118");             favoritecar2value.put("bmw series 1 - 120", "120");          }         public map<string, string> getfavoritecar2value()        {            return favoritecar2value;         }           public string getmessage() {         return message;     }     public void setmessage(string message) {         this.message = message;     }     public string getvaldisplay() {         return valdisplay;     }     public void setvaldisplay(string valdisplay) {         this.valdisplay = valdisplay;     }     public uicomponent getvalbinding() {         return valbinding;     }     public void setvalbinding(uicomponent valbinding) {         this.valbinding = valbinding;     }     public string getid() {         return id;     }     public void setid(string id) {         this.id = id;     }     public string getclientname() {         return clientname;     }     public void setclientname(string clientname) {         this.clientname = clientname;     }       public string getfavcolor1() {         return favcolor1;     }     public void setfavcolor1(string favcolor1) {         this.favcolor1 = favcolor1;     }     public void setchangeval(ajaxbehaviorevent e){         this.setvaldisplay("none");         if(this.getfavcolor1().equalsignorecase("red")){           this.setvaldisplay("block");           this.setmessage("red");         }else if(this.getfavcolor1().equalsignorecase("green")){             this.setvaldisplay("block");             this.setmessage("green");         }      }      public void setdropdwnval(ajaxbehaviorevent e){          if(this.getfavoritecar2().equalsignorecase("116")){             linkedlist<car> ob = new linkedlist<car>();             ob.add(new car("aife","0"));             ob.add(new car("anirban","1"));             this.setcar3list(ob);          }else {              linkedlist<car> ob1 = new linkedlist<car>();             ob1.add(new car("saife","0"));             ob1.add(new car("abid","1"));             this.setcar3list(ob1);          }      }   } 

car.java:

import java.io.serializable;   public class car implements serializable {     public string carlabel;     public string carvalue;     public car(string carlabel, string carvalue) {         this.carlabel=carlabel;         this.carvalue=carvalue;      }     public string getcarlabel() {         return carlabel;     }     public void setcarlabel(string carlabel) {         this.carlabel = carlabel;     }     public string getcarvalue() {         return carvalue;     }     public void setcarvalue(string carvalue) {         this.carvalue = carvalue;     }  } 

the second dropdown down not coming properly.in listener method of first dropdown value first drop down not 116/118/120. ***the dbms portion ignored.i had put implementation.

i able spot several problems.

  1. you closed h:form after group of radio buttons, continued put other elements outside form. similarly, f:view closed right after panel group, yet other form elements appear after one. move closing tags h:form , f:view right after closing tag of last h:selectonemenu (in order), should fix 1 part of problem.

  2. both of f:ajax bound click, change change, otherwise actions performed when user, example, opens dropdown.

  3. both of f:ajax have render defined starting colon - had remove colon, otherwise elements not found.

  4. the "choose owner" dropdown's value bound empty string, not correct. change value="#{authordata.favoritecar2}".

  5. the method author.getcar4list() creates new list each time called. consider keeping return car4list; there , moving creation separate method, annotated @postconstruct called once:

    @postconstruct private void init() {     car4list = new arraylist<car>();     car4list.add(new car("bmw", "116"));     car4list.add(new car("audi", "118"));     car4list.add(new car("mercedes", "120")); } 

i hope these changes made, cannot recall there else needed.

userdata definition not part of code shared here, had replace of these references authordata , remove few of them, should not have impact things mentioned above.


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -