java - Acquire method reference in JSF EL -
in jsf 2 have declared function inspect
takes 1 argument of type java.lang.reflect.method
, based on argument performs annotation inspection , returns true
or false
. catch want call function inspect
jsf el able modify ui according return value not able reference of target method pass argument of function, ask how it?
example
package some.pkg; @managedbean( name = "someclass" ) public class someclass { @myannotation public void somemethod( string arg1, integer arg2 ) { /* ... */ } }
jsf function declaration
<function> <function-name>inspect</function-name> <function-class>some.pkg.inspector</function-class> <function-signature>boolean inspect(java.lang.reflect.method)</function-signature> </function>
desired invocation jsf, doesn't work
<h:outputtext value="somemethod annotated @myannotation" rendered="#{inspect(someclass.somemethod)}" />
acceptable this, less comfortable variant
<h:outputtext value="somemethod annotated @myannotation" rendered="#{inspect(some.pkg.someclass.somemethod)}" />
why don't try in server side? know before rendering page if the method annotated in current bean or not, so:
@managedbean( name = "someclass" ) public class someclass { boolean annotated = false; public boolean isannotated(){ return annotated; } @postconstruct public void postconstruct(){ if (inspect(this.getclass().getmethod("somemethod")){ annotated=true; } } }
and in xhtml page:
<h:outputtext value="somemethod annotated @myannotation" rendered="#{someclass.annotated}" />
you can adapt use parameter , calculate on-the-fly:
//notice in case we're using method, not getter public boolean annotated(string methodname){ return inspect(this.getclass().getmethod(methodname); }
calling that:
<h:outputtext value="somemethod annotated @myannotation" rendered="#{someclass.annotated('methodname')}" />
or can use @applicationscoped
managed bean have access every single view:
@managedbean @applicationscoped public class inspectorbean{ public boolean inspectmethod(string classname, string methodname){ return inspect(class.forname(classname).getmethod(methodname)); } }
then can access all views:
<h:outputtext value="somemethod annotated @myannotation" rendered="#{inspectorbean.inspectmethod('completeclassname','methodname')}" />
Comments
Post a Comment