views:

14

answers:

1

How would you port this front-end logic from erb to jsp/jstl?

<%= error_messages_for :foo %>

Is there any Java library that does something similar?

+1  A: 

Maintain them in a Map<String, String> in the business code, put it in the request scope like so:

Map<String, String> messages = new HashMap<String, String>();
request.setAttribute("messages", messages);
// ...
messages.put("foo", "Please enter valid value");

Then you can access it in JSP the usual EL way by ${messages.key} or ${messages['key']}:

<input id="foo" name="foo" value="${fn:escapeXml(param.foo)}">
<label for="foo" class="error">${messages.foo}</label>

EL is builtin in JSP since ages. You don't need to install it. Only the fn:escapeXml (which is mandatory to prevent XSS) is part of JSTL which may need to be installed separately at certain servletcontainers.

BalusC
So the <label for="foo"> won't be seen unless there is an error? What triggers it to be seen?
peasoup
Just ... If there's no content, then nothing is visible to enduser :)
BalusC
Ah, I see. Thanks.
peasoup
You're welcome.
BalusC