aspose file tools*
The moose likes JSP and the fly likes Populating a dropdown box from data in the database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Populating a dropdown box from data in the database" Watch "Populating a dropdown box from data in the database" New topic
Author

Populating a dropdown box from data in the database

Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1732
    
  12

I have a toy program for school.

I'm trying to generate a dropdown box on a page used to update an object. I would like to select the object in the top from the list of "all objects", then use the data in the object to populate the editable fields down bottom. The bottom area is a form which posts an update to the object.

I am having a hard time getting the data from the service layer to the jsp. I tried populating a static list in my domain object, but the tomcat log showed that it was calling the method recursively and blowing out my data connections. So I moved the population of the list to the controller and called it from the init method in the servlet. Probably not ideal, also doesn't work -- I'm getting "property not found on object" Jasper exceptions.

I'm sure there's a pattern for this, but I have tried searching google and I only come up with folks who populate after the page has loaded. I clearly need the list populated as the page is loading.

Are there pointers on this somewhere? Am I missing something obvious?


When you do things right, people won't be sure you've done anything at all.
Scott Selikoff
author
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3716
    
    5

Are you using JavaScript on change event? You can set up a call back so that when the drop down choice is selected, an event automatically fires that's loads the data from the data layer and populates your fields as needed. You often need a separate call back servlet to load the data from.


My Blog: Down Home Country Coding with Scott Selikoff
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1732
    
  12

The problem is before that.

I can't even get the data in the dropdown to show up. The list is null and I get a weird blank dropdown with nothing in it.

I wrote a fancy JavaScript function to get called when onChange occurs, but I can't change the selection because there's nothing to select.
Scott Selikoff
author
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3716
    
    5

Can you post the snippet where you are creating the drop down?
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1732
    
  12

As you can see, this program is for my nephew to keep track of his Skylanders Adventure characters. :)




And here's the error:

exception

org.apache.jasper.JasperException: org.apache.jasper.el.JspPropertyNotFoundException: /update.jsp(29,4) '${characterBean.allSkylanders}' Property 'allSkylanders' not found on type model.domain.SkylanderBean
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:549)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)


root cause

org.apache.jasper.el.JspPropertyNotFoundException: /update.jsp(29,4) '${characterBean.allSkylanders}' Property 'allSkylanders' not found on type model.domain.SkylanderBean
org.apache.jasper.el.JspValueExpression.getValue(JspValueExpression.java:109)
org.apache.jsp.update_jsp._jspx_meth_c_005fforEach_005f0(update_jsp.java:141)
org.apache.jsp.update_jsp._jspService(update_jsp.java:103)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1732
    
  12

When I remove the JS function and the form, I get this:



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

13: <body>
14:
15: <select name="skylander" >
16: <c:forEach items="${characterBean.allSkylanders}" var="character">
17: <option value="${character}"><c:out value="${character.name}" /></option>
18: </c:forEach>
19: </select>


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)


root cause

org.apache.jasper.el.JspPropertyNotFoundException: /update.jsp(16,4) '${characterBean.allSkylanders}' Property 'allSkylanders' not found on type model.domain.SkylanderBean
org.apache.jasper.el.JspValueExpression.getValue(JspValueExpression.java:109)
org.apache.jsp.update_jsp._jspx_meth_c_005fforEach_005f0(update_jsp.java:119)
org.apache.jsp.update_jsp._jspService(update_jsp.java:90)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Show us the SkylanderBean -- it's likely that the property accessor for allSkylanders is not named correctly (or isn't public?).


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1732
    
  12

Here's the bean....


I populate it with a service that uses Hibernate. Other pieces that use hibernate/this bean work and I can see them from the view correctly.

here's the query:

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

You need an accessor for the allSkylanders property. Making the variable public is not allowed by the bean standard and the EL won't see it.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1732
    
  12

Scott told me to try it without the accessor.

I have the same error with or without a public getter.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Add the accessor. It will not work without it. Make sure that is is named getAllSkylanders and that it is public and not static.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1732
    
  12

Excellent!!! The page stopped exploding! :) :) :)

But my dropdown is still tiny and empty. :(
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Most likely cause is that the list is empty. You can log it to see what it contains, or even use the empty operator on the page as a quick test:
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1732
    
  12

I have some system out statements inside my hibernate service and it doesn't seem to be getting there.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

So the problem lies deeper!

P.S. Highly recommend Log4J in place of sys out.

/off-to-bed-for-bear
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1732
    
  12

Log4J is outside the scope of this class. :D

Night Bear/Scott. Thanks for your help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Populating a dropdown box from data in the database