ejb - How to pass value from managed bean to custom jsf component? -
this question has answer here:
i have created custom jsf component following code:
@facescomponent("mainmenu") public class menucomponent extends uicomponentbase { private list<menuitem> items; private string title; public menucomponent() { } public string gettitle() { return title; } public void settitle(string title) { this.title = title; } public list<menuitem> getitems() { return items; } public void setitems(list<menuitem> items) { this.items = items; } public string getfamily() { return ui.family; } }
then added renderer component:
@facesrenderer(componentfamily = ui.family, renderertype = "mainmenurenderer") public class menucomponentrenderer extends renderer { @override public void encodebegin(facescontext context, uicomponent component) throws ioexception { menucomponent menucomponent = (menucomponent) component; responsewriter responsewriter = context.getresponsewriter(); responsewriter.startelement("ul", menucomponent); responsewriter.writeattribute("class", "sidebar-menu", ""); // заголовок responsewriter.startelement("li", menucomponent); responsewriter.writeattribute("class", "header", ""); responsewriter.write(menucomponent.gettitle()); } @override public void encodeend(facescontext context, uicomponent component) throws ioexception { menucomponent menucomponent = (menucomponent) component; responsewriter responsewriter = context.getresponsewriter(); responsewriter.endelement("ul"); // title responsewriter.endelement("li"); // menu items (menuitem menuitem : menucomponent.getitems()) { responsewriter.startelement("li", menucomponent); } } }
then registered component in taglib.xml:
<tag> <tag-name>mainmenu</tag-name> <component> <component-type>mainmenu</component-type> <renderer-type>mainmenurenderer</renderer-type> </component> <attribute> <name>items</name> <required>true</required> </attribute> <attribute> <name>title</name> <required>true</required> </attribute> </tag>
in jsf page used in following way:
<mds:mainmenu items="${sidebar.menuitems}" title="Главное меню"</mds:mainmenu>
tag related sidebar managed bean, bean simple possible:
@managedbean(name = "sidebar", eager = true) @requestscoped public class sidebar { @inject private menuservice menuservice; private list<menuitem> menuitems; public sidebar() { system.out.println("hello, world!"); } @postconstruct public void init(){ setmenuitems(menuservice.getmainmenu()); } public list<menuitem> getmenuitems() { return menuitems; } public void setmenuitems(list<menuitem> menuitems) { this.menuitems = menuitems; } }
but! have value of "title" attribute in renderer, not "items"! how pass "items" attibute value component? when debug, there no call "sidebar" bean. wrong in code?
object value = menucomponent.getvalueexpression("items").getvalue(context.getelcontext());
Comments
Post a Comment