473,385 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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
9 6479
"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
"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
"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
"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
"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
"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
"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
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
(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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: David. E. Goble | last post by:
Hi All; I have a few of these; sigsImages="sigs/finished1.jpg" sigsImages="sigs/foghorn.jpg" lower I have;
2
by: IkBenHet | last post by:
Hello, I am uploading a file using this form in ASP.NET. I have also added a simpel textfield: <form runat="server" enctype="multipart/form-data"> <input type="file" id="oFile" Name="oFile"...
2
by: jain_tj | last post by:
Could anyone please help me with the following problem My xml file is ============== <fig id="F0000001"> <caption>Caption text</caption> <image id="I0000001" image.class="halftone"...
4
by: chaz | last post by:
here is the html : <br> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="<%=LABEL_WIDTH%>" class="formtext"><%= HTEXT("Connection type:")%></td> <td...
4
by: stevewy | last post by:
If I am using srcElement (or "target" for non-IE models) to return various properties of an object I have clicked on, can I access for "label for" value in any way? I'm thinking, for example, of...
1
by: bodilima | last post by:
Dear All, I'm new to this! I wanna insert selected values from form "SaveModel" to form "SaveMake" <input name="makeID" value="<<<<need to insert here(x)>>>>"> & <input name="make"...
0
by: David | last post by:
Hi, I have an asp page which lists records. Each record has an ID appended to it. On the following edit page, I am trying to test against a certain field value, if it is >0 then run the update,...
1
by: gimme_this_gimme_that | last post by:
When an achor tag is defined like this - what might be going on? Why would someone assign the id a value associated with a JavaScript method? Or is something else going on? This comes from a...
10
by: dkyadav80 | last post by:
<html> /// here what shoud be java script for: ->when script run then not display all input text field only display selection field. ->when user select other value for institute only this...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.