0

I have a simple JSF2.0 from page as shown below:

<h:body> 
    <div><center><h1><big><u>Java Learning Center</u></big></h1></center></div>
    <h2><big><a>Account Login</a></big></h2>
    <h:outputText value="#{userBean.errorMessage}" style="color:red; text-size:20"/>
    <h:form>
        <h:panelGrid id="loginPanel" columns="3">
            <h:outputText value="Username" />
            <h:inputText value="#{userBean.username}" id="username" required="true" />
            <h:message for="username" style="color:red; text-size:18"/>
            <h:outputText value="Password" />
            <h:inputSecret value="#{userBean.password}" id="password" required="true" />
            <h:message for="password" style="color:red; text-size:18"/>
            <h:commandButton value="Login" action="#{userBean.verifyUser()}" />
        </h:panelGrid>
    </h:form>
</h:body>

And the Corresponding JSF2 Bean class is as shown:

@ManagedBean(name="userBean")
@SessionScoped
public class UserBean {
    private String username;
    private String password;
    private String errorMessage;
    //setters and getters
    public String verifyUser() {
      //User verification logic
    }
}

Now, when the form Page is rendered for the first time, I get the output in console as:

getErrorMessage()...
getUsername()...
getPassword()...

My Question is:

1. Why these getter methods are invoked even before submitting the form Page?

And when I am finally submitting the form page with values, then the console output is as follows:

getUsername()...
getPassword()...
setUsername()...
setPassword()...
verifyUser()...

Here,

2. Why the getters are invoked before setters ?. In my view the setters should be invoked first.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Amitesh Rai
  • 866
  • 11
  • 21
  • You need to have the values generated in order to put them in the HTML containing the form sent to the client. – Thorbjørn Ravn Andersen Aug 17 '14 at 18:41
  • To learn JSF, I recommend getting a sane book rather than relying on random tutorial found on the web. The code snippet doesn't look very good with that `
    ` tag which was deprecated in 1998. This is a very strong hint that the author's knowledge in web development must be taken with a bag of salt.
    – BalusC Aug 17 '14 at 19:20
  • @BalusC, Thanks for pointing out that. I am new to web development (infact, new to whole world of programming). At the moment I am reading Core JSF2 Book, which stands there in your whishlist(on your blog). The above code was just my own. By the way your article on JSF debugging was great, Help a lot in clearing doubts about JSF lifecycle. – Amitesh Rai Aug 17 '14 at 21:50

0 Answers0