在选择下拉列表时,第二个下拉值未正确显示。我在此处附加代码 xhtml文件:
<?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;
}
}
第二个下拉列表未正确显示。在第一个下拉列表的监听方法中,第一个下拉列表的值不是116/118/120。 *** dbms部分将被忽略。我已将其用于另一个实现。
答案 0 :(得分:1)
我能够发现几个问题。
您在单选按钮组之后关闭了h:form
,但继续将其他元素放在表单之外。同样,您的f:view
在面板组之后立即关闭,但在此之后会出现其他表单元素。只需在关闭最后一个h:form
的标记后按此f:view
和h:selectOneMenu
移动结束标记(按此顺序),这应该可以解决问题的一部分。
您的f:ajax
都绑定到click
,将其更改为change
,否则即使用户打开下拉列表也会执行操作。
你的f:ajax
都将render
定义为以冒号开头的东西 - 我必须删除冒号,否则找不到元素。
“选择您的所有者”下拉列表的value
绑定为空字符串,这是不正确的。将其更改为value="#{authorData.favoriteCar2}"
。
方法Author.getCar4List()
每次调用时都会创建一个新列表。考虑仅保留return car4List;
并将创建移动到单独的方法,使用@PostConstruct
注释,以便仅调用一次:
@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"));
}
我希望这些都是我所做的改变,不记得还有什么需要。
userData
定义不是您在此处共享的代码的一部分,因此我必须使用authorData
替换其中一些引用,并删除其中的一些,但这不会对事物产生太大影响如上所述。