Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Array using JSTL or EL

I'm working on a web application using Java and its frameworks(Spring 3.1.1). And I'm trying to avoid using scriptlets as much as possible, however I can't find a way other than this to define an array:

<%     String[] alphabet = {"A", "B", "C", ... , "Z"};     pageContext.setAttribute("alphabet", alphabet);       %>  

After setting pageContext attribute, I can use it with ${alphabet}. But I want to know, is it possible to use plain JSTL/EL to create an array?

UPDATE: I'm using this array to create links. For example, if user clicks 'S', a list of employees whose first name starts with 'S' comes. So, instead of creating links one by one I'm iterating ${alphabet}.

like image 804
Alpha Carinae Avatar asked Jan 22 '13 08:01

Alpha Carinae


People also ask

What is JSTL El?

A primary feature of JSTL is its support for an expression language (EL). An expression language, in concert with JSTL tags, makes it possible to easily access application data and manipulate it in simple ways without having to use scriptlets or request-time expressions.

What is the difference when using JSTL and JSP?

JSP lets you even define your own tags (you must write the code that actually implement the logic of those tags in Java). JSTL is just a standard tag library provided by Sun (well, now Oracle) to carry out common tasks (such as looping, formatting, etc.).

What is JSTL used for?

JSTL stands for JSP Standard Tag Library. JSTL is the standard tag library that provides tags to control the JSP page behavior. JSTL tags can be used for iteration and control statements, internationalization, SQL etc.

What is JSTL and its advantages?

Advantage of JSTL Fast Development JSTL provides many tags that simplify the JSP. Code Reusability We can use the JSTL tags on various pages. No need to use scriptlet tag It avoids the use of scriptlet tag.


2 Answers

If you're already on EL 3.0 (Tomcat 8+, WildFly 8+, GlassFish 4+, Payara 4+, TomEE 7+, etc), which supports new operations on collection objects, you can use ${[...]} syntax to construct a list, and ${{...}} syntax to construct a set.

<c:set var="alphabet" value="${['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']}" scope="application" /> 

If you're not on EL 3.0 yet, use the ${fn:split()} function trick on a single string which separates the individual characters by a common separator, such as comma.

<c:set var="alphabet" value="${fn:split('A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z', ',')}" scope="application" /> 

I do however agree that you're better off using normal Java code for this. Given that it's apparently static data, just create this listener class:

@WebListener public class ApplicationData implements ServletContextListener {      private static final String[] ALPHABET = { "A", "B", "C", ..., "Z" };      @Override     public void contextInitialized(ServletContextEvent event) {         event.getServletContext().setAttribute("alphabet", ALPHABET);     }      @Override     public void contextDestroyed(ServletContextEvent event) {         // NOOP.     }  } 

It'll transparently auto-register itself on webapp's startup and put the desired data in application scope.

like image 94
BalusC Avatar answered Oct 14 '22 12:10

BalusC


If you want to iterate over tokens in string then simply use forTokens:

<c:set var="alphabet">A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z</c:set>  <c:forTokens items="${alphabet}" delims="," var="letter">     ${letter} </c:forTokens> 
like image 28
tellnobody1 Avatar answered Oct 14 '22 12:10

tellnobody1