tags:

views:

65

answers:

4

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.

+5  A: 

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:

Vivien Barousse
+1 - No one should ever write a JSP without JSTL.
duffymo
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
+3  A: 

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
+1  A: 

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
Thanks this looks great.
Ankur
A: 

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
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
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