Last active
April 24, 2018 17:31
-
-
Save guilhermesilveira/6593510 to your computer and use it in GitHub Desktop.
View helper example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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); | |
} | |
stack.next(method, args); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ 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)); | |
} | |
%> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
${app.format(usuario.createdAt)} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface ViewHelper { | |
public String getName(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment