1

While upgrading the JSF version of an old web application from MyFaces 1.1 to MyFaces 2.2.12, I am trying to replace the <managed-bean> entries in my faces-config.xml file with @ManagedBean annotations directly in the bean classes. I am using Migrating from JSF 1.2 to JSF 2.0 as a general guide for the migration.

For example, I am replacing something like

<managed-bean>
    <managed-bean-name>MyBean</managed-bean-name>
    <managed-bean-class>some.package.MyBean</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
</managed-bean>

with

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class MyBean {

According to Are there going to be two instances for a bean if I write @managed bean annotation and define same in faces-config.xml, annotations are overwritten by corresponding entries in the faces-config.xml, so I deleted the <managed-bean> element in my faces-config.xml.

Since the project consists of several maven modules which are packed as jars individually before being deployed as a combined war file, I also tried to follow the advice from How does JSF find beans annotated with @ManagedBean? and added another META-INF folder containing a faces-config.xml to the submodule containing the bean, at the following location (respecting the accepted answer in How to reference JSF managed beans which are provided in a JAR file?):

MainProject
|  SubModule
|     |src
|       | main
|          | resources
|             | META-INF
|                | faces-config.xml

with the following content:

<faces-config
        xmlns="http://xmlns.jcp.org/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
        version="2.2">
</faces-config>

Sadly, I still get the following error when trying to open the page that uses this specific bean:

javax.el.PropertyNotFoundException: Target Unreachable, identifier 'MyBean' resolved to null

Since I am using JSF for bean management, I followed the JSF part of the instructions in Identifying and solving javax.el.PropertyNotFoundException: Target Unreachable but even after verifying the individual points mentioned there, I still get the same error.

I am using Tomcat 7 as servlet container, which - according to http://tomcat.apache.org/whichversion.html - supports servlet specification up to version 3.0, which, in turn, should suffice for JSF 2.2, which - if I understand correctly - requires at least servlet spec 2.5, according to http://myfaces.apache.org/core22/.

I have already searched quite a lot for the cause of the problem (as mentioned, I tried several of the SO articles mentioned above), but still can't solve the problem. I would be very grateful for any help!

scholt
  • 147
  • 2
  • 16
  • 1
    Small suggestion: With JSF 2.3, the `@ManagedBean` annotation is deprecated in favour of using the CDI `@Named`. Wouldn't now be the right time to switch to that directly instead of @ManagedBean? – Kukeltje May 23 '17 at 14:04
  • @Kukeltje Thanks for the suggestion. I considered upgrading to CDI and the `@Named` annotation, but ran into many other problems. But perhaps you are right and I should try to solve those problems instead of getting something to work that will be deprecated soon. – scholt May 23 '17 at 14:06

1 Answers1

0

The answer can be found in the @ManagedBean Javadoc:

The value of the ManagedBean.name attribute is taken to be the managed-bean-name. If the value of the name attribute is unspecified or is the empty String, the managed-bean-name is derived from taking the unqualified class name portion of the fully qualified class name and converting the first character to lower case. For example, if the ManagedBean annotation is on a class with the fully qualified class name com.foo.Bean, and there is no name attribute on the annotation, the managed-bean-name is taken to be bean. The fully qualified class name of the class to which this annotation is attached is taken to be the managed-bean-class.

So your bean is named myBean and not MyBean. If you want it to be MyBean, provide the name with the annotation: @ManagedBean(name = "MyBean").

Jasper de Vries
  • 13,693
  • 6
  • 54
  • 86
  • Thanks a lot for your answer! Unfortunately, I already tried to solve the problem that way by setting the name with `(name = "MyBean")` (and just tried it again), but I still get the same error: `javax.el.PropertyNotFoundException: Target Unreachable, identifier 'MyBean' resolved to null` – scholt May 23 '17 at 11:39
  • I am getting crazy here. I have tried putting the META-INF folder and the contained `faces-config.xml` into every imaginable location, but the error remains the same. – scholt May 23 '17 at 13:43