{"id":1373,"date":"2011-06-02T15:30:06","date_gmt":"2011-06-02T15:30:06","guid":{"rendered":"http:\/\/salientsoft.co.uk\/?p=1373"},"modified":"2011-06-02T15:30:06","modified_gmt":"2011-06-02T15:30:06","slug":"jsf-2objects-as-selectonemenu-itemscustom-convertercdi-issues","status":"publish","type":"post","link":"https:\/\/salientsoft.co.uk\/?p=1373","title":{"rendered":"JSF 2&ndash;Objects as selectOneMenu items&ndash;Custom Converter\/CDI issues"},"content":{"rendered":"<p>With JSF 2.0, it is now possible to use any class as an item in a selectOneMenu combo and related components. This is described in&#160; \u201cCore JavaserverFaces\u201d p157ff. However, the book does not mention that you also need a custom JSF converter to convert between object and string.<\/p>\n<p>If you then implement a custom converter as in the book on p279, e.g. with:-<\/p>\n<p>&lt;h:selectOneMenu styleClass=&quot;ss-tsw-select&quot; id=&quot;cmbTheme&quot;   <br \/>&#160;&#160;&#160;&#160;&#160; value=&quot;#{themeSwitcherBean.theme}&quot; disabled=&quot;#{themeSwitcherBean.selectDisabled}&quot;    <br \/>&#160;&#160;&#160;&#160;&#160; converter=&quot;uk.co.salientsoft.thememanager.ThemeConverter&quot;    <br \/>&#160;&#160;&#160;&#160;&#160; immediate=&quot;true&quot; onchange=&quot;submit()&quot;&gt;<\/p>\n<p>CDI fails to inject any references used in the converter class, as JSF 2 does not allow CDI in converters, as detailed in<strong> <\/strong><a href=\"http:\/\/stackoverflow.com\/questions\/3630403\/how-do-i-access-ejb-bean-when-inside-a-custom-converter\/3631043#3631043\"><strong>this Stack Overflow Post<\/strong><\/a><strong>.<\/strong><\/p>\n<p>It does work however if you use an EL reference in the converter attribute as mentioned in <a href=\"http:\/\/stackoverflow.com\/questions\/2019495\/inject-a-ejb-into-a-jsf-converter-with-jee6\/2171152#2171152\"><strong>this post here<\/strong><\/a>.<\/p>\n<p>Note that the post indicates that it may require it to be an application scoped bean. In my case, it worked fine as both a request scoped and application scoped bean with no trouble. I also removed the<em> @FacesConverter(&quot;uk.co.salientsoft.thememanager.Theme&quot;)<\/em> annotation as this was no longer required due to the EL reference. The post indicates that this is perceived to be more untidy due to the use of EL and a managed bean, but I cannot see an issue with this. I cannot see why converters cannot be application scoped CDI beans, it all fits in fine. Here is the code fragment above, modified to use an EL reference instead:-<\/p>\n<p>&lt;h:selectOneMenu styleClass=&quot;ss-tsw-select&quot; id=&quot;cmbTheme&quot;   <br \/>&#160;&#160;&#160;&#160;&#160; value=&quot;#{themeSwitcherBean.theme}&quot; disabled=&quot;#{themeSwitcherBean.selectDisabled}&quot;    <br \/>&#160;&#160;&#160;&#160;&#160; converter=&quot;#{themeConverter}&quot;    <br \/>&#160;&#160;&#160;&#160;&#160; immediate=&quot;true&quot; onchange=&quot;submit()&quot;&gt;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>With JSF 2.0, it is now possible to use any class as an item in a selectOneMenu combo and related components. This is described in&#160; \u201cCore JavaserverFaces\u201d p157ff. However, the book does not mention that you also need a custom JSF converter to convert between object and string. If you then implement a custom converter [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[22],"tags":[110,184,16,15],"_links":{"self":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1373"}],"collection":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1373"}],"version-history":[{"count":0,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1373\/revisions"}],"wp:attachment":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1373"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1373"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1373"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}