6

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

0

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Not sure. I actually haven't use JSF in quite some time. It shouldn't really matter.
no I checked it in IDE there is no action attribute for that button tag though commandButton does have and its working pretty well with that. So, I guess you should edit it accordingly
2

To set (display) 5 as the default value in the textbox, make the following change in the view -

<h:inputText value="#{userNumberBean.userNumber}"> 

and set the value of the property in the managedbean:

@ManagedBean @SessionScoped public class UserNumberBean { private Integer randomInt = null; private Integer userNumber = null; public UserNumberBean() { //... this.userNumber = 5; // try this here } //getter/setter 

Comments

-1

Use JBoss EL. See this documentation of JBoss EL.

8 Comments

Tomcat7 supports it, I think.
No idea about Tomcat but its not working on GlassFish 3... Let me try including that jar hope that works
@exex: This answer does not explain/answer your concrete problem at all. -1 for that. Whoever upvoted this answer should reread the question.
@BalusC You shouldn't have downvoted this he has given correct answer.
@exex: no, JBoss EL won't solve your problem. You simply used invalid EL syntax, as explained by jahroy. No one EL implementation would solve it. You just need to fix your invalid EL syntax. Besides, JBoss EL is only useful if your environment doesn't support EL 2.2. But yours does! (EL 2.2 is part of Java EE 6). So this answer is not only wrong, but also useless in its entirety.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.