• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Arrays in JSP

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm really not sure what you are asking.
 
Edward Kenworthy
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
What are you saying? I thought you said that Santa gave you that. And this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic