使用LinkedHashMap在h:selectManyListbox中添加所选项目

时间:2015-11-07 08:59:15

标签: jsf

我正在研究JSF应用程序。我使用新的LinkedHashMap从数据库表中检索两个列值以填充列表框。第一列包含Item,第二列包含Price。这些项目显示在我的列表框中,但是当我选择一个或两个项目并单击按钮计算时,它会给我错误。错误是

  

javax.faces.FacesException:目标模型类型不是集合或数组

请参阅下面的代码 Java

String url = "jdbc:mysql://localhost:3306/ListBox";
String user = "root";
String pw = "root";

String favlst;
double total;
double price;
String item;
Map<String,Object> lst; {
    try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(url, user, pw);

String sql = "SELECT * FROM list";
Statement stt = con.createStatement();
ResultSet rs = stt.executeQuery(sql);

lst = new LinkedHashMap<String, Object>();
while(rs.next()){
     price = rs.getDouble("Price");
     item = rs.getString("Item");
    lst.put(item, price);
}

}catch(Exception e){

    }
}

public double getTotal() {
    return total;
}

public void setTotal(double total) {
    this.total = total;
}

public Map<String, Object> getSelectlst() {
    return lst;
}

public String Calculate(){
    total = 0;
    price = 0;
    total += (Double)lst.get(item);
return "success";
   }
 }

JSF

<h:form>
    <h3> Generated by Map </h3>

    <h:selectManyListbox value = "#{menu2.selectlst}">
        <f:selectItems value = "#{menu2.selectlst}"/>
    </h:selectManyListbox>  

    <br> <br>

    <h:commandButton value = "Calculate" action = "#{menu2.Calculate}"/>
    <br> <br>
Total: Rs <h:outputLabel value = "#{menu2.total}"></h:outputLabel>

</h:form>

1 个答案:

答案 0 :(得分:1)

您的第一个错误是您在f:selectItems en h:selectManyListbox的value属性中使用了相同的属性。那一定不对。如果你看看LinkedHasMap api,https://docs.oracle.com/javase/6/docs/api/java/util/LinkedHashMap.html,错误是非常清楚的。将selectManyListbox值后面的属性更改为类型正确的属性。