Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass parameter in value expression and method expression in JSF 2?

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 ??

like image 755
exexzian Avatar asked Aug 14 '12 02:08

exexzian


People also ask

Which is the correct syntax to use method expression in a JSF page?

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.

What are method value and expression?

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.

What is JSF expression language?

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.


1 Answers

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.

like image 163
jahroy Avatar answered Nov 04 '22 06:11

jahroy