Sunday, August 19, 2012

JSF doesn't work well with Spring

On last week, I had been struggling on how could I integrate JSF and Spring together in a proper way. Since I am using JSF 2.0, I do know that I am required to have the following code to be place in faces-config.xml file:

< application >
  < variable-resolver > 
  < /variable-resolver >
< /application >

And my authentication bean and main screen bean was declared using session scope (consider the following code), but I am still not able to show my welcome string in the page.

@ManagedBean(name= "authBean" )
public class Authentication {

   @ManagedProperty( value="authVo" )
   private User user;

   /** getter and setter of user **/

@ManagedBean(name= "authVo" )
public class User {
  private String logonUser;

  /** getter and setter of logonUser **/

@ManagedBean(name= "mainBean" )
public class MainScreen {

   private String welcome = "Welcome to main screen";
   /** getter and setter of welcome **/

After a long searching on the root cause, I found this code in mainScreen.xhtml file cause the problem:

< h5 >
  You are currently logon as: 
  < h:outputText value= "#{authBean.authVo.logonUser}"/ >
</ h5 >
< h:outputText value= "#{mainBean.welcome}"/ >

If I remove the first outputText, the setter of MainScreen class will get invoked, welcome string is shown, otherwise it don't. Meaning that, if I invoke the authBean first, then I can only see the property of authBean, otherwise I will only see mainBean. Now I know that each screen can only have one bean associate with it. Does it really behave in that way when Spring integrate into JSF?

One of my colleague argue that it shouldn't happen in this way, since the Bean was already declared in session scope, it should be seen globally. But that one was JBoss Seam.

Anyhow there is still work around on this. I just need to follow the Spring way, which is the Spring IoC way, to retrieve the logonUser and showing welcome string.

In the mean time, I need to look into the magic of JBoss Seam.

No comments: