• Post Reply Bookmark Topic Watch Topic
  • New Topic

populating a select box on a jsp from a database  RSS feed

 
Dan Fox
Greenhorn
Posts: 3
Android Firefox Browser Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I'd like to populate a <select> on a jsp. Right now I have this working, but it looks ugly and I have a lot of embedded Java in the jsp.

I have a bean - call it 'entries' that has an ArrayList of beans - call them instances of ddlb-entry. ddlb-entry has getters for 'value' and 'label'; entries has a getter for 'entry-list'. (names changed to protect the innocent.)

So I have Java embedded in the page that gets 'entry-list', pulls an iterator off it, and


Like I said, it works but it's ugly, and I've always been told to keep Java off the jsps where possible.

The world is full of examples that use <c:forEach> to iterate over some collection or other, but they're always singly-dimensioned and composed of Objects that have an implict toString() method that makes sense.

Am I missing the Zen of JSTL here, or have I architected the situation wrong to begin with? Referrals to outside resources welcome (i.e. "RTFM" is OK by me but please tell me where to find the manual). I didn't find the answer to this one in the FAQs.

Thanks in advance - Dan Fox
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are exactly on the right track.

Your list of beans with value and label properties is a good way to package up the data for use on a JSP. And yes, Java in a JSP is a big no-no in modern code.

Your page controller should create this list and place it as a scoped variable in request scope. On the JSP, you can use the JSTL's <c:forEach> to iterate over the array, and use the EL to fetch the bean properties.

Give it a try and post your solution here. We'll help you with any issue that arise.
 
Dan Fox
Greenhorn
Posts: 3
Android Firefox Browser Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear -

Thanks for the reply! After cruising your bio etc. I have to say I appreciate the heck out of people at your level who take the time to participate in the newbie forums.

So: I have a the top of the jsp:
<%@page import="com.company.project.entry_bean"%>
<jsp:usebean>
id="entry_list"
class="java.util.ArrayList"
scope="request"
</jsp:usebean>

So far so good. In the body of the jsp:
<select name="entry_selector">
<c:forEach items="entry_list" var="entry">
<option VALUE="${entry.type}"${entry.name}></option>
</c:forEach>
</select>

This yields a stacktrace with this error at the top:
javax.el.PropertyNotFoundException: Property 'type' not found on type java.lang.String

Changing the EL expression in the <option> tag to this:
<option VALUE="${entry.getType()}"${entry.getName()}></option>

yields the following error:
org.apache.jasper.JasperException: /data_entry.jsp(line, char) The function getType() must be used with a prefix when a default namespace is not specified.

So which is it? Am I supposed to use implied magical introspection as in the first example or java object get/set notation as in the second?

More Googling indicates that EL assumes beans ('adheres to the Javabean spec') so that ${bean.var} will yield 'insert string value of whatever's returned from an invocation of bean.getVar()'. So my problem is in getting the page to realize that each "entry" returned from <forEach>ing "entry_list" is an instance of "entry_bean", or at least that it's a bean with bean behavior.

I'm stuck. What I find seems to tell me that the first example should work, but whatever's trying to get the work done seems to think it's a String.

Thanks in advance - Dan Fox
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a lot of syntax errors in your code. It'd also be easier to read if you use code tags. Please click this link ⇒ UseCodeTags ⇐ for more information.

For example, in your <jsp:useBean> tag, the attributes are erroneously placed in the body of the tag rather than on the opening tag.

Secondly, on your <c:forEach> the attribute value for items is the string "entry_list". Hence, the error about string not having any properties. What you really meant was to reference the entry_list scope variable. That would be:
Take it from there...
 
Dan Fox
Greenhorn
Posts: 3
Android Firefox Browser Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear -

Thanks so much! It is working now.

The first error (in the usebean declaration) was simply my copying error; it was correct in the code. PEBKAC, if you know what I mean. The second error (in the forEach syntax) did the trick.

As for the code-tagging, I apologize. That was an (apparently misguided) attempt to condense the length of my post; the code tags do paint a box on the page that takes up quite a bit of room. But if you want 'em, you got 'em.

Again, I appreciate your help and will return for more, no doubt.

-Dan
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Room is not an issue. Readability is!

Glad it's working now!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!