Search code examples
jsfmanaged-bean

Sending JSF textbox value to a managed bean function as a parameter


I have two textboxes and one submit button in my login.xhtml page. I also have a bean. Here are the codes:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
    <title>Welcome to Online Banking</title>
</h:head>
<h:body>
    <h:outputText value="Online Banking System Login" ></h:outputText>
    <h:form>
        <h:panelGrid columns="2" border="1">
            <h:outputText value="Username:"></h:outputText>
            <h:inputText id="username" value="#{loginBean.username}"></h:inputText>
            <h:outputText value="Password"></h:outputText>
            <h:inputSecret  id="password" value="#{loginBean.password}" >    </h:inputSecret>
            <h:commandButton value="Login" action="#{loginBean.loginCheck(username, password)}"></h:commandButton>

        </h:panelGrid>
    </h:form>
</h:body>
</html>

And the beans file:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package beans;

import javax.inject.Named;
import javax.enterprise.context.Dependent;

/**
 *
 * @author SUUSER
 */
@Named(value = "loginBean")
@Dependent

public class LoginBean {

/**
 * Creates a new instance of LoginBean
 */
public LoginBean() {
}

private static String username="", password="";

public String getUsername(){
    return username;
}
public String getPassword(){
    return password;
}
public void setUsername(String Username){
    username=Username;
}
public void setPassword(String Password){
    password=Password;
}

public void loginCheck(String username, String password){


}
}

I will do the database check in my loginCheck function, so i need to pass the values of those two textboxes as a parameter. But i do not know how to do this. I just tried the code but it just passes empty strings as parameteres. Can anyone help me with this?

Thanks


Solution

  • Actually, you do not need to pass username and password parameters to a action method in your case.

    A JSF page has a request life cycle. If you look inside JSF request life cycles, you will notice that values is applied to managed bean before the action.

    Therefore, loginBean.username and loginBean.password values are set to managed bean username and password fields before the action. You can access them in the action method.

    Your action will be

    <h:commandButton value="Login" action="#{loginBean.loginCheck}"></h:commandButton>
    

    and the action method

    public String loginCheck(){
    
      // search username and password in database.
      // username, password field are set to values on page and accessible in the action method. 
    // Do not forget to navigate a proper page after according to login attempt. 
    
    }
    

    For further reading

    Communication in JSF 2.0 tutorial by BalusC

    Basic Login Mechanism using Filters

    JSF Request Life Cycle