Help loading collection into data table

[nobr]Hi all, I'm having what I believe to be a minor problem with getting my collection stored in a bean to output to a jsp page using jsf data table. I think the actual error is to due with the mapping from faces-config to the bean itself as the error message is "stockItems cannot be resolved as a member of stockListings" the stockListings bean is clearly defined in faces-config. I'm posting my faces-config.xml, the jsp page I'm using, and the bean I want to access below. I would appreciate any help anybody could provide as I can't figure out why it can't the collection in the class.

faces-config.xml==========================

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN" "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">

<faces-config>

<managed-bean>

<managed-bean-name>stockListings</managed-bean-name>

<managed-bean-class>logic.WebLogic</managed-bean-class>

<managed-bean-scope>request</managed-bean-scope>

</managed-bean>

<managed-bean>

<managed-bean-name>newsBean</managed-bean-name>

<managed-bean-class>model.DataBean</managed-bean-class>

<managed-bean-scope>request</managed-bean-scope>

</managed-bean>

<managed-bean>

<managed-bean-name>stockBean</managed-bean-name>

<managed-bean-class>model.StockBean</managed-bean-class>

<managed-bean-scope>request</managed-bean-scope>

</managed-bean>

<navigation-rule>

</navigation-rule>

</faces-config>

=================================================

showStocks.jsp==========================

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core" %>

<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html" %>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<base href="<%=basePath%>">

<title>My JSP'showStocks.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->

</head>

<body>

<strong>Stocks and Related Stories</strong> <br>

<f:view>

<h:dataTable border="1" var="stocks" rendered="true" value="#{stockListings.stockItems}" style="" headerClass="HEADING" rowClasses="row1">

<h:column>

<f:facet name="header">

<h:outputText value="Stocks"/>

</f:facet>

<h:outputText value="# {stocks.name}" />

</h:column>

</h:dataTable>

</f:view>

</body>

</html>

=================================================

WebLogic.jsp==========================

package logic;

import java.util.*;

import model.*;

publicclass WebLogicextends BaseLogic

{

public Collection stockItems;

//constructor

public WebLogic()

{

super();

}

//getters & setters

public Collection GetStockNames()

{

//pull out a the string name of the stock item to print in a

//data table

Collection tmpArray =new ArrayList();

for(int i = 0; i < StorageUnit.GetInstance().GetStockListSize(); i++)

tmpArray.add((StockBean)StorageUnit.GetInstance().GetStockItem(i));

stockItems = tmpArray;

return stockItems;

}

publicvoid SetStockItem(StockBean newStock)

{

StorageUnit.GetInstance().GetStockList().add(newStock);

}

}

[/nobr]

[6201 byte] By [sirmaca] at [2007-11-27 5:50:02]
# 1

First try to follow the naming conventions: http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html

At least the methodnames should start with lowercase.

And you also have to follow the Javabean spec for every property which you want to use in a JSF page: encapsulated (private) properties which can be accessed by accessors (public getter/setter).

So replace public class WebLogic extends BaseLogic

{

public Collection stockItems;

}

bypublic class WebLogic extends BaseLogic {

private Collection stockItems; // Declare the property private.

public Collection getStockItems() { // Create a public getter.

return stockItems;

}

public void setStockItems(Collection stockItems) { // And a public setter.

this.stockItems = stockItems;

}

}

If you're using Eclipse as IDE then you can autogenerate getters/setters by SHIFT+ALT+S and then R, while the focus is on the code.

BalusCa at 2007-7-12 15:37:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Thank you so much, I'm still new to Java and jsf especially, and I didn't figure naming conventions would be so important. Thank you very much for your help that fixed the problem!
sirmaca at 2007-7-12 15:37:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...