This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Populating a dropdown box from data in the database

 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
Scott Selikoff
author
Saloon Keeper
Posts: 4020
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4020
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the snippet where you are creating the drop down?
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Show us the SkylanderBean -- it's likely that the property accessor for allSkylanders is not named correctly (or isn't public?).
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent!!! The page stopped exploding! :) :) :)

But my dropdown is still tiny and empty. :(
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the problem lies deeper!

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

/off-to-bed-for-bear
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Log4J is outside the scope of this class. :D

Night Bear/Scott. Thanks for your help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic