Skip to content

Instantly share code, notes, and snippets.

@rponte
Forked from guilhermesilveira/ApplicationHelper.java
Last active December 23, 2015 06:59
Show Gist options
  • Save rponte/6598027 to your computer and use it in GitHub Desktop.
Save rponte/6598027 to your computer and use it in GitHub Desktop.
@Component
public class ApplicationHelper implements ViewHelper {
private final PrettyTimeFormatter formatter;
public ApplicationHelper(PrettyTimeFormatter formatter) {
this.formatter = formatter;
}
public String format(AbstractInstant instant) {
return formatter.format(instant);
}
@Override
public String getName() {
return "app";
}
}
@Intercepts(before=ForwardToDefaultViewInterceptor.class)
class HelpersInterceptor implements Interceptor{
private final List<ViewHelper> helpers;
private final Result result;
public HelpersInterceptor(List<ViewHelper> helpers, Result result) {
this.helpers = helpers;
this.result = result;
}
@Override
public boolean accepts(ResourceMethod method) {
return true;
}
@Override
public void intercept(InterceptorStack stack, ResourceMethod method,
Object args) throws InterceptionException {
for (ViewHelper helper : helpers) {
// result.include(helper.getClass().getSimpleName(), helper);
result.include(helper.getName(), helper); // <3
}
stack.next(method, args);
}
}
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@attribute name="time" required="true" type="org.joda.time.base.AbstractInstant"%>
<%
if (time != null) {
org.joda.time.base.AbstractInstant time = (org.joda.time.base.AbstractInstant) jspContext.getAttribute("time");
br.com.caelum.gnarus.utilMovePlease.PrettyTimeFormatter formatter = (br.com.caelum.gnarus.utilMovePlease.PrettyTimeFormatter) request.getAttribute("timeFormatter");
out.write(formatter.format(time));
}
%>
${app.format(usuario.createdAt)}
interface ViewHelper {
public String getName();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment