Hi there,
I want to call a specific java method from a static class and I was wondering if this can be done from a freemarker template.
I just don’t want to pass the object to all my templates because this method should be call on the website main template or layout. So the basic solution to get the java object in all my controller and send them to all templates using .render(…) is not an acceptable solution.
Thanks for your help
public class Ninja extends NinjaDefault {
@Inject
TemplateEngineFreemarker tpl;
@Override
public void onFrameworkStart() {
BeansWrapper wrapper = BeansWrapper.getDefaultInstance();
TemplateHashModel staticModels = wrapper.getStaticModels();
TemplateHashModel tplHashModelStringUtils = null;
try {
tplHashModelStringUtils = (TemplateHashModel) staticModels.get("com.serphacker.javafixutil.StringUtils");
} catch (TemplateModelException e) {
e.printStackTrace();
System.exit(1);
}
tpl.getConfiguration().setSharedVariable("StringUtils", tplHashModelStringUtils);
}
}Now i get another problem:/
Calling a static method work. But my Static Method should be able to call some database data and return them to the template.
So I write some :
@Inject
UserDao userDao;
But the injection doesn’t work because when we call staticModels.get("tools.Test") it just reference it in order to call the method and do not instantiate it with all the injection stuff.
So does someone know if there is a way to get all the injection stuff working outside of a controller ? (in a static method). How to get my DAO working in a static method ? Or maybe I am doing these in a wrong way ?
I am completely lost here because in Play framework it was so easy to make this ex : @MyStatic.staticMethod() and that’s it… staticMethod could have transactions etc and still work.
I completely agree with you Raphael until I found that I have this use case :
I need to show all the user last actions on the website on the left menu. The left menu is part of the template and this left menu is showed in almost all my website.
In order to get this “last actions” I have to call the database. So as you can see the use case is very simple.
So the question now is : how to get those last action without having to retrive them and render them in every single controller method of my website (big refactoring).
So the cleanest solution I found is to let the template manage this.
How would you Raphael solve this use case ?
To view this discussion on the web visit https://groups.google.com/d/msgid/ninja-framework/3d4cff5c-aabe-4d32-95a5-a986edfaf00d%40googlegroups.com.
@Inject UserDao userDao; @Inject
TemplateEngineFreemarker tpl;
@Override
public Result filter(FilterChain chain, Context context) {
if (context.getSession() != null && context.getSession().get(USERID) != null) {
long userid = Long.parseLong(context.getSession().get(USERID));
try {
tpl.getConfiguration().setSharedVariable("userActions", userDao.getLastsUserAction(userid, 5));
} catch (TemplateModelException e) {
e.printStackTrace();
}
}
return chain.next(context);
}java.lang.IllegalArgumentException: You already want to render a Renderable class. Adding more items to render is not supported. at ninja.Result.assertObjectNoRenderableOrThrowException(Result.java:753) at ninja.Result.render(Result.java:270) at ninja.Result.render(Result.java:331) at filters.LoggedInFilter.filter(LoggedInFilter.java:79) at ninja.FilterChainImpl.next(FilterChainImpl.java:35) at ninja.NinjaDefault.onRouteRequest(NinjaDefault.java:102) at conf.Ninja.onRouteRequest(Ninja.java:45) at ninja.servlet.NinjaServletDispatcher.service(NinjaServletDispatcher.java:86) at com.google.inject.servlet.ServletDefinition.doServiceImpl(ServletDefinition.java:287) at com.google.inject.servlet.ServletDefinition.doService(ServletDefinition.java:277) at com.google.inject.servlet.ServletDefinition.service(ServletDefinition.java:182) at com.google.inject.servlet.ManagedServletPipeline.service(ManagedServletPipeline.java:91) at com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:85) at com.google.inject.servlet.ManagedFilterPipeline.dispatch(ManagedFilterPipeline.java:119) at com.google.inject.servlet.GuiceFilter$1.call(GuiceFilter.java:133) at com.google.inject.servlet.GuiceFilter$1.call(GuiceFilter.java:130) at com.google.inject.servlet.GuiceFilter$Context.call(GuiceFilter.java:203) at com.google.inject.servlet.GuiceFilter.doFilter(GuiceFilter.java:130) at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652) at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:585) at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1127) at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515) at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1061) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) at org.eclipse.jetty.server.Server.handle(Server.java:497) at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:310) at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:257) at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:540) at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635) at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555) at java.lang.Thread.run(Thread.java:744)