异常:javax.el.PropertyNotFoundException: Target Unreachable, 'ManageItemBackingBean' returned null

在引用EL#{bean.entity.property}中的managed bean时,有时会出现javax.el.PropertyNotFoundException:Target Unreachable exception。通常是在设置一个property或者触发一个bean action的时候。有五个类似的exception message.

Target Unreachable, identifier 'bean' resolved to null
Target Unreachable, 'entity' returned null
Target Unreachable, 'null' returned null
Target Unreachable, ''0'' returned null
Target Unreachable, 'BracketSuffix' returned null

每种message的原因都不同,解决思路如下:

第一种“Target Unreachable, identifier 'bean' resolved to null“

1. Target Unreachable, identifier 'bean' resolved to null

This boils down to that the managed bean instance itself could not be found by exactly that identifier (managed bean name) in EL like so #{bean}.

Identifying the cause can be breakdown in three steps:

a. Who's managing the bean?
b. What's the (default) managed bean name?
c. Where's the backing bean class?

 第二种 ”Target Unreachable, 'entity' returned null“

2. Target Unreachable, 'entity' returned null

This boils down to that the nested property entity as in #{bean.entity.property} returned null. This usually only exposes when JSF needs to set the value for property via an input component like below, while the #{bean.entity} actually returned null.

<h:inputText value="#{bean.entity.property}" />
You need to make sure that you have prepared the model entity beforehand in a @PostConstruct, or <f:viewAction> method, or perhaps an add() action method in case you're working with CRUD lists and/or dialogs on same view.

第三种 "Target Unreachable, 'null' returned null"

3. Target Unreachable, 'null' returned null

This has actually the same cause as #2, only the (older) EL implementation being used is somewhat buggy in preserving the property name to display in the exception message, which ultimately incorrectly exposed as 'null'. This only makes debugging and fixing a bit harder when you've quite some nested properties like so #{bean.entity.subentity.subsubentity.property}.

The solution is still the same: make sure that the nested entity in question is not null, in all levels.

第四种 ”Target Unreachable, '0' returned null“

4. Target Unreachable, ''0'' returned null

This has also the same cause as #2, only the (older) EL implementation being used is buggy in formulating the exception message. This exposes only when you use the brace notation [] in EL as in #{bean.collection[index]} where the #{bean.collection} itself is non-null, but the item at the specified index doesn't exist. Such a message must then be interpreted as:

Target Unreachable, 'collection[0]' returned null
The solution is also the same as #2: make sure that the collection item is available.

第五种 “Target Unreachable, 'BracketSuffix' returned null“

5. Target Unreachable, 'BracketSuffix' returned null

This has actually the same cause as #4, only the (older) EL implementation being used is somewhat buggy in preserving the iteration index to display in the exception message, which ultimately incorrectly exposed as 'BracketSuffix' which is really the character ]. This only makes debugging and fixing a bit harder when you've multiple items in the collection.

原文出处:https://stackoverflow.com/questions/30128395/identifying-and-solving-javax-el-propertynotfoundexception-target-unreachable

发表评论