2

I've been a Java developer for 14 years, and most of my projects are where we had to get things done, rather than be able to plan and design. Things are quiet at the moment, and for the sake of the company (and my CV), I want to do things right.

When writing JSPs, I usually declare a few Java classes at the top in <% %>, and then insert bits of Java code to iterate through ArrayLists/HashMaps, or when I need to SELECT a particular in a tag.

I've used JSTL in the past, but it drove me mad trying to manipulate the construct to do what I wanted to do; writing the equivalent in Java was trivial.

What should I be doing? Is JSTL the "correct" way of doing things? Have things moved on far more than that, that I should be using another way to write JSPs?

I currently use Struts (yep, not even Struts 2), and would like to move to Spring/Struts 2 if I could. But there's no point me driving a Ferrari if I can't even ride my pushbike correctly.

Thoughts, suggestions, criticisms are all very welcome.

cs94njw
  • 535
  • 5
  • 12
  • It's cool how honest and humble you are. – Daniel Sep 05 '11 at 15:52
  • 1
    Related: http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files I know nothing about Struts, all I know is that it's a legacy MVC framework. Does it really not offer any ways to hook actions on GET/POST requests? It would make more sense to me that you're using Struts (or any other MVC framework) instead of *scriptlets*. – BalusC Sep 05 '11 at 16:16
  • Thanks BalusC. I'm not doing anything silly like business logic inside my JSP, but your last three examples are relevant examples of what I do often. I'll start to cautiously move some pages to using JSTL. – cs94njw Sep 05 '11 at 16:34

1 Answers1

1

Using scriptlets is a pretty antiquated practice. Iterating over a collection is as simple as <c:forEach ...>. The type of the collection doesn't matter as long as it's iterable.

Without knowing more about what you're actually doing in the scriptlets, or why, it's tough to help much beyond finger-wagging.

Things like deciding if an option is currently selected are almost always handled in custom tags, not manually--normally you don't have to think about it at all, let alone decide how to handle it yourself. (That's true of Struts 1 tags as well; you shouldn't have to do that manually.)

digitaljoel
  • 26,265
  • 15
  • 89
  • 115
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • 1
    Agree. I think the general principle here is to prefer tag libraries over scriptlets when working with JSPs. Whether it's JSTL, or custom tags, or . – Liggy Sep 06 '11 at 15:06