How to get managedbean property from another bean in JSF

后端 未结 1 662
伪装坚强ぢ
伪装坚强ぢ 2020-12-13 11:03

I searched similar questions but I\'m a bit confused. I have a login page, so LoginBean also which is;

@ManagedBean(name = \"loginBean\")
@SessionScoped
publ         


        
相关标签:
1条回答
  • 2020-12-13 11:28

    The @ManagedProperty declares the location where JSF should set the property, not where JSF should "export" the property. You need to just inject the LoginBean as property of OrderBean.

    public class OrderBean {
    
        @ManagedProperty(value="#{loginBean}")
        private LoginBean loginBean; // +setter
    
        // ...
    }
    

    This way you can access it in the OrderBean by just

    loginBean.getIdentityNr();
    

    Alternatively, if you make your OrderBean request or view scoped, then you can also set only the identityNr property.

    public class OrderBean {
    
        @ManagedProperty(value="#{loginBean.identityNr}")
        private String identityNr; // +setter
    
        // ...
    }
    

    Unrelated to the concrete problem: initializing String properties with an empty string is a poor practice.

    0 讨论(0)
提交回复
热议问题