Question

I am developing a java webapp. In the view it would be nice if I could create a list/sequence that only the view knows about.

But JSTL cannot (as I understand it) cannot create list, but can only be given a list. But often we need a list dynamically created that only the jsp knows about (which the control and model should know nothing about). Here is a simple mock example, in which I use a scriplet to create a list that would later be looped by JSTL.

Is the following bad practice? How else would one go about this, as often we need lists that are only part of the view and not the model or control. I am used to Freemarker, and regularly create sequences that are only for the view, how else to do this in JSTL without using a scriplet, any ideas? I know scriplets should be avoided, but what do people think about this situation (as things like this are included with FreeMarker?

<%  // this code could go at the top or in an imported header
    ArrayList<String> colors = new ArrayList<String>();
    colors.add("red");
    colors.add("green");
    colors.add("blue");
    colors.add("orange");
    request.setAttribute("colors", colors);
%>

<c:forEach items="${colors}" var="color">   
    color= ${color} </br>
</c:forEach>
Was it helpful?

Solution

JSTL doesn't have any tool to declare a list, but you can use fn:split to create an array from a String:

<c:set var="colors" value="${fn:split('red,green,blue,orange', ',')}"  />

However, in my point of view, it is not a very good practice, as it is not very readable, and I think that the maintenance would be easier if the list was defined in the model.

OTHER TIPS

Scriplets indeed should be avoided. This problem can be solved by involving a little bit MVC structure/idea You can use a servlet to create the list and then save the list to the request object. In your servlet:

ArrayList<String> colors = new ArrayList<String>();
colors.add("red");
colors.add("green");
colors.add("blue");
colors.add("orange");

request.setAttribute("colors", colors);
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("yourPage.jsp");
dispatcher.forward(request, response);
return;

in "yourpage.jsp", you can use EL and jstl to display the list like you did. please note that you still need to do some research on how to create servlet, but it is pretty easy to learn.

I appreciate the answers I recieved, but I am going to go a head and answer my own question (though I am open to more feedback).

It appears that JSTL does provide a basic way of creating lists through strings. The following will create a comma separated list (of sorts) and store it in the page scope.

<c:set var="colors" scope="page" value="red,green,blue,orange"/>

This list can then be looped over using the following

<c:forEach var="color" items="${colors}">
    <LI>${color} <br/>
</c:forEach>

This does not require use of the split function, and is handled automatically in the loop. While ',' is the default delim. I think this is pretty clean and will handle most cases for the UI.

I do not like the idea of building UI related lists int servlet, as that seems to violate MVC more than putting in a simple scriplet, but the above seems to work, and the split function should work well also if needed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top