This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Arrays in JSP

 
Edward Kenworthy
Ranch Hand
Posts: 66
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64182
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm really not sure what you are asking.
 
Edward Kenworthy
Ranch Hand
Posts: 66
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64182
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2874
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
{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
Pie
Posts: 64182
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic