Referencing components from other components
endelt260 Jan 19, 2010 11:10 PMSo, I'm attempting to do something which I think should work, but doesn't. I'm going to post a way oversimplified version of what I'm doing. I'm questioning my fundamental understanding of Seam. What I'm looking for by way of response is:
a) Yes, Brian, what you've written there should work. Something that you've left out must be breaking it
OR
b) No, Brian, you don't get Seam at all.
Given the following code:
@Name("authenticator")
public class AuthenticatorAction extends SeamLoginModule implements Authenticator
{
@Out(required=false)
private WebUser webUser;
public boolean authenticate()
{
//do some authentication stuff
webUser = new WebUser();
webUser.setUserDataOne("one");
webUser.setUserDataTwo("two");
}
}
And the following def of WebUser
@Name("webUser")
@Scope(ScopeType.SESSION)
public class WebUser implements Serializable
{
private String userDataOne;
private String userDataTwo;
public WebUser()
{
this.userDateOne = "initone";
this.userDataTwo = "inittwo";
}
}
Assume the proper getters/setters for userDataOne and userDataTwo (I'll omit them to save space)
I've got another component, we'll call it myThing. It'll be instantiated by a reference in an .xhtml page. (like some tag with value=#{myThing.somevalue}
)
Obviously, the myThing reference won't be made until after the authenticate function has run.
myThing defined as:
@Name("myThing")
@Scope(ScopeType.SESSION)
public class MyThingBean implements Serializable
@In
private WebUser webUser;
public MyThingBean()
{
String myString = webUser.getUserDataOne;
}
}
When I get into the constructor of MyThingBean, because it has been referenced by the xhtml file, webUser is null.
I'm sure that webUser gets instantiated by the authenticate method. Also, the @In annotation on webUser in MyThingBean doesn't have required=false, so why doesn't that cause the code to blow up when webUser is null? Instead I get a NPE in the constructor when I try to access a member of webUser.
I am allowed to access a component like that from within a backing bean, right? Elsewhere in my app I've got actions that access components via the @In annotation just fine, but when I try to do it from inside a backing bean it never seems to work.
Thanks in advance.