2

I have been considering a range of options to move away form using scriptlets in my jsps.

The main cause of "messiness" tends to be the problems with printing out ArrayList and other Collections objects, because some presentation related markup gets mixed up with some Java.

What would you recommend as the best solution for dealing with this type of situation. I am looking at Wicket (very briefly) but can't work out if this will help to clean up this situation.

Ankur
  • 50,282
  • 110
  • 242
  • 312
  • 2
    Related: [How to avoid Java code in JSP files](http://stackoverflow.com/questions/3177733/howto-avoid-java-code-in-jsp-files). – BalusC Sep 12 '10 at 15:07
  • 1
    The only alternative to JSTL is a MVC framework which offers taglibs to control flow in view side like JSF, Struts, Spring-MVC, Wicket, etc. It's only another learning curve on top of JSP/JSTL. – BalusC Sep 12 '10 at 15:16
  • or you could ditch JSP altogether and use something like Velocity – matt b Sep 12 '10 at 15:19

4 Answers4

5

If you're using plain JSP, without any other Web framework on top of it, I suggest you take a look a the JSTL. It gives you a lot of tags to manipulate your objects.

One tag that might interest you in your case is the <c:forEach /> tag. It allows you to use for-each loops inside your JSP pages.

For more informations, here are some links:

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
Vivien Barousse
  • 20,555
  • 2
  • 63
  • 64
  • Thanks, I knew about JSTL but had read some posts etc. that suggested people weren't using them. Since it's so well supported this might be the place to start, and then if I need something more I can look at things like Wicket in more detail. – Ankur Sep 12 '10 at 15:58
3

JSTL has:

<c:forEach items="${list}" var="item">
   ${item}
</c:forEach>

You should just put the list as a request attribute named list.

Take a look at the other JSTL tags in order to get rid of scriptlets.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
1

I believe pretty much every higher-level web API has something to help you along those lines.

For Wicket, I found Repeaters:

Repeaters are components that can render their body markup multiple times. These components are useful for creating output that is traditionally created via loops.

Using a ListView, for example, you can do this:

Code:

List list = Arrays.asList(new String[] { "a", "b", "c" });
ListView listview = new ListView("listview", list) {
    protected void populateItem(ListItem item) {
        item.add(new Label("label", item.getModel()));
    }
};

Markup:

<span wicket:id="listview">
   this label is: <span wicket:id="label">label</span><br/>
</span>

Will result in the following rendered to the browser:

<span wicket:id="listview">
   this label is: <span wicket:id="label">a</span><br/>
</span><span wicket:id="listview">
   this label is: <span wicket:id="label">b</span><br/>
</span><span wicket:id="listview">
   this label is: <span wicket:id="label">c</span><br/>
</span>

There's more advanced examples on that page as well.

For, say, JSF, there's:

  • <ui:repeat>, which is similar to JSTL's forEach that other posters have pointed out.
  • <ui:dataTable>, which similar to Wicket's ListView

Hope that helps!

The Alchemist
  • 3,397
  • 21
  • 22
0

If your markup and logic get entangled, you best separate them using a MVC-Pattern (Model-View-Controller).

See here: http://en.wikipedia.org/wiki/Model–View–Controller#Java:_Java_Platform.2C_Enterprise_Edition_.28Java_EE.29 ; or here: http://www.oracle.com/technetwork/java/javaee/overview/index.html

FK82
  • 4,907
  • 4
  • 29
  • 42
  • I wouldn't say my business logic is getting entangled. That is all safely abstracted away. But the display logic tends to be a combination of Java and HTML. – Ankur Sep 12 '10 at 15:35
  • Alright, I misunderstood your question then. An MVC pattern would however do away with the somewhat messy juxtaposition of HTML and JSP/JSLT tags. You would use `Servlet`s to print HTML content using the `Request#getWriter` method. It basically achieves the same thing but will be more modular and thus easier to write and debug. – FK82 Sep 12 '10 at 18:14