Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top