views:

14

answers:

1

Is there a jsp/jstl equivalent of this Rails error flash?

    <%- flash.each do |name, msg| -%>
      <%= content_tag :div, msg, :id => "flash_#{name}" %>
    <%- end -%>

I've been looking for a pre-built solution that is as simple as this Rails idiom.

+1  A: 

You can use JSTL c:forEach to iterate over a Map<String, String>. Every iteration gives you a Map.Entry which in turn has getKey() and getValue() methods. Assuming that you've put it in the request scope by key messages, here's an example:

<dl>
    <c:forEach items="${messages}" var="entry">
        <dt>${entry.key}</dt><dd>${entry.value}</dd>
    </c:forEach>
</dl>

By the way, JSP/JSTL is not really comparable to the RoR MVC framework. JSP/JSTL is pretty low-level and offers practically no useful abstractions/facilities out of the box to represent a decent MVC approach. You may want to take a look for JSF 2.0 instead. It's the Java EE provided MVC framework. JSP is just a view technology. JSTL is just a standard flow/function/format taglib. Here's a JSF 2.0 tutorial.

BalusC
Thanks for the JSF link. Someone else told me to look at Sitemesh. Would that be similar to JSF?
peasoup
Sitemesh is a templating (layout/decoration/include) framework. It's not a MVC framework. You don't need it in JSF 2.0 as well. JSF 2.0 itself uses Facelets instead of JSP as view technology which in turn already offers pretty awesome templating possibilities. Check [this answer](http://stackoverflow.com/questions/2095397/what-is-the-difference-between-jsf-servlet-and-jsp/2097732#2097732) to learn more about JSP/Servlet/JSF.
BalusC
Thanks so much!
peasoup
You're welcome.
BalusC