Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set default value of input field while using EL statement to read value

Tags:

jsf

el

So my team has a jsf and we use an EL statement to read in the value of an input box, works fine but by default we have our value set as what should be filled into the field. How can I set a default value for the input while still having the EL statement work. Example:

 <h:inputText value="#{registrationBean.firstName}" styleClass="formEle"/>

I tried

<h:inputText value="#{registrationBean.firstName}First Name" styleClass="formEle"/>

but this breaks the connection when we submit. Any ideas?

like image 457
Evo510 Avatar asked Apr 12 '12 20:04

Evo510


People also ask

How do I set default input value?

The Input Text defaultValue Property in HTML DOM is used to set or return the default value of the Text Field. This property is used to reflect the HTML value attribute.

How can you specify default text in an input field?

The value attribute specifies the value of an <input> element. The value attribute is used differently for different input types: For "button", "reset", and "submit" - it defines the text on the button. For "text", "password", and "hidden" - it defines the initial (default) value of the input field.

What is the way set default text inside textbox in HTML?

If you want to set the default value to a textbox, you can use the value attribute. Whenever the form is loaded, the default value is shown inside the textbox.

What do you mean by default value of a field in base and how can it be set?

Set a default value Select the field that you want to change. On the General tab, type a value in the Default Value property box. The value you that you can enter depends on the data type that is set for the field. For example, you can type =Date() to insert the current date in a Date/Time field.


2 Answers

You're looking for something which is called a "watermark" or "placeholder". At low level, these days this is normally to be done by the HTML5 placeholder attribute:

<input type="text" placeholder="First Name" />

This works of course only in browsers supporting HTML5 and then only with JSF components supporting the new HTML5 attribute. The standard JSF <h:inputText> doesn't support this natively (yet). You'd need to look for 3rd party component libraries supporting this. Among them is PrimeFaces with its <p:watermark>:

<h:inputText id="firstName" value="#{registrationBean.firstName}" />  
<p:watermark for="firstName" value="First Name" />  

If using PrimeFaces is not an option for some reason, you'd need to reinvent it yourself in flavor of a custom component and/or a custom piece of JS/jQuery, exactly like <p:watermark> is doing under the covers.

like image 108
BalusC Avatar answered Oct 04 '22 20:10

BalusC


You could define default value of <h:inputText> in your backing bean in PostConstrut phase.

@ManagedBean(name="registrationBean")   
public class RegistrationBean{

    private String firstName;

    @PostConstruct
    public void init(){
        firstName = "your default value";
    }

}

After page creation value displayed in h:inputText will be as you defined in backing bean. It's kind of work around without any third party library maybe it will help someone.

like image 37
Onizuka Avatar answered Oct 04 '22 22:10

Onizuka