java - How can I create an instance from a own class in FreeMarker (FTL) -
i wish instantiate java class have defined in domain , want use ftl code in way, i'm getting error.
<#assign myclassinstance = "com.domain.myclass"?new())>
is possible? should change it?
myclass
doesn't implements templatemodel
thanks!
there's no built-in function instantiating arbitrary non-templatemodel
classes... maybe there should setting allow ?new
. anyway, can write templatemethodmodelex
that, , can pull of commonly included/imported templates <#assign unrestrictednew = "com.example.unrestrictednewmethodmodel"?new()>
(or put instance data-model or configuration
shared variable) , can <#assign myclassinstance = unrestrictednew("com.domain.myclass")(arg1, arg2, argn)>
in templates. there 2 tricky parts in implementing such templatemethodmodel
. 1 resolving class name class
, recommend env.getnewbuiltinclassresolver().resolve(classname, env, null)
, env
current freemarker.core.environment
object. other calling constructor, have convert parameter values , possibly chose overloaded constructor. recommend calling ow = env.getobjectwrapper()
, see if ow instanceof beanswrapper
(throw exception if isn't), return ((beanswrapper) ow).newinstance(cl, arguments)
.
Comments
Post a Comment