{"id":23,"date":"2014-02-06T13:38:55","date_gmt":"2014-02-06T13:38:55","guid":{"rendered":"http:\/\/www.solewing.org\/blog\/?page_id=23"},"modified":"2014-02-06T13:40:59","modified_gmt":"2014-02-06T13:40:59","slug":"jsf-2-x-errors-and-causes","status":"publish","type":"page","link":"http:\/\/www.solewing.org\/blog\/jsf-2-x-errors-and-causes\/","title":{"rendered":"JSF 2.x Errors and Causes"},"content":{"rendered":"<p>When learning JSF, occasionally you&#8217;ll forget some detail and end up with an error in the browser.  This page simply enumerates some errors I&#8217;ve encountered in my journey, and identifies the corresponding causes.<\/p>\n<h1>Can&#8217;t Resolve a Managed Bean<\/h1>\n<pre>\r\n\/address.xhtml @17,38 binding=\"#{addressBean.city}\": Target Unreachable, identifier 'addressBean' resolved to null\r\n<\/pre>\n<p>The important thing to note here is that the identifier that couldn&#8217;t be resolved (&#8220;addressBean&#8221;) is the name of a bean that should be available in the JSF context.  There are a couple of reasons this can happen:<\/p>\n<ol>\n<li>The bean doesn&#8217;t have the correct annotations.<\/li>\n<li>The view doesn&#8217;t reference the correct name for the bean.<\/li>\n<\/ol>\n<h3>Getting the Annotations Right<\/h3>\n<p>Every bean that is referenced in a JSF view must be properly annotated (or configured in <code>faces-config.xml<\/code>).  There are two ways to do this, depending on whether your application runs in a modern Java EE container (e.g. JBoss, Wildfly, Glassfish, WebLogic, WebSphere, etc) or a simple servlet container, like Tomcat.<\/p>\n<h5>Bean annotations using CDI<\/h5>\n<p>Java EE 6 (and later) containers have built-in support for Contexts and Dependency Injection (CDI) services.  If your application is designed to run in a Java EE container, you should prefer the use of CDI annotations for your JSF beans.  The applicable CDI annotation for a JSF bean is <code>@javax.inject.Named<\/code>.<\/p>\n<p>[java]<br \/>\nimport javax.inject.Named;<br \/>\nimport javax.enterprise.context.RequestScoped;<\/p>\n<p>@Named<br \/>\n@RequestScoped<br \/>\npublic class AddressBean {<br \/>\n  private String city;<br \/>\n  private String state<\/p>\n<p>  &#8230;<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>Note that, in addition to the <code>@Named<\/code> annotation, your bean will typically have a scope annotation.  When using CDI, it is important that the scope annotation come from the <code>javax.enterprise.context<\/code> package.<\/p>\n<h5>Bean annotations using JSF<\/h5>\n<p>If your application is designed to run in a simple servlet container, like Tomcat, CDI isn&#8217;t usually available.  In this case, your managed beans will use the <code>@javax.faces.bean.ManagedBean<\/code> specified in the JSF API.<\/p>\n<p>[java]<br \/>\nimport javax.faces.bean.ManagedBean;<br \/>\nimport javax.faces.bean.RequestScoped;<\/p>\n<p>@Named<br \/>\n@RequestScoped<br \/>\npublic class AddressBean {<br \/>\n  private String city;<br \/>\n  private String state<\/p>\n<p>  &#8230;<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>Note that, the scope annotation specified here also comes from the <code>javax.faces.bean<\/code> package.<\/p>\n<h5>Another <code>@ManagedBean<\/code> Annotation<\/h5>\n<p>The Commons Annotation specification (JSR-250) also defines a <code>@javax.annoation.ManagedBean<\/code>.  This annotation <strong>is not supported by the JSF standard<\/strong>, and can cause JSF to fail to resolve a bean reference.<\/p>\n<p>If your bean appears to have a <code>@ManagedBean<\/code> annotation but JSF is complaining that it cannot resolve the bean reference, carefully check your import statements to verify that you are importing <code>javax.faces.bean.ManagedBean<\/code>.<\/p>\n<h3>Getting the Name Right<\/h3>\n<p>The identifier that you use in a JSF view depends on how you&#8217;ve applied the <code>@Named<\/code> or <code>@ManagedBean<\/code>.  JSF resolves bean references by name (not by type).  When troubleshooting a problem in resolving a managed bean, carefully check the identifier specified in the view and make certain that it corresponds to the annotations applied to the bean.<\/p>\n<h5>Default Bean Naming<\/h5>\n<p>When a name is not explicitly specified in the bean annotation, the unqualified class name is used as the bean name. The beans in the preceding examples used <code>@Named<\/code> or <code>@ManagedBean<\/code> without providing an explicit name attribute.  The bean class name used in each example was <code>AddressBean<\/code>, therefore the bean identifier to use in a view would be <code>addressBean<\/code>.<\/p>\n<p>Note that the first letter of the class name was converted to lower case in the default bean name.  If the class name starts with two or more upper case letters, this conversion <em>will not<\/em> occur.  For example, if your bean class name was <code>URLBean<\/code>, the bean name would default to <code>URLBean<\/code>, not <code>uRLBean<\/code> or <code>urlBean<\/code>.<\/p>\n<h5>Explicit Bean Naming with CDI<\/h5>\n<p>When using CDI, you can explicitly specify a bean name by providing the name as an annotation attribute:<br \/>\n[java]<br \/>\nimport javax.inject.Named;<br \/>\nimport javax.enterprise.context.RequestScoped;<\/p>\n<p>@Named(&#8220;address&#8221;)<br \/>\npublic class AddressBean {<br \/>\n  &#8230;<br \/>\n}<br \/>\n[\/java]<\/p>\n<h5>Explicit Bean Naming with JSF<\/h5>\n<p>When using JSF annotations, you can explicitly specify a bean name using the <code>name<\/code> attribute:<br \/>\n[java]<br \/>\nimport javax.faces.bean.ManagedBean<br \/>\nimport javax.faces.bean.RequestScoped;<\/p>\n<p>@ManagedBean(name = &#8220;address&#8221;)<br \/>\npublic class AddressBean {<br \/>\n  &#8230;<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>When troubleshooting a problem in resolving a managed bean, carefully check the name specified in the view and make certain that it corresponds to the annotations applied to the bean.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When learning JSF, occasionally you&#8217;ll forget some detail and end up with an error in the browser. This page simply enumerates some errors I&#8217;ve encountered in my journey, and identifies the corresponding causes. Can&#8217;t Resolve a Managed Bean \/address.xhtml @17,38 binding=&#8221;#{addressBean.city}&#8221;: Target Unreachable, identifier &#8216;addressBean&#8217; resolved to null The important thing to note here is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-23","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/www.solewing.org\/blog\/wp-json\/wp\/v2\/pages\/23","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.solewing.org\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/www.solewing.org\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/www.solewing.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.solewing.org\/blog\/wp-json\/wp\/v2\/comments?post=23"}],"version-history":[{"count":2,"href":"http:\/\/www.solewing.org\/blog\/wp-json\/wp\/v2\/pages\/23\/revisions"}],"predecessor-version":[{"id":27,"href":"http:\/\/www.solewing.org\/blog\/wp-json\/wp\/v2\/pages\/23\/revisions\/27"}],"wp:attachment":[{"href":"http:\/\/www.solewing.org\/blog\/wp-json\/wp\/v2\/media?parent=23"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}