This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I have a select box (actually several of them) as follows <select name="beanFieldname"> <option selected value="<%=Bean.getField()%>"> <%(Bean.getDesc())%> </option> <option value="1">Label 1</option> <option value="2">Label 2</option> <option value="3">Label 3</option> </select>
As you can see above, when retrieving the selected value(Bean.getField), from the database, I am using a java function(Bean.getDesc) to fetch the corresponding label. Is there a way by which once you have fetched the value from the database, the label can be fetched directly (without a separate function?)
No. You have two different values that are to be placed in two different locations on the page. You will need to make two calls.
The Struts framework takes some of the pain out of doing this. They let you use a LabelValueBean. This is really a collection of beans. Each bean has a label and a value. You tell the framework which collection to use and it prints out all the options for you.
The squeaky wheel gets the grease. Well, that or replaced...
Joined: May 26, 2005
Um. When I say two different locations, I mean, even though they are visually close to each other to you and me, there is template text in the middle of the two calls:
Is there some particular reason you wanted to do this in one call?
[ June 09, 2005: Message edited by: Rusty Enisin ] [ June 09, 2005: Message edited by: Rusty Enisin ]
Joined: Jun 07, 2005
The only reason I want to do this in one call is that I have so many of these dropdowns that I need a function which maps the label with the value for each of these. So, I was just making sure I am not missing out on some simple shortcut. Thanks a lot for your input. By the way, we are using JSP 1.1, so Struts is eons away....
Yeah. Ben is right. That is another option. You could use JSTL to iterate over a collection of beans. Struts is not as far away as you might think. JSP 1.1 supports taglibs. You could use struts logic tags even without using all of struts. You are right in that you will not be able to use most of the HTML tags without struts.
While we are on the subject of tags, if you do not want to use struts you could right your own taglib that accepts the collection of beans and outputs what you want.
I thought of suggesting you put the <option> code in the bean. But there are many developers, myself included, that do not like to put HTML strings in Java code. Some get very uptight about this. I fear that by suggesting this I might get hung, burned at the stake, and left out as jerky to the vulchers.
However, if you were to do this I would suggest only adding one more method to your bean. Add a getOptionTag() or something similar to your bean. The bean then would look something like this:
If your bean is a collection, life will get easier for you. You could then iterate over all the little beans it contained. It would call the getOptionTag() method on each one. It would add those all together and output it to the page (see example below).
The desire to keep HTML strings out of Java was the driving force behind JSP. Putting HTML code in java gets messy. Case in point, the method above.
So here is the bottom line. If you can, use JSTL (JSP 1.1 supports this) or write a JSP include scriptlet that does all this for you. If you are using scriptlets (this is another sore point in the java world) then you might consider using that. If you are opposed to scriptlets, try looking at JSTL. JSTL would look something like this:
Using a scriptlet would look like this:
Struts has been out for some time. I don't think it requires JSP 1.2. I wrote applications a couple of years back with struts. At that time I was using JSP 1.1.