Skip to main content

Error accessing @ManagedProperty in JSF2.0 across @ManagedBeans

11 replies [Last post]
userjava1
Offline
Joined: 2009-06-03
Points: 0

Hi,

Why can't i access this property across @ManagedBeans who has same Session scope? Can you pls help what i am missing?

In the below example id is set by JSF facelet and its set via Text Box.

@ManagedBean(name="a")
@SessionScoped
public class A {
@ManagedProperty(name="id",value="#{id}")
private int id;

//getter and setter methods
}

@ManagedBean(name="b")
@SessionScoped
public class B {
@ManagedProperty(value="#{a.id}")
private int id;

//Only setter method

private String printValue(){
System.out.println("ID Value is"+id);
}
}

Thanks,
User.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Ed Burns

>>>>> On Mon, 19 Jul 2010 14:11:16 PDT, webtier@javadesktop.org said:

>> It works with JSF 2.0.2 also only change is it needs
>> both Getter and Setter methods for ID in Class B
>>
>> Issue still in 2.0.2 and 2.1.0 is
>>
>> 1. When user hit backbutton and change a different
>> value for ID (in text box its updating A) but its not
>> propagating to ID Class B (Class B ID still has old
>> value).
>>
>> I will log this as a defect.

R> But that would not be a defect, but expected behavior. B has already
R> been instantiated and is in the session so there is no reason for JSF
R> to create it again or change the values of its properties.

I also have to point out that "id" is a reserved attribute name. What
happens if you use a different attribute name?

Ed

--
| edburns... | office: +1 407 458 0017
| homepage: | http://ridingthecrest.com/

---------------------------------------------------------------------
To unsubscribe, e-mail: webtier-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: webtier-help@glassfish.dev.java.net

raydecampo
Offline
Joined: 2007-07-15
Points: 0

It sounds like you have some scoping issues if the value of a session bean property is being set by request data. Keep in mind that the value of id for B will be set when B is created and will not be updated just because the value of id for A is updated. Perhaps it makes more sense to have A injected into B and have B get the id from the instance of A.

userjava1
Offline
Joined: 2009-06-03
Points: 0

Just to add one more Point

Both are Session Scoped and B cannot be accessed untill the JSF for A is set (this is how my page navigations are designed).

In this Case A's ID needs to be entered before page can access B. I am not getting any exeception but the value is not carried from A to B.

Thanks,
User

raydecampo
Offline
Joined: 2007-07-15
Points: 0

Well there appear to be two possibilities (although I am open to the idea there are more). The first is that injection is broken is JSF (which version and implementation are you using anyway?). The second is that you are mistaken concerning when exactly B is created.

#1 implies there is a serious bug in a widely-used open source library. That is unlikely (although not impossible). Many people would have to had dropped the ball. Also, this is a pretty fundamental part of the library, so it is likely that others would also discover the issue. #2 implies that one person has made a mistake.

From an objective point-of-view it is much more likely that #2 is the actual scenario. So you need to do a bit of work before ruling it out and jumping to #1. Add some logging to see when exactly B is created and the id property is set. Construct a small example that demonstrates the issue. Once you have a small example that reproduces the problem and you are sure it is correct, log a bug report against the implementation of JSF you are using.

Oops, another step before logging a bug report: upgrade to the latest version of the implementation.

userjava1
Offline
Joined: 2009-06-03
Points: 0

I am using mojarra-2.0.2-FCS on JBoss 6.0 M2. I agree it may not be #1 :)

raydecampo
Offline
Joined: 2007-07-15
Points: 0

Well there is a Mojarra 2.0.3. Try that out, and try making a small example. If you still see the problem, post a bug report to https://javaserverfaces.dev.java.net/issues/

userjava1
Offline
Joined: 2009-06-03
Points: 0

It works with JSF 2.0.2 also only change is it needs both Getter and Setter methods for ID in Class B

Issue still in 2.0.2 and 2.1.0 is

1. When user hit backbutton and change a different value for ID (in text box its updating A) but its not propagating to ID Class B (Class B ID still has old value).

I will log this as a defect.

Thanks,
User.

raydecampo
Offline
Joined: 2007-07-15
Points: 0

> It works with JSF 2.0.2 also only change is it needs
> both Getter and Setter methods for ID in Class B
>
> Issue still in 2.0.2 and 2.1.0 is
>
> 1. When user hit backbutton and change a different
> value for ID (in text box its updating A) but its not
> propagating to ID Class B (Class B ID still has old
> value).
>
> I will log this as a defect.

But that would not be a defect, but expected behavior. B has already been instantiated and is in the session so there is no reason for JSF to create it again or change the values of its properties.

userjava1
Offline
Joined: 2009-06-03
Points: 0

So how do you update the value when its changed?

raydecampo
Offline
Joined: 2007-07-15
Points: 0

> So how do you update the value when its changed?

Well you could go the JavaBean route with property change listeners and all that jazz....

Or you could take my earlier suggestion to inject the instance of A into the instance of B, so then B can just call A.getId() and always get the correct value.

userjava1
Offline
Joined: 2009-06-03
Points: 0

This is bug and Ed fixed in 2.2 version
http://java.net/jira/browse/JAVASERVERFACES-1737?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
Nice thing i did is file a bug and help it get resolved in future versions
raydecampo nice i did not listen to your advice :) and do agree if its a defect