aspose file tools*
The moose likes JSP and the fly likes Arrays in JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Arrays in JSP" Watch "Arrays in JSP" New topic
Author

Arrays in JSP

Edward Kenworthy
Ranch Hand

Joined: Oct 05, 2003
Posts: 66
In Java I'd do it like this:

String[] foo ={"string1", "string2","string3"};

System.out.println(foo[index]);

Ie I can trivially map an int to a String. However the closest I can come to this in JSP is to use a JSTL choose: which is seriously clunky. Is there a better way?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61654
    
  67

I'm really not sure what you are asking.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Edward Kenworthy
Ranch Hand

Joined: Oct 05, 2003
Posts: 66
Originally posted by Bear Bibeault:
I'm really not sure what you are asking.


In my database I store status values as integers eg 0 for recorded, 1 for accepted, 2 for rejected.

When I display the status I want to display the text not the number. Using plain old Java I would use something like:


String[] statusText={"recorded", "accepted", "rejected"}

then I'd use:

statusText[status}

to select the correct string to use.

In JSP the only option I can see is to use a <choose> which is basically as clunky as using if..else if...else if...

What I wanted to know is if there was a way in a JSP page (ideally without using JS) that I could setup a String[] and then use that as to generate my output eg ${statusText[item.status]}

Edward
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61654
    
  67

Except for setting up the array, which should occur in your servlet controller before the JSP page is forwarded to, that all sounds pretty straight-forward. What part are you having issues with?
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
you can use a HashMap for this.
If you are using a VO then its better to set the string instead of number.
Edward Kenworthy
Ranch Hand

Joined: Oct 05, 2003
Posts: 66
{QUOTE]Except for setting up the array, which should occur in your servlet controller before the JSP page is forwarded to, that all sounds pretty straight-forward. What part are you having issues with?


Originally posted by Adeel Ansari:
you can use a HashMap for this.
If you are using a VO then its better to set the string instead of number.


Uhm. No. It's not the controller's job to render view items: it's the view's ie the JSP.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61654
    
  67

It's not the controller's job to render view items


Absolutely! And I'm one of the staunchest defenders of this principle. But you are painting it with too broad a brush. Setting up data so that it can be used by the JSP pages in a straight-forward fashion doesn't constitute rendering.

The JSTL and the EL were purposefully designed to work within a limited data world view; that is: bean-patterned classes, arrays and collections (such as Lists and Maps). This design focus was specifically chosen to steer people away from processing on the JSP pages.

Togther, the controller and view layers compose the Presentation Layer. If you were to convert your app to a Swing or command line program, the controller layer would not tag along. It is intrinsically part of the UI, even if it does not directly participate in renderring it.

It is entirely appropriate, in fact expected by the EL, for the controller to set up the scoped variable data for the page in an "EL-friendly" manner. This does not cross the line of "renderring" -- now if your controller starts building up the HTML to display those strings in string buffers, you know you've crossed the line big-time.
[ December 09, 2004: Message edited by: Bear Bibeault ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Arrays in JSP