Jim Richards wrote:
> As I understand it, is the basic design of struts generally to avoid
> any direct java code in the .jsp files, and do everything through tags?
Tags and beans, yes.
> And although we can so things within <% %> tags, it is not
> preferred?
That's the conventional wisdom; however, IMHO it's sometimes
permissible to use scriplets. Consider this JSP page:
<%
java.util.Enumeration e = request.getParameterNames();
boolean hasParams = false;
while(e.hasMoreElements()) {
String name = (String)e.nextElement();
String[] values = request.getParameterValues(name);
hasParams = true;
for(int i=0; i < values.length; ++i) {
String next = values[i];
if(i == 0) { %>
<b><%= name %>:</b> <%= next %>
<% }
else { %>
, <%= next %>
<% }
} %>
<% }
if(!hasParams) { %>
<i>No parameters with this request</i>
<% } %>
The JSP page listed above prints request parameters, which is useful for
debugging. I refer to JSP pages that are mostly scriptlets with a small
amount of HTML sprinkled throughout as JSP components, for lack of a better
term. In such cases, I believe scriptlets are just fine.
Of course, the functionality in the JSP page listed above could be
implemented as a custom tag. Whether you implement JSP pages or custom tags
is largely a matter of taste. Custom tags are a little more difficult to
develop, but they are easier for page authors to use. JSP components are
easier to develop, but they are a little more difficult for page authors to
use because they must be included.
> Just wondering before I embark on my project, if it's worth including the
> design of any extra tags, or just doing the things I want in <% %> ...
The bottom line is twofold: reuse and division of labor. You can't reuse
scriptlets buried in a JSP page, but you can reuse custom tags or JSP
components. If you're on a large project with software developers and page
authors, ideally you'd like the page authors to use tags implemented by
developers. This helps maintain a division of labor where software developers
and page authors can work in parallel, with few dependencies.
david