Question

I have the following scriptlet in my JSP:

<% for (int i=0; i<emailSettings.qualified_apn.length; i++) { %>
    var g ='<%= emailSettings.qualified_apn[i] %>'
    //some code
<% } %>

I moved the variable emailSettings into a bean... so now, how do I change the loop?

Was it helpful?

Solution

You should be using JSTL to browse the list instead of using scriptlets, which are considered a very bad practice... All the bussiness logic should be encapsulated into classes, and the JSP should only contain presentation logic...

You should move all your //some code to Java classes, and once you have collected into classes all the values you want to show in your page, you can use JSTL like this:

<c:forEach var="item" items="${emailSettings.qualified_apn}">
    <p>Item foo: ${item.foo}</p>
    <p>Item bar: ${item.bar}</p>
    ...
</c:forEach>

Remember you have to import the JSTL core library into your JSP with:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

Also note that, as commented by @JBNizet, you'll need a getter for the qualified_apn field in your EmailSettings class (you should never use public fields!). Also consider respecting the Java naming conventions: no underscores in variables and methods (i.e., qualifiedApn).

I suggest you to start reading through the Oracle's official JSTL documentation. Furthermore, you can find many tutorials about JSTL on the web just googling "JSTL tutorial", such as on tutorialspoint.com.

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