• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Search form, html:option, MySql?! :o(

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there
I�m freakin� out here with a problem, maybe someone can help?!!
I�m trying to make a web-application with Struts in which it grabs and inserts some stuff from/into my MySQL database.
The problem is I wanna use a search form to filter what�s to be shown from a certain sql table (for example I have a huge list of colors and textures but I want listed only the textures that have the color �black�).
I have my java beans and forms and actions all ready that list the whole table, and my search form already works if I enter by hand (using select like below)
<html:select property="color" size="1">
<html ption value="black">black</html ption>
<html ption value="blue">blue</html ption>
<html ption value="red">red</html ption>
<html ption value="green">green</html ption>
</html:select>
Now I�m trying to figure out a way that the option values (upon the loading of the page) show/list all the color values from the database!
Any ideas on how I could make that work?!
I would really REALLY appreciate it! Goin' nuts here!
Thanks!
Chris
[ October 22, 2003: Message edited by: Chris Perth ]
 
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Try:
-------
<html:select property="priority">
<html ptions collection="priorities" property="value" labelProperty="key" />
</html:select>
---------
priority is a property of the formbean
priorities is an request/session attribute - java.util.Map
Steps:
1.An ActionForm initializes the attribute priorities.
Map map = new HashMap();
map.put("red", "red34234");
request.setAttribute("priorities", map);
2. The ActionForm then forwards the user to the JSP page. If the ForwardAction is a redirect you need to set the attribute in the session.
Does it make sense?
-Yoo-Jin.
 
Chris Perth
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi �Yoo-Jin,
Thanks for answering! I�m wiggin� out here, tried so many things already!
I never used this �priority� attribute from java.util.Map, I wonder if it�ll work�
Just so you know what exactly I�m trying to do (in case you come up with any other suggestions!):
There�s this one link that forwards to this one Search page.
Before I�d made that the html:select would show me the list of colors which I had entered by hand (written separately in a .properties text file), so the Search page used to open without doing anything, just loading the names of the colors from the text list as options for the select�
What I�m trying is that:
AS SOON AS I�m forwarded to the Search page through the link, then the option names and values are gotten from the color table in the Database!
So if I insert or remove some color in the future then I don�t have to keep changing the text file, since the Search page would always have the actual list of colors I have and it�d show listed on the select for the person to choose, and in my case the color �black�, then the person can enter the texture and click on the search button and the list with all the stuff with color �black� would show, know what I mean?
I�ll go read up on the �priority� thingy and keep trying...
Thanks
Chris
 
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch Chris (aka "Kreatur").
Your display name doesn't quite fit the famous JavaRanch naming policy. Could you please take a minute to change your display name to show 2 names, preferably your real name. (It's all about maintaining our professional image - don't let the one-eyed moose fool you!).
Thanks and hope you'll be visiting the ranch often,
Pauline
 
Chris Perth
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, didn't realize that (that's my default nickname) :roll:
Thanks 4 the warning (any ideas on my problem?)
Chris
 
Chris Perth
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems like it's gotta be possible that when my search page's loaded
an action'd grab the data from the mysql table and fill it into my select
options... I keep trying but I don't know HOW!
I've managed to list it as text but how could I pack it into the options
upon loading the page??!
No ideas? Anybody??!!

[ October 22, 2003: Message edited by: Chris ]
 
Chris Perth
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nobody???!!!
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chris, check out this tutorial. It has examples for the options tag that you might find useful. Also, please change your publicly displayed name to comply with the Naming Policy. Thanks.
 
Chris Perth
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Junilu!
I'm going through these tutorials maybe it'll really help! (hopefully!)
Chris
 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chris,
I'm doing something similar and the way I did was to create a generic LabelValueBean object with fields named "label" and "value". I do a query to pull the necessary info out of the database, create new instances of LableValueBean setting the properties from the data, and place them in an ArrayList. I don't think you can use a Map as was suggested earlier, Map doesn't implement the Collection interface, so I don't believe it will work. I never tried it, so I could be wrong. In any event Arraylist is a collection, so you can use it like so in your JSP:

So I define a bean called "optionsList" of type ArrayList from a session attribute called "list". The options tag then finds the "optionsList" collection, loops through it grabbing each element(in my case each will be an instance of LabelValueBean), then looking in each element for properties called "label" and "value", then builds the options based on those values.
HTH,
E
[ October 22, 2003: Message edited by: Eric Fletcher ]
 
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Similar jstl code. Hope this will help
<c:set var="deglist" value="${applicationScope.profileCache.professionDegreeListMap}"/>
<c:forEach var="entry" items="${deglist}">
<c:if test='${entry.key == prof}'>
<select name="degree" class="text10">
<option value="">---------- Select One ----------</option>
<c:forEach var="degree" items="${entry.value}">
<c:set var="intldeg" value="${degree.isIntl}"/>
<c:set var="degid" value="${degree.degreeID}"/>
<c:set var="name" value="${degree.description}"/>
<c:choose>
<c:when test="${cntry == 'us'}">
<c:if test="${intldeg != 1}">
<option value="<c ut value="${degid}"/>" <c:if test='${degid == deg}'> selected </c:if> ><c ut value="${name}"/></option>
</c:if>
</c:when>
<c therwise>
<c:if test="${intldeg == 1}">
<option value="<c ut value="${degid}"/>" <c:if test='${degid == deg}'> selected </c:if> ><c ut value="${name}"/></option>
</c:if>
</c therwise>
</c:choose>
</c:forEach>
</select>
</c:if>
</c:forEach>
 
Chris Perth
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much for helping Eric & desi dude!
I just got here so I'll look into both ideas and see if it works!
Suspense...
Exciting though, dying to see this form working!
Chris
 
Chris Perth
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Eric!
I think your idea might really work!!!
So far I haven't been able to integrate it to my code though,
I mean, I think I made the changes like you suggested and it compiles
& builds ok but when I load it the page with the changes shows blank
("done"), then I check the code on my browser and it shows nothing (??!).
Is it too much to ask (if what you're working on is indeed the same of
what I'm trying to do--which seems like it is!) if you could please post or mail
me with more instructions or a bit more of the code?!
I'm probably (as usual) doing something silly wrong (sometimes takes a
whole long & frustrating day to find a mistake like a simple mistype or
symbol in wrong place!)
Thank you so much! Wishing you luck with your project,
Chris
 
Eric Fletcher
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chris,
The code I posted is pretty much all there is to it on the JSP side. If the browser is white-screening on you, then you are most likely dealing with some sort of runtime exception being thrown, which could be a number of things, i.e. a JspException from a tag, or possibly a NullPointerException somewhere. Not sure what container you are using, but can you check the server log and see if it's giving you a stack trace? That will probably give you a good place to start.
Post again if you still have problems.
Cheers,
E
 
Chris Perth
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Eric,
maybe I'm mixing all my .javas together. I think I sort of understand how to
put my jsps together and enter the paths and infos in the struts-config but I
seem to always have trouble with the beans, and no matter how many
examples I've been looking into none seem to match closely enough to what
I've been trying to do so that I can realize what or where I've been going
wrong!
Could you please tell me how many .javas you use and which one is being
called by the .jsp to show the select options (the collection)? Like, which one
does what? I tried writing a separate bean just to grab the option but it didn't
work, then I added an ArrayList and collection part in my old .java, it's all so
frustrating that after a few whole days (and nights!) merged with the problem I
feel like I'm getting more and more confused by the second!
Thanks
Chris
 
Eric Fletcher
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chris,
Don't worry, this stuff can be very confusing, to say the least!
Let's step back a minute, take it one step at a time. Here is what I am doing:
1-I have a utility class that handles my database interaction. My action classes call methods on instances of that class to get data out of the database. One of the methods of that class runs a query to select the values and labels I want for my option list in my JSP, puts each pair of labels and values into a LabelValueBean, adds that bean to an ArrayList, and returns the ArrayList to my action class.
2-My action class takes the returned ArrayList, places it in the session as an attribute named "list".
3-When the action class forwards it to my JSP, it uses the code I posted earlier to format and render the option list.
So, the ArrayList holding the label-value pairs is, in the eyes of the Struts <bean:define> tag, a "bean" of type java.util.ArrayList. So we create a page-scope variable named "optionsList" when we call:

OK, so we have a variable called "optionsList" on our page. We know that we can create a select box with an option list by using the <html:select> tag with a nested <html:options> tag, so that's what we do by doing the following:

The <html:options> tag treats each element in the collection("optionsList") as a new "bean" with properties defined as "value" and "label". The tag then looks in the bean for methods getValue() and getLabel(). If it finds them, it retrieves their return values and places them in the option element as the value and the label, respectively.
And that's about it. So you probably need to check a few things:
1-Check your server log if you can, or try to run a console on your app server and see if you can get a stack trace when you run the app.
2-Are there getter methods for label and value properties in your LabelValueBean class?
3-Is your ArrayList class in the proper scope, i.e. in the session if that is where you are trying to find it?
4-Is there an uncaught exception somewhere upstream, say in your action class?
Let me know how it goes.
E
 
Chris Perth
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I'm getting there... Thank you so much Eric!
I'll post later with the results, if it worked...
Have a nice week!
Chris
 
Yoo-Jin Lee
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Sorry it's been a while but work has been hectic.
1. Chris 'priority' is not an attribute of java.util.Map. It's just a name I gave to a request attribute.
2. Eric - you can use a java.util.Map. The attribute collection for html ptions tag is a little misleading and the API for it doesn't really explain it fully.
The following from the logic:iterate tag applies to the html ptions tag - I believe.

-----------------------
--------LoopActionForm.java--------

------------------------
----------loop.jsp-----------

The url you would navigate to is http://localhost:8080/struts/loop.do.
I'm using Tomcat 4.1.24 on port 8080 and Struts 1.1.
- Hope this helps.
-Yoo-Jin
[ October 27, 2003: Message edited by: Yoo-Jin Lee ]
 
Chris Perth
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!!!
Yoo-Jin:
Thank you so much for the help!!! I ended up using the HashMap
like you said and my select option collection works!!!

Eric:
I did manage to get the stuff from the database and put it into
the ArrayList, but even though I tried like crazy to find out where
was the mistake, I gave up in the end, sick and tired of that
blank page "Done" I told you about... (I hate it that no matter if
I monitor Catalina on my tomcat it never shows where/what my
mistake/problem is!!!)
One more question, actually two! (maybe I'll write a new post)...
Maybe to you Yoo-Jin who has experience with HashMaps...
-Is there a direct way to sort the keys/labels alphabetically?!
I'd really love it that the options on my select are organized!
I mean, the select from the database is already distinct and
ordered by that particular row, but still, no matter in what order
I put the stuff into the HashMap, it always lists the content
all scrambled up into my select!!!
(I saw somewhere that I could use a sort method but that it won't
work directly with the Map but rather with the key through an
ArrayList, is that right???!)
-I tried as experiment doing another select (once one worked I
got carried away!) so this time I wanted the colors and the
textures to be taken from the database... but it didn't work!
Maybe it lays on my sql ResultSet (maybe only one can be done
at a time?!), any ideas how I can do this? (I'll need two
HashMaps and two pageContext.setAttributes)
I'd appreciate any further help!!! You guys are great!
Chris
 
Chris Perth
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I already figured it out...
Thanks anyways!
Chris
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic