Here is an quote from Oracle JavaEE 6 tutorial docs for passing parameter in value expression and method expression in JSF 2.
Parameters are supported for both value expressions and method expressions. In the following example, which is a modified tag from guessNumber application, a random number is provided as an argument rather than from user input to the method call:
<h:inputText value="#{userNumberBean.userNumber('5')}">
The above example uses a value expression.
And this is the default one:
<h:inputText value="#{userNumberBean.userNumber}">
Bean class -
import java.util.Random;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean
@SessionScoped
public class UserNumberBean {
Integer randomInt = null;
Integer userNumber = null;
public UserNumberBean() {
Random randomGR = new Random();
randomInt = new Integer(randomGR.nextInt(10));
System.out.println("Duke's number: " + randomInt);
}
public void setUserNumber(Integer user_number) {
userNumber = user_number;
}
public Integer getUserNumber() {
return userNumber;
}
}
The following expression is not passing 5 as a parameter to the inputText:
<h:inputText value="#{userNumberBean.userNumber('5')}">
It actually causes an error at run-time.
My question: How do I achieve this ??
Because a method can be invoked during different phases of the lifecycle, method expressions must always use the deferred evaluation syntax. Like lvalue expressions, method expressions can use the . and the [] operators.
The EL defines two kinds of expressions: value expressions and method expressions. Value expressions can either yield a value or set a value. Method expressions reference methods that can be invoked and can return a value.
Expression Language (EL), is a scripting language that's seen adoption within many Java frameworks, such as Spring with SpEL and JBoss with JBoss EL. In this article, we'll focus at the JSF's implementation of this scripting language – Unified EL.
You don't need to pass a parameter in the example you've provided.
In this situation getters and setters are invoked automatically on your backing bean.
The following code will invoke getUserNumber and/or setUserNumber to retrieve and/or modify the value of the inputText component:
<h:inputText value="#{userNumberBean.userNumber}">
The form value entered by the user will be passed to setUserNumber as a parameter.
To pass a parameter to a backing bean method, you might do something like this:
<h:commandButton action="#{userNumberBean.displayAlert('Hey')}" value="Say Hey"/>
<h:commandButton action="#{userNumberBean.displayAlert('Later')}" value="Say Bye"/>
This would invoke a method that looks like this:
public String displayAlert(String someText)
As Bhesh Gurung's answer suggests, you can set userNumber to 5 by default in the constructor.
You could also use one of the methods suggested here to apply a default value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With