You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by shashi_struts <sh...@hotmail.com> on 2003/03/06 05:29:00 UTC

Re: how to get a single value from hashtable

Hi Max

Sorry for writing the wrong code.
In my code :
---
 Hashtable hash=new Hashtable();
 hash.put("page 1",new ArrayList());
 hash.put("page 2",new ArrayList());
 hash.put("page 3",new ArrayList());
 the code i am using in java is
 if(hash.containsKey(new String("page 2"))
 ArrayList value=hash.get(new String("page 2"))

i want to this will be implemented through the tag library.
----------------------------------------
| page 1 | questions in a arraylist            |
----------------------------------------
|  page 2 | questions in a arraylist            |
-----------------------------------------
|  page 3 |questions in a arraylist            |
----------------------------------------
this is a key-value pare.

i think now you under stand the question.
i am already reterive this through scriplets and implement, but now i want
this through the tag library.

is any tag provided for this type of functionality.

Regards

Shashi BHushan


----- Original Message -----
From: "Max Cooper" <ma...@maxcooper.com>
To: "Struts Users Mailing List" <st...@jakarta.apache.org>
Sent: Thursday, March 27, 2003 7:49 PM
Subject: Re: how to get a single value from hashtable


> I am not exactly sure what you are asking. Consider posting a minimal but
> complete code (JSP, Java) example to help us understand if the following
> info doesn't help:
>
> Hashtable.elements() returns an Enumeration of the values, not the keys. I
> believe that logic:iterate will just iterate though the Enumeration of
> values.
>
> hash.contains(new String("one")) will return false in your example code,
> because there is no map entry with a value of "one". I think you mean
> hash.containsKey("one"), which is much faster and will return true. Read
the
> API docs:
> http://java.sun.com/j2se/1.4.1/docs/api/java/util/Hashtable.html
>
> Also, the newer HashMap class might be faster than Hashtable, since
HashMap
> is not synchronized.
>
> -Max
>
> ----- Original Message -----
> From: "shashi_struts" <sh...@hotmail.com>
> To: "Struts Users Mailing List" <st...@jakarta.apache.org>
> Sent: Wednesday, March 05, 2003 10:04 PM
> Subject: how to get a single value from hashtable
>
>
> hi
> i am using the struts and jstl in my web application and using the
> logic:iterate for iterating the hashtable,arraylist.
> I faced the problem at this point:-
> i am getting any tag for retrival of only one value from the hashtable
that
> we are getting through java function.
> ex:-
> Hashtable hash=new Hashtable();
> hash.put("one","first");
> hash.put("two","second");
> the code i am using in java is
> if(hash.contains(new String("one"))
> String value=hash.get(new String("one"))
>
> but i am not getting this using struts or jstl
>
> Please help me
> Regards
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: how to get a single value from hashtable

Posted by Max Cooper <ma...@maxcooper.com>.
Shashi,

Ah, very clear now -- excellent description.

This will almost do what you want. It requires that the Hashtable instance
you want to iterate over be available in some scope (probably a request
attribute) under the name "pages":

<table>
  <logic:iterate id="currentPage" name="pages">
  <tr>
    <td><bean:write name="currentPage" property="key" /></td>
    <td><ol>
      <logic:iterate id="question" name="currentPage" property="value">
        <li><bean:write name="question" /></li>
      </logic:iterate>
    </td></ol>
  </tr>
  </logic:iterate>
</table>

There is one major problem with this approach, however. A Hashtable is not
an ordered list. I made a little example app (I'll send that directly to you
in a separate message, Shashi), and it would always output the "page 2" row
first. If you need to guarantee the order in which the rows display, a
Hashtable is not an appropriate structure. You need some kind of List to
guarantee the order. Alternately, you could construct key strings with the
number in them in the right order and use that to get the right value from
the Hashtable, but that is somewhat inelegant, requires more code in a
scriptlet, and it will break if you change the strings. A Hashtable is a
synchronized Map, too, so performance will be somewhat impacted negatively
versus using an unsychronized Map class like HashMap. This will be minor
(though still present) if each new request gets its own Hashtable, but it
could be a lot worse if many requests might share the same Hashtable
instance.

To solve the ordering problem, you could change from a Hashtable to some
kind of List (ArrayList will work well), so the code in the action would
look like this:

      List questions;
      List pages = new ArrayList();
      // page 1
      questions = new ArrayList();
      questions.add("Where did I park my car?");
      questions.add("Why is the sky blue?");
      pages.add(questions);
      // page 2
      questions = new ArrayList();
      questions.add("When do I need to finish this project?");
      questions.add("Do you have any objections to using Struts?");
      pages.add(questions);
      // add the hashtables of pages to the request
      request.setAttribute("pages", pages);

And then use this in the JSP:

<table>
  <logic:iterate id="currentPage" indexId="pageIndex" name="pages">
  <tr>
    <td>page <%=pageIndex.intValue()+1%></td>
    <td><ol>
      <logic:iterate id="question" name="currentPage">
        <li><bean:write name="question" /></li>
      </logic:iterate>
    </td></ol>
  </tr>
  </logic:iterate>
</table>

Even the List implementation has some shortcomings, however. Specifically,
the row headings for the "pages" are now in the JSP rather than your action,
so you lose a lot of flexibility about what you can name them. And you need
a scriptlet to display the page number, since pageIndex will start at 0.

Both the Hashtable and List implementations aren't very O-O, which severely
limits their flexibility. Imagine if you wanted to add a third column with
some additional information from the "page". You can't since it isn't really
an object with properties, but rather just a List of questions. The
questions in the Hashtable and List implementations aren't really "question"
objects, either. That limits them in a similar fashion -- they are just
Strings, so you can't add more properties, like an answer or a URL to
provide a link to more info, etc. Identifying those objects as "pages" and
"questions" may be difficult for your future maintenance programmers since
variable names and comments are all they have to go on and they won't
necessarily be consistent or even well named. To the runtime, you just have
a Hashtable of String keys, where each is mapped to an ArrayList of String
objects. Your maintenance programmers might be thankful if you give it more
structure, where your ActionMapping/Action/JSP share some kind of
ActionForm, with a "pages" property that is a List of Pages, where each Page
has a "questions" property that is a List of Question objects, and each
Question object has a "text" property that is a String that represents the
text of the question. If you are writing an app with many pages that use
these things, the Page and Question beans could be reused.

If you are able to change the data structure, that would be a good idea. I
might do something like this:

// you probably want a better name that relates to what this page represents
in your problem domain, but I don't
// know what the domain is, so I gave the class this simple name...
class StructuredActionForm extends ActionForm {
   private List pages;
   public List getPages() {
      return pages;
   }
   public void setPages(List pages) {
      this.pages = pages;
   }
}

class Page {
   private String name;
   private List questions;
   public Page(String name, List questions) {
      this.name = name;
      this.questions = questions;
   }
   public List getQuestions() {
      return questions;
   }
   public String getName() {
      return name;
   }
}

class Question {
   private String text;
   public Question(String text) {
      this.text = text;
   }
   public String getText() {
      return text;
   }
}

// in your Action, where Struts passes in a StructuredActionForm instance as
the parameter 'form'
// and puts it in request scope for you under the name "structuredForm"
   List questions;
   List pages = new ArrayList();
   // page 1
   questions = new ArrayList();
   questions.add(new Question("Where did I park my car?"));
   questions.add(new Question("Why is the sky blue?"));
   pages.add(new Page("page 1", questions));
   // page 2
   questions = new ArrayList();
   questions.add(new Question("When do I need to finish this project?"));
   questions.add(new Question("Do you have any objections to using
Struts?"));
   pages.add(new Page("page 2", questions));
   // add the pages to the main form
   ((StructuredActionForm)form).setPages(pages);

And in the JSP, you can do this:

<table>
  <logic:iterate id="currentPage" name="structuredForm" property="pages"
scope="request">
  <tr>
    <td><bean:write name="currentPage" property="name" scope="page" /></td>
    <td><ol>
      <logic:iterate id="question" name="currentPage" property="questions"
scope="page">
        <li><bean:write name="question" property="text" scope="page" /></li>
      </logic:iterate>
    </td></ol>
  </tr>
  </logic:iterate>
</table>

The output is the same, but this structure is flexible enough to add
properties to the objects, reuse them elsewhere in the app, and provide some
useful clues as to what they are for your maintenance programmers.

You also don't have to explicitly put anything in the request in your Action
(which you could also acheive with the Hashtable or List implementations
above using the same technique). I like to avoid putting stuff in the
request or session in my Action code for my JSP to find under some secret
name, since it is less clear and more fragile than just making the thing to
find accessible from the main ActionForm for the page. Having a property on
the ActionForm makes it easier to find and allows the compiler to do some
checking for you.

-Max

----- Original Message -----
From: "shashi_struts" <sh...@hotmail.com>
To: "Struts Users Mailing List" <st...@jakarta.apache.org>
Sent: Wednesday, March 05, 2003 8:29 PM
Subject: Re: how to get a single value from hashtable


> Hi Max
>
> Sorry for writing the wrong code.
> In my code :
> ---
>  Hashtable hash=new Hashtable();
>  hash.put("page 1",new ArrayList());
>  hash.put("page 2",new ArrayList());
>  hash.put("page 3",new ArrayList());
>  the code i am using in java is
>  if(hash.containsKey(new String("page 2"))
>  ArrayList value=hash.get(new String("page 2"))
>
> i want to this will be implemented through the tag library.
> ----------------------------------------
> | page 1 | questions in a arraylist            |
> ----------------------------------------
> |  page 2 | questions in a arraylist            |
> -----------------------------------------
> |  page 3 |questions in a arraylist            |
> ----------------------------------------
> this is a key-value pare.
>
> i think now you under stand the question.
> i am already reterive this through scriplets and implement, but now i want
> this through the tag library.
>
> is any tag provided for this type of functionality.
>
> Regards
>
> Shashi BHushan
>
>
> ----- Original Message -----
> From: "Max Cooper" <ma...@maxcooper.com>
> To: "Struts Users Mailing List" <st...@jakarta.apache.org>
> Sent: Thursday, March 27, 2003 7:49 PM
> Subject: Re: how to get a single value from hashtable
>
>
> > I am not exactly sure what you are asking. Consider posting a minimal
but
> > complete code (JSP, Java) example to help us understand if the following
> > info doesn't help:
> >
> > Hashtable.elements() returns an Enumeration of the values, not the keys.
I
> > believe that logic:iterate will just iterate though the Enumeration of
> > values.
> >
> > hash.contains(new String("one")) will return false in your example code,
> > because there is no map entry with a value of "one". I think you mean
> > hash.containsKey("one"), which is much faster and will return true. Read
> the
> > API docs:
> > http://java.sun.com/j2se/1.4.1/docs/api/java/util/Hashtable.html
> >
> > Also, the newer HashMap class might be faster than Hashtable, since
> HashMap
> > is not synchronized.
> >
> > -Max
> >
> > ----- Original Message -----
> > From: "shashi_struts" <sh...@hotmail.com>
> > To: "Struts Users Mailing List" <st...@jakarta.apache.org>
> > Sent: Wednesday, March 05, 2003 10:04 PM
> > Subject: how to get a single value from hashtable
> >
> >
> > hi
> > i am using the struts and jstl in my web application and using the
> > logic:iterate for iterating the hashtable,arraylist.
> > I faced the problem at this point:-
> > i am getting any tag for retrival of only one value from the hashtable
> that
> > we are getting through java function.
> > ex:-
> > Hashtable hash=new Hashtable();
> > hash.put("one","first");
> > hash.put("two","second");
> > the code i am using in java is
> > if(hash.contains(new String("one"))
> > String value=hash.get(new String("one"))
> >
> > but i am not getting this using struts or jstl
> >
> > Please help me
> > Regards
> >
> >
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: struts-user-help@jakarta.apache.org
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
>
>



---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org