java - EL expression for class -
i don't understand how represent class
in el expressions.
i have el function takes class parameter (namely enum class) return possible enum values.
i want invoke el expression. e.g. ${mytld:enumer(com.example.enums.myenum)}
however:
- the above syntax passes null argument
- using
myenum.class
throws exception such expression cannot evaluated
how express class in el without possibly passing string representation?
the below works
<function> <description> returns list of enum values given enum class </description> <name>enumer</name> <function-class>com.example.functions</function-class> <function-signature>list enumer(java.lang.string)</function-signature> </function> ${tld:enumer('com.example.myenum')}
what like
<function-signature>list enumer(java.lang.class)</function-signature>
you can't represent class
in plain el. can @ pass through if bean property ${mytld:enumer(bean.someenumclass)}
returning class<? extends enum>
, or ${mytld:enumer(bean.someenum['class'])}
someenum
actual enum
, that's it. passing around class name string
best bet.
if you're on el 3.0 (available on servlet 3.1 containers tomcat 8, wildfly 8, etc), alternative custom function import right away in jsp below using new el 3.0 importhandler
api:
${pagecontext.elcontext.importhandler.importclass('com.example.enums.myenum')}
the enum available ${myenum}
.
<c:foreach items="${myenum.values()}" var="myenumvalue"> ${myenumvalue}<br/> </c:foreach>
alternatively, import ${myenum}
globally via servlet context listener below:
@weblistener public class config implements servletcontextlistener { @override public void contextinitialized(servletcontextevent event) { jspfactory.getdefaultfactory().getjspapplicationcontext(event.getservletcontext()).addelcontextlistener(new elcontextlistener() { @override public void contextcreated(elcontextevent event) { event.getelcontext().getimporthandler().importclass(myenum.class.getname()); } }); } // ... }
Comments
Post a Comment