Reputation: 9
While selecting a dropdown,the second drop down values are not coming properly.I am attaching the 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>
You have selected #{authorData.message}
</span>
</h:panelGroup>
</f:view>
<br/>
<h:outputLabel value="Choose your Car: * " 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 your Owner: * " 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 is not coming properly.In listener method of first dropdown the value from first drop down is not 116/118/120. ***The dbms portion is to be ignored.I had put it for another implementation.
Upvotes: 0
Views: 83
Reputation: 1131
I was able to spot several problems.
You closed your h:form
just after the group of radio buttons, but continued to put other elements outside the form. Similarly, your f:view
is closed right after the panel group, yet other form elements appear after this one. Just move the closing tags for your h:form
and f:view
right after closing tag of the last h:selectOneMenu
(in this order), this should fix one part of your problem.
Both of your f:ajax
are bound to click
, change this to change
, otherwise actions are performed even when user, for example, just opens the dropdown.
Both of your f:ajax
have render
defined as something starting with colon - I had to remove the colon, otherwise the elements were not found.
The "Choose your owner" dropdown's value
is bound to an empty string, which is not correct. Change this to value="#{authorData.favoriteCar2}"
.
The method Author.getCar4List()
creates a new list each time it is called. Consider keeping only return car4List;
there and moving the creation to a separate method, annotated with @PostConstruct
so that it is only 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 are all the changes I made, cannot recall there would be anything else needed.
userData
definition was not part of the code you shared here, so I had to replace some of these references with authorData
and remove few of them, but that should not have much impact to the things mentioned above.
Upvotes: 1