JSF creates new instance of the same class for each el expression in facelets
I have a login page in a web application project:
<h:form>
<h:messages />
<h:inputText label="Usern开发者_Go百科ame" value="#{login.username}" required="true" />
<h:inputSecret label="Password" value="#{login.pass}" required="true" />
<h:commandButton value="Accedi" action="#{login.check()}" />
</h:form>
When I submit the form, jsf creates three instances of Login class (I've noticed this behavior using debugger). In this way I can't use the username and password in Login.check() method: they are both null.
Besides I've tested another more complex project I've created some time ago and it works fine: only one instance is created. I don't understand where I'm wrong.
Take a look at the managed bean configuration of the login bean (either annotations of the class or in faces-config.xml
), specifically its scope. A scope of "none" would have exactly the effect you're observing. The appropriate scope would probably be "request".
精彩评论