By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,798 Members | 1,343 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,798 IT Pros & Developers. It's quick & easy.

[Help] Struts/JSP -- easy way associate an "ID" to a "Value"?

P: n/a
I'm using "ID" and "Value" in the generic sense here...

Let's say one page I had a <html:select> with a collection like this:

<html:options collection="items" property="key" labelProperty="value"/>

In this case "key" is what I mean by "ID", and "value" is what I mean
by "Value" -- the "Value" is shown to the user, but the "ID" is what
I plan to keep, to be saved to a database (as a reference in a table
to a common table as a part of normalization). Oh, and "items" is
just a vector of a simple object with key, value & extraValue fields,
and extraValue isn't always used.

Now, what I'm running into is that they want a confirmation screen
(let's call that "Page X") -- print out the values the user has input
(on what we'll call "Page 1" and "Page 2" since we're combining two
screens for confirmation (our business model requires this as two
separate steps) and confirm before processing (i.e. saving to the
database and started business flow on it) all the data.

Now, I have easy access to the "ID" on the Page2ActionForm, but I
want to show the "Value" instead. Obviously, I can read through the
original "items" object, compare the "key" values and return the
"value" value (ugh, sorry) when found, but that seems tedious.

Is there an easy/better way to do this? Some way I can easily do
this with JSP tags? It seems <logic:iterate> is going output
everything (as I don't think I can put a <bean:write> of the
Page2ActionForm property "ID" as a value in a <logic:equal> inside
the <logic:iterate> as, from experience, it seems struts doesn't
like nesting tags :-), and I don't see any other logic or bean tag
examples that do what I'm looking for.

On the off chance the above makes sense to anyone, does anyone have
a quick & easy way, or I should I get around to writing my find
routines?
Jul 17 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
"Russ Perry Jr" <sl******@rcn.com> wrote in message
news:c6**************************@posting.google.c om...
I'm using "ID" and "Value" in the generic sense here...

Let's say one page I had a <html:select> with a collection like this:

<html:options collection="items" property="key" labelProperty="value"/>

In this case "key" is what I mean by "ID", and "value" is what I mean
by "Value" -- the "Value" is shown to the user, but the "ID" is what
I plan to keep, to be saved to a database (as a reference in a table
to a common table as a part of normalization). Oh, and "items" is
just a vector of a simple object with key, value & extraValue fields,
and extraValue isn't always used.

Now, what I'm running into is that they want a confirmation screen
(let's call that "Page X") -- print out the values the user has input
(on what we'll call "Page 1" and "Page 2" since we're combining two
screens for confirmation (our business model requires this as two
separate steps) and confirm before processing (i.e. saving to the
database and started business flow on it) all the data.

Now, I have easy access to the "ID" on the Page2ActionForm, but I
want to show the "Value" instead. Obviously, I can read through the
original "items" object, compare the "key" values and return the
"value" value (ugh, sorry) when found, but that seems tedious.

Is there an easy/better way to do this? Some way I can easily do
this with JSP tags? It seems <logic:iterate> is going output
everything (as I don't think I can put a <bean:write> of the
Page2ActionForm property "ID" as a value in a <logic:equal> inside
the <logic:iterate> as, from experience, it seems struts doesn't
like nesting tags :-), and I don't see any other logic or bean tag
examples that do what I'm looking for.

On the off chance the above makes sense to anyone, does anyone have
a quick & easy way, or I should I get around to writing my find
routines?


Store selected ID/value pairs in a Map in the session for display and then
use after confirmation? You'd be better off posting to
comp.lang.java.programmer. This group is not supposed to exist.

Jul 17 '05 #2

P: n/a
"Ryan Stewart" <zz********@gSPAMo.com> wrote:
"Russ Perry Jr" <sl******@rcn.com> wrote:
On the off chance the above [deleted here; see previous post] makes
sense to anyone, does anyone have a quick & easy way, or I should I
get around to writing my find routines?
Store selected ID/value pairs in a Map in the session for display and
then use after confirmation?
Can I easily search by the key to get the value with a Map? Sometimes
our objects need that third field, but I could always make parallel
objects in those cases...
You'd be better off posting to comp.lang.java.programmer. This group
is not supposed to exist.


Really? I didn't know that... How'd that happen?

But I was actually intending to put it in the .help group, and have
since copied my original post there.

Thanks for the reply though, I'll look into that.
Jul 17 '05 #3

P: n/a
"Russ Perry Jr" <sl******@rcn.com> wrote in message
news:c6*************************@posting.google.co m...
"Ryan Stewart" <zz********@gSPAMo.com> wrote:
"Russ Perry Jr" <sl******@rcn.com> wrote:
On the off chance the above [deleted here; see previous post] makes
sense to anyone, does anyone have a quick & easy way, or I should I
get around to writing my find routines?
Store selected ID/value pairs in a Map in the session for display and
then use after confirmation?


Can I easily search by the key to get the value with a Map? Sometimes
our objects need that third field, but I could always make parallel
objects in those cases...

You *use* the key to get the value with a Map. Maybe you should spend a
little more time learning the core APIs before leaping into Struts.
You'd be better off posting to comp.lang.java.programmer. This group
is not supposed to exist.


Really? I didn't know that... How'd that happen?

These groups are technically regulated, though pretty loosely obviously. The
comp.lang.java.* groups (some of them) are "legal" groups. This and some
others (I think .misc and .developer at least) are not. The only real
problem with this is that not all news servers carry the "invalid" groups.
Jul 17 '05 #4

P: n/a
"Ryan Stewart" <zz********@gSPAMo.com> wrote:
"Russ Perry Jr" <sl******@rcn.com> wrote:
"Ryan Stewart" <zz********@gSPAMo.com> wrote:
"Russ Perry Jr" <sl******@rcn.com> wrote:
> On the off chance the above [deleted here; see previous post] makes
> sense to anyone, does anyone have a quick & easy way, or I should I
> get around to writing my find routines? Store selected ID/value pairs in a Map in the session for display and
then use after confirmation?
Can I easily search by the key to get the value with a Map? Sometimes
our objects need that third field, but I could always make parallel
objects in those cases...
You *use* the key to get the value with a Map. Maybe you should spend a
little more time learning the core APIs before leaping into Struts.


Actually, I know what a Map does, but I obviously whiffed on remembering
it when I was replying -- I was thinking that we'd have a Vector of Maps
instead of a Vector of OurObject, and that's obviously wrong... Ugh.
Sorry about that.

However, the architect here decided we'd use Vectors instead of other
collections because it's already synchronized, and Maps aren't. But,
looking into things a bit, I see something that might work to easily
synchronize a Map (Collections.synchronizedMap()), so I'll be looking
into that next.

Thanks again for the response.
Jul 17 '05 #5

P: n/a
"Ryan Stewart" <zz********@gSPAMo.com> wrote:
"Russ Perry Jr" <sl******@rcn.com> wrote:
"Ryan Stewart" <zz********@gSPAMo.com> wrote:
"Russ Perry Jr" <sl******@rcn.com> wrote:
> On the off chance the above [deleted here; see previous post] makes
> sense to anyone, does anyone have a quick & easy way, or I should I
> get around to writing my find routines? Store selected ID/value pairs in a Map in the session for display and
then use after confirmation?


I think I've found more "trouble" with the Map... I can make a Map with
the ID/value pairs no sweat. Now, how do I reference them on the JSP?
Looking at http://struts.apache.org/faqs/indexedprops.html it appears that
I'd have to nest bean:write statements (the inner one to get the ID I'm
trying to look up in the Map), which doesn't work last I knew. Something
like this:

<bean:write name="bean" property="stringMapped(NESTED BEAN:WRITE HERE)"/>

The other issue is that I'm not sure how to get the common info Map from
ServletContext while in my ActionForm (as shown on that same page) to have
the Map to do the get() on... I might be missing something there, but...

I guess I'm going to have to do some useBean sleight of hand and some
embedded Java to do this?
Jul 17 '05 #6

P: n/a
"Russ Perry Jr" <sl******@rcn.com> wrote in message
news:c6**************************@posting.google.c om...
I think I've found more "trouble" with the Map... I can make a Map with
the ID/value pairs no sweat. Now, how do I reference them on the JSP? Assume you have something beanish like this:
public class SomeBean {
Map values = new HashMap();

Map getValues() {
return values;
}
}

You put some key/value pairs in the Map, then store the bean in the page,
request, session, or application scope (like in a Struts Action) like so:
request.setAttribute("myBean", myBean);

In the JSP, using JSTL, do this:
<c:out value="${myBean.values['foo']}"/>
or
<c:out value="${myBean.values[3]}"/>
or
<c:out value="${myBean.values[myOtherBean.someProperty]}"/>

Those three examples respectively will output the String representation of
the value in the Map with a key equal to:
-the String object "foo"
-the Integer object with an int value of 3 (Integer only I think, not Short,
Long, etc., but I may be wrong)
-whatever object "myOtherBean.getSomeProperty()" returns

The JSTL download comes with example and documentation WARs like Struts
does. It has some basic information. You'll find more here:
http://java.sun.com/products/jsp/jstl/

You can get the JSTL spec there, which is referenced often from the JSTL
implementation docs.
Looking at http://struts.apache.org/faqs/indexedprops.html it appears that
I'd have to nest bean:write statements (the inner one to get the ID I'm
trying to look up in the Map), which doesn't work last I knew. Something
like this:

<bean:write name="bean" property="stringMapped(NESTED BEAN:WRITE HERE)"/>
According to Struts documentation, you should use JSTL over Struts tags
whenever possible. See above :)
The other issue is that I'm not sure how to get the common info Map from
ServletContext while in my ActionForm (as shown on that same page) to have
the Map to do the get() on... I might be missing something there, but...

I guess I'm going to have to do some useBean sleight of hand and some
embedded Java to do this?

Why would you need anything from a ServletContext in an ActionForm?
ActionForms are strictly for modeling HTML forms and getting the information
from the user to your Action. In an Action, you have access to (most likely)
an HttpServletRequest object named "request". To get a ServletContext from
there:
request.getSession().getServletContext()
Jul 17 '05 #7

P: n/a
"Ryan Stewart" <zz********@gSPAMo.com> wrote:
"Russ Perry Jr" <sl******@rcn.com> wrote:
I think I've found more "trouble" with the Map... I can make a Map with
the ID/value pairs no sweat. Now, how do I reference them on the JSP?
Assume you have something beanish like this:
public class SomeBean {
Map values = new HashMap(); [...]

Actually, not quite... Right now we just make the Map in an init servlet
and throw it into session scope.
In the JSP, using JSTL, do this: [...] According to Struts documentation, you should use JSTL over Struts tags
whenever possible. See above :)
Ah... Right now, we're not using JSTL. It was hard enough trying to
convince people around here that we should be using JSP tags instead
of embedded Java. So, I might look into it, but I probably won't be
able to get permission to use it in our app, at least not version 1.0.
The other issue is that I'm not sure how to get the common info Map from
ServletContext while in my ActionForm (as shown on that same page) to have
the Map to do the get() on...

Why would you need anything from a ServletContext in an ActionForm?
Turns out I don't. It's just the way I was looking at it at first; the
most direct route was to put a getValueField() method in the ActionForm
to pull the info out of the Map, which I needed to retrieve from session
scope, which would have required the ServletContext.
ActionForms are strictly for modeling HTML forms and getting the information
from the user to your Action.
Just an aside, I'm new to Struts (obviously), so it's hard for me to tell
when I'm being clever, when I'm reinventing the wheel, and when I'm doing
something "bad". I'm not in a position where I can justify reading a lot
at work, nor do I have much time to do so at home either.
In an Action, you have access to (most likely) an HttpServletRequest
object named "request". To get a ServletContext from there:
request.getSession().getServletContext()


Yeah, that part's easy. We're doing that kind of thing all over, no
problems.

What I ended up doing, at least for the time being, is to get() the info
from the Map in my Action, but also setting up a simple bean with it to
use on the JSP. It works, though it might not be ideal.

Thing is, at the moment I have two different common objects -- a Vector
for the initial JSP, and the Map for the confirmation JSP. I've seen
references that you can't use a Map with a <html:options> tag, but other
places say it can, so I'll probably try that soon, as I'd like to minimize
the amount of monkey work I'm doing with the two objects and extra bean,
etc...

Thanks again for your reply.
Jul 17 '05 #8

P: n/a
Putting this kind of data in session scope is a bad idea. The id->value
mapping shouldn't be changing across the lifetime of the app (or else you're
going to end up with data integrity problems), so why keep it in session as
opposed to application (servlet context) scope?

Better yet, you should have a DAO that can keep these in a cache so that, in
your action, you ask for the map and then set it as an attribute of the
request. What does this get you? Well, your DAO is responsible for managing
the cache and your app doesn't have to worry about where the data is stored
(in the DB or in memory). Putting it on the request makes it immediately
available to your your view controller, your JSP, and any tiles without them
having to know anything about the DAO. This layers your app nicely.

"Russ Perry Jr" <sl******@rcn.com> wrote in message
news:c6**************************@posting.google.c om...
"Ryan Stewart" <zz********@gSPAMo.com> wrote:
"Russ Perry Jr" <sl******@rcn.com> wrote:
I think I've found more "trouble" with the Map... I can make a Map with the ID/value pairs no sweat. Now, how do I reference them on the JSP?
Assume you have something beanish like this:
public class SomeBean {
Map values = new HashMap();

[...]

Actually, not quite... Right now we just make the Map in an init servlet
and throw it into session scope.
In the JSP, using JSTL, do this:

[...]
According to Struts documentation, you should use JSTL over Struts tags
whenever possible. See above :)


Ah... Right now, we're not using JSTL. It was hard enough trying to
convince people around here that we should be using JSP tags instead
of embedded Java. So, I might look into it, but I probably won't be
able to get permission to use it in our app, at least not version 1.0.
The other issue is that I'm not sure how to get the common info Map from ServletContext while in my ActionForm (as shown on that same page) to have the Map to do the get() on...
Why would you need anything from a ServletContext in an ActionForm?


Turns out I don't. It's just the way I was looking at it at first; the
most direct route was to put a getValueField() method in the ActionForm
to pull the info out of the Map, which I needed to retrieve from session
scope, which would have required the ServletContext.
ActionForms are strictly for modeling HTML forms and getting the

information from the user to your Action.


Just an aside, I'm new to Struts (obviously), so it's hard for me to tell
when I'm being clever, when I'm reinventing the wheel, and when I'm doing
something "bad". I'm not in a position where I can justify reading a lot
at work, nor do I have much time to do so at home either.
In an Action, you have access to (most likely) an HttpServletRequest
object named "request". To get a ServletContext from there:
request.getSession().getServletContext()


Yeah, that part's easy. We're doing that kind of thing all over, no
problems.

What I ended up doing, at least for the time being, is to get() the info
from the Map in my Action, but also setting up a simple bean with it to
use on the JSP. It works, though it might not be ideal.

Thing is, at the moment I have two different common objects -- a Vector
for the initial JSP, and the Map for the confirmation JSP. I've seen
references that you can't use a Map with a <html:options> tag, but other
places say it can, so I'll probably try that soon, as I'd like to minimize
the amount of monkey work I'm doing with the two objects and extra bean,
etc...

Thanks again for your reply.

Jul 17 '05 #9

P: n/a
(Argh, I got an error trying to post this; my apologies if it shows up twice)

"Java Architect" <da**@dynamicresolve.com> wrote:
Putting this kind of data in session scope is a bad idea. The id->value
mapping shouldn't be changing across the lifetime of the app (or else you're
going to end up with data integrity problems), so why keep it in session as
opposed to application (servlet context) scope?
I misspoke -- we're actually putting them in application scope, not session
scope as I said. That's why we made the init servlet in the first place.
Better yet, you should have a DAO that can keep these in a cache so that, in
your action, you ask for the map and then set it as an attribute of the
request. What does this get you? Well, your DAO is responsible for managing
the cache and your app doesn't have to worry about where the data is stored
(in the DB or in memory). Putting it on the request makes it immediately
available to your your view controller, your JSP, and any tiles without them
having to know anything about the DAO. This layers your app nicely.
We're not doing any DAO, at least not as I saw in my struts class. When we've
released our app and are refining it in the future, perhaps the architect here
will approve it, but for now we're using either table-wrapping objects, or JIT
SQL queries (via stored procs at least).

Thanks for the reply.
"Russ Perry Jr" <sl******@rcn.com> wrote:
"Ryan Stewart" <zz********@gSPAMo.com> wrote:
"Russ Perry Jr" <sl******@rcn.com> wrote:
> I think I've found more "trouble" with the Map... I can make a Map with> the ID/value pairs no sweat. Now, how do I reference them on the JSP? Assume you have something beanish like this:
public class SomeBean {
Map values = new HashMap();

[...]

Actually, not quite... Right now we just make the Map in an init servlet
and throw it into session scope.
In the JSP, using JSTL, do this: [...] According to Struts documentation, you should use JSTL over Struts tags
whenever possible. See above :)


Ah... Right now, we're not using JSTL. It was hard enough trying to
convince people around here that we should be using JSP tags instead
of embedded Java. So, I might look into it, but I probably won't be
able to get permission to use it in our app, at least not version 1.0.
> The other issue is that I'm not sure how to get the common info Map from> ServletContext while in my ActionForm (as shown on that same page) to have> the Map to do the get() on... Why would you need anything from a ServletContext in an ActionForm?


Turns out I don't. It's just the way I was looking at it at first; the
most direct route was to put a getValueField() method in the ActionForm
to pull the info out of the Map, which I needed to retrieve from session
scope, which would have required the ServletContext.
ActionForms are strictly for modeling HTML forms and getting the information from the user to your Action.


Just an aside, I'm new to Struts (obviously), so it's hard for me to tell
when I'm being clever, when I'm reinventing the wheel, and when I'm doing
something "bad". I'm not in a position where I can justify reading a lot
at work, nor do I have much time to do so at home either.
In an Action, you have access to (most likely) an HttpServletRequest
object named "request". To get a ServletContext from there:
request.getSession().getServletContext()


Yeah, that part's easy. We're doing that kind of thing all over, no
problems.

What I ended up doing, at least for the time being, is to get() the info
from the Map in my Action, but also setting up a simple bean with it to
use on the JSP. It works, though it might not be ideal.

Thing is, at the moment I have two different common objects -- a Vector
for the initial JSP, and the Map for the confirmation JSP. I've seen
references that you can't use a Map with a <html:options> tag, but other
places say it can, so I'll probably try that soon, as I'd like to minimize
the amount of monkey work I'm doing with the two objects and extra bean,
etc...

Thanks again for your reply.

Jul 17 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.