Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Struts 1.2 - HTML:SELECT problem

437 views
Skip to first unread message

Rudi

unread,
Aug 19, 2008, 2:31:38 PM8/19/08
to
Hi Everyone,

I'm new at using Struts. I'm using MyEclipse version 6 in Windows XP
and trying to have a dropdown list in a jsp with Struts. I tried using
the html:select tag but keep getting an error:

javax.servlet.ServletException: javax.servlet.jsp.JspException: Cannot
find bean under name org.apache.struts.taglib.html.BEAN

Also:

org.apache.jasper.JasperException: An exception occurred processing
JSP page /jsp/invoiceList.jsp at line 14

11:
12: <body>
13:
14: <html:select property="staff_number">
15: <html:options property="members" />
16: </html:select>
17:

The code is below. I'm not sure how to setup the select (options)
right obviously since I keep getting these error messages. The
contents in the collection (a list of type Staff) are correct and I
can display them to the console. So the attribute members has data.
I'm also able to display the contents of members to a table in the jsp
page using iterate. Hence the problem is only getting the html:select
to work. I have tried different variations and always complains about
some Bean without much detail or clue to help. The code is provided
below.

Any help is appreciated. Thanks in advance.

Best regards,

Rudi

=============================================
InvoiceList.jsp

<%@ page language="java" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean"
prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html"
prefix="html"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic"
prefix="logic" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-nested"
prefix="nested" %>

<html>
<head>
<title>Show invoice list</title>
</head>

<body>

<html:select property="staff_number">
<html:options property="members" />
</html:select>

<table border="1">
<tbody>
<logic:notEmpty name="invoiceListForm" property="members">
<logic:iterate name="invoiceListForm" property="members"
id="staff">
<tr>
<%-- print out the book informations --%>
<td><bean:write name="staff" property="staff_number" /></td>
<td><bean:write name="staff" property="staff_name" /></td>
</tr>
</logic:iterate>
</logic:notEmpty>
</tbody>
</table>

</body>
</html>

=============================================
Staff.java

package com.blablabla;

public class Staff implements java.io.Serializable {

private static final long serialVersionUID = 1L;

private String staff_number;
private String staff_name;

protected Staff() {}

public Staff(String staff_number, String staff_name) {
this.staff_number = staff_number;
this.staff_name = staff_name;
}

public String getStaff_number() {
return staff_number;
}

public void setStaff_number(String staff_number) {
this.staff_number = staff_number;
}

public String getStaff_name() {
return staff_name;
}

public void setStaff_name(String staff_name) {
this.staff_name = staff_name;
}

}

=============================================
InvoiceListAction.java

package com.blablabla.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.blablabla.*;
import com.blablabla.struts.form.InvoiceListForm;
import com.blablabla.sirs.hibernate.*;

import org.hibernate.*;
import org.hibernate.criterion.Projections;

import java.util.*;

public class InvoiceListAction extends Action {

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {

InvoiceListForm invoiceListForm = (InvoiceListForm) form;

SessionFactory factory = null;
Session session = null;

try {

factory = HibernateSessionFactory.getSessionFactory();
session = (Session) factory.openSession();

List<Staff>staffmbrs = session.createQuery("from Staff
").list();

System.out.println("Query Size = " + staffmbrs.size());

if (staffmbrs.isEmpty()) {
System.out.println("Could not get book using embedded
query");
} else {
for (int i = 0; i < staffmbrs.size(); i++) {
System.out.print("The name is " +
staffmbrs.get(i).getStaff_number());
System.out.print(" ");
System.out.println(staffmbrs.get(i).getStaff_name());
}
}

invoiceListForm.reset(mapping, request);
invoiceListForm.setMembers(staffmbrs);
invoiceListForm.setMemberSelected("GC");

} finally {
session.close();
}

return mapping.findForward("showList");
}
}

=============================================
InvoiceListForm.java

package com.blablabla.struts.form;

import java.util.ArrayList;
import java.util.Collection;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import com.blablabla.Staff;

public class InvoiceListForm extends ActionForm {

private static final long serialVersionUID = 1L;

private Collection<Staff> members;
private String memberSelected;

public Collection<Staff> getMembers() {
return members;
}

public void setMembers(Collection<Staff> staffmembers) {
this.members = staffmembers;
}

public String getMemberSelected() {
return memberSelected;
}

public void setMemberSelected(String memberSelected) {
this.memberSelected = memberSelected;
}

public void reset(ActionMapping mapping, HttpServletRequest request)
{
members = new ArrayList<Staff>();
memberSelected = null;
}

}


Dirk Michaelsen

unread,
Aug 20, 2008, 4:14:42 AM8/20/08
to
Rudi <diaz_ru...@yahoo.com> wrote:

><html:select property="staff_number">
> <html:options property="members" />
></html:select>

Sure you're getting an error. If you where Struts how could you know
wich part of the Collection you should display?

Try something like this:

<html:select property="staff_number">
<html:options property="members" key="staff_name"
value="key_number"/>
</html:select>

Dirk

Dirk Michaelsen

unread,
Aug 20, 2008, 4:45:46 AM8/20/08
to
I meant:

<html:select property="staff_number">
<html:options property="members" key="staff_name"

value="staff_number"/>
</html:select>

Dirk

Rudi

unread,
Aug 25, 2008, 5:00:25 PM8/25/08
to


Hi Dirk,

Thanks for the reply.

You're right. I should specify what part of the collection
to show and what to use for value. However, when I tried to
use the "key" and "value" keywords, MyEclipse flag those words
as error.

I replaced those keywords with "property" and "labelProperty"
to try to get things going.

So far I have tried:

<html:select property="memberSelected" value="GC" >
<html:options name="members" property="staff_number"
labelProperty="staff_name" />
</html:select>

I get an error:

org.apache.jasper.JasperException: javax.servlet.ServletException:
javax.servlet.jsp.JspException: Cannot find bean under name members

and also if I use:

<html:select property="staff_number" >
<html:options collection="members" property="staff_number"
labelProperty="staff_name" />
</html:select>

which is a bit similar to what you proposed, and I get an error:

javax.servlet.ServletException: javax.servlet.jsp.JspException: Cannot
find bean under name org.apache.struts.taglib.html.BEAN

I'm using Struts 1.2. Any help is appreciated.

Thanks,

Rudi


Donkey Hottie

unread,
Aug 26, 2008, 1:48:58 AM8/26/08
to
Rudi <diaz_ru...@yahoo.com> wrote in news:fda1b123-aa5b-4ee4-83ef-
7396c4...@b30g2000prf.googlegroups.com:

> and also if I use:
>
><html:select property="staff_number" >
> <html:options collection="members" property="staff_number"
> labelProperty="staff_name" />
></html:select>
>
> which is a bit similar to what you proposed, and I get an error:
>
> javax.servlet.ServletException: javax.servlet.jsp.JspException: Cannot
> find bean under name org.apache.struts.taglib.html.BEAN
>
> I'm using Struts 1.2. Any help is appreciated.
>

Try optionsCollection

If "members" is a form property:

<html:select property="staff_number" >
<html:optionsCollection property="members" value="staff_number"
label="staff_name"/>
</html:select>

or if members is set with request.setAttribute("members", members) then

<html:select property="staff_number" >
<html:optionsCollection name="members" value="staff_number"
label="staff_name"/>
</html:select>

Dirk Michaelsen

unread,
Aug 26, 2008, 2:01:31 AM8/26/08
to
Rudi <diaz_ru...@yahoo.com> wrote:

> You're right. I should specify what part of the collection
>to show and what to use for value. However, when I tried to
>use the "key" and "value" keywords, MyEclipse flag those words
>as error.

ok my fault. I tried to answer this question by memory ;-)

Here is some code from a productive application that works just fine
with Struts 1.2.9:

<html:select property="filter">
<html:optionsCollection property="filterList"
label="key" value="value" />
</html:select>

The Collection that is used for display here is a Collection of simple
KeyValueBeans implemented like this:

public class KeyValueBean<K extends Comparable, V> implements
Comparable<KeyValueBean>, Serializable {

private K key;
private V value;

// + Getters and Setters
}

this approach must work.

Dirk

Rudi

unread,
Aug 26, 2008, 1:21:10 PM8/26/08
to
On Aug 26, 2:01 am, Dirk Michaelsen <dim...@gmx.net> wrote:


Hi Donkey and Dirk,

Thanks for the posts and suggestions. I finally got the select to
work.

The jsp code is:

<html:select property="memberSelected" >
<html:optionsCollection property="members" label="staff_number"
value="staff_name"/>
</html:select>

As you guys suggested, I used the label and value fields in the
option part.

As Dirk suggested, I used a key value bean collection, a list of
Staff members called
staffmbrs.

Then I made that list an attribute of the request:

request.setAttribute("members", staffmbrs);

Finally, memberSelected is a property that I defined in the Struts
Form and is supposed
to hold the currently selected item in the list.

Now I'm trying to figure out how to get the selected item from the
dropdown list once
the user makes a selection. I guess that would involve submitting the
form and some sort
of javascript.

By the way, can you guys tell me what is your impression of the
Struts books out there?
Could you recommend any good Struts 1.2 book that has good examples
useful for programmers?

Have a great day and thanks again for all your help. :)

Best regards,

Rudi


Donkey Hottie

unread,
Aug 26, 2008, 1:44:22 PM8/26/08
to
Rudi <diaz_ru...@yahoo.com> wrote in news:d44b044a-8194-4ba1-aae5-
247b22...@o40g2000prn.googlegroups.com:

>
> Hi Donkey and Dirk,
>
> Thanks for the posts and suggestions. I finally got the select to
> work.
>
> The jsp code is:
>
><html:select property="memberSelected" >
> <html:optionsCollection property="members" label="staff_number"
> value="staff_name"/>
></html:select>

>
> As you guys suggested, I used the label and value fields in the
> option part.
>
> As Dirk suggested, I used a key value bean collection, a list of
> Staff members called
> staffmbrs.

No! That has it all backwards!

label property is the what you want shown to the user, you have
"staff_number" there? Have you constructed the key-value bean backwards?

I think it should be

value="staff_number" label="staff_name"

as you certainly want show "name" and select "number".

>
> Then I made that list an attribute of the request:
>
> request.setAttribute("members", staffmbrs);

Oh no. If you set the collection as request attribute, you have to use
"name", not the "property" in the optionsCollection.

>
> Finally, memberSelected is a property that I defined in the Struts
> Form and is supposed
> to hold the currently selected item in the list.
>
> Now I'm trying to figure out how to get the selected item from the
> dropdown list once
> the user makes a selection. I guess that would involve submitting the
> form and some sort
> of javascript.

No javascript needed, nor does it help any.

The form is submitted to your Action (form action="/myaction.do"), not sure
about this how it goes in struts-config as I use XDoclet to generate
these..

The selected item should be

MyFormClass myForm = (MyFormClass) form ;
Integer selectedMember = myForm.getMemberSelected() ;

or

DynaActionForm dynaForm = (DynaActionForm) form ;
Integer selectedMember = (Integer) dynaForm.get("memberSelected") ;

if the form is a DynaActionForm which I use.

Struts may be challenging sure..

Rudi

unread,
Aug 26, 2008, 2:38:37 PM8/26/08
to
On Aug 26, 1:44 pm, Donkey Hottie <s...@plc.is-a-geek.com> wrote:
> Rudi <diaz_ruben2...@yahoo.com> wrote in news:d44b044a-8194-4ba1-aae5-
> 247b2289d...@o40g2000prn.googlegroups.com:


Hi,

You're right. I wrote it backwards in the post. The value has the
key,
and the label is what one shows the user on the jsp. Sorry about
that.

Correct. I noticed that I had members also as a form attribute.
Geez.
So, I commented out the code in the Struts Action where I set an
attribute
called members too. That way I don't need to change anything and can
use
property.

> No javascript needed, nor does it help any.

Great.

> The form is submitted to your Action (form action="/myaction.do"), not sure
> about this how it goes in struts-config as I use XDoclet to generate
> these..

I understand. Thanks. I'll try getting more of these things to work
and see
how it goes. Thanks for the code piece to show how to get the selected
item too.

Let me know if you guys know of any books that have good Strut
examples that
are useful for programmers.

Have a nice day. Thanks again.

Best regards,

Rudi

0 new messages