Drop List error

The drop list is supposed to be populated by a form before the user enters the jsp page. The error is - "Failed to obtain specified collection". I think it is a servlet call or the logic of javascript and JSP whichever either process first. I'm trying to do the droplist, then I'll be doing the chain droplist by subcategories via ajax call. Any other strategies might be nice. I added the<body onload="retrieveCategories();"> to call the function in the js -

var req;

function retrieveCategories(){

var url ="/CategoriesAction/categories;"

if (window.XMLHttpRequest){

req =new XMLHttpRequest();

}

elseif (window.ActiveXObject){

req =new ActiveXObject("Microsoft.XMLHTTP");

}

req.open("Get", url,true);

req.send();

}

struts-config.xml - the js should get this info and call CategoriesAction.

<action input="/addsheet.jsp" name="refsheetsForm" path="/categories" scope="request" type="com.refsheets.struts.CategoriesAction" validate="false"/>

This code summons the struts Action - CategoriesAction. It gets the list of categories from database and setAttribute in the form. I'm not sure if it's right -request.setAttribute(categories, list); I basically set the first parameter to the form - Collection categories, and supply the list(data) to be set in.

publicclass CategoriesActionextends Action{

/* forward name="success" path="" */

privatefinalstatic String SUCCESS ="success";

Collection list =new ArrayList();

String categories ="categories";

public ActionForward execute(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response)

throws Exception{

DAOFactory TomcatFactory =

DAOFactory.getDAOFactory(DAOFactory.TOMCAT);

ClientDAO clientdao = TomcatFactory.getClientDAO();

list = clientdao.getCategories();

request.setAttribute(categories, list);

return mapping.findForward(SUCCESS);

}

}

the form - as you can see the setCategories(Collection categories) it should be set up before the droplist requests this collection.

public Collection getCategories(){

return categories;

}

publicvoid setCategories(Collection categories){

this.categories = categories;

}

The jsp page - placed the keys to get the form data. I couldn't figure out why it is still returning an error.

<html:select property="category" value="Select Categories">

<html:optionsCollection property="categories"/>

</html:select>

[4061 byte] By [Meepa] at [2007-11-27 11:42:27]
# 1

Where is value and label attributes in your html:optionsCollection?

<html:optionsCollection property="categories"/>

<html:optionsCollection property="categories" value="id" label="category"/>

This id & category should match with your bean class.

skp71a at 2007-7-29 17:44:13 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

In your,

list = clientdao.getCategories();

you should populate the bean something like this:

ArrayList myList = new ArrayList();

while (rs != null && rs.next()) { //assumed you're getting data from database

YourTO yourTO = new YourTO(); //this bean must have id, category and getter/setter

yourTO.setCategoryId(rs.getString(1));

yourTO.setCategory(rs.getString(2));

myList.add(yourTO);

}

skp71a at 2007-7-29 17:44:13 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

I'm not sure what do you meant - id & category.

this collection only returns from a SQL statement which is

Games

Programming

Software Development

Hardware

The 'bean class' is an Action class? if you are talking about this - it's - request.setAttribute(categories, list);

It'll be

<html:select property="category" value="Select Categories">

<html:optionsCollection property="categories" value="list" label="category"/>

</html:select>

It didn't work, it still returned an error.

Meepa at 2007-7-29 17:44:13 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Ok fixing this - Didn't see your second reply. I'll back to you.

Meepa at 2007-7-29 17:44:13 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

I changed a SQL statement to return cid (id) and category.

it looks like

1Programming

2Software Development

etc.

I changed this code - value = cid. label = category. its properties is collection - categories. It still returns the error.

<html:select property="category" value="Select Categories">

<html:optionsCollection property="categories" value="cid" label="category"/>

</html:select>

Meepa at 2007-7-29 17:44:13 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

I added the logj4 statement in the CategoriesAction, it looks like it wasn't summoned. Is this correct? the 'categories' is the path in the struts.

var url = "/CategoriesAction/categories;"

struts -

<action input="/addsheet.jsp" name="refsheetsForm" path="/categories" scope="request" type="com.refsheets.struts.CategoriesAction" validate="false"/>

Meepa at 2007-7-29 17:44:13 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

<action path="/categories"

type="com.refsheets.struts.CategoriesAction"

name="refsheetsForm"

scope="request"

input="/jsp/addsheet.jsp">

<forward name="success" path="/jsp/addsheet.jsp" />

</action>

skp71a at 2007-7-29 17:44:13 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8

Did you resolve this issue?

skp71a at 2007-7-29 17:44:13 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9

The error was list.setAttribute(list, categories);

It should be refForm.setCategories(list); which sends the collection to the form. And then, it'll populate the optionsCollections.

Meepa at 2007-7-29 17:44:13 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10

Yes, thanks - now I have to work on the chaining droplist - I think I better review the code and make sure I understand all.

Meepa at 2007-7-29 17:44:13 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...