maralbjo
maralbjo

Reputation: 963

Trouble setting default value for a member variable in JSF

package no.northcreek.mabjo;
import javax.annotation.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;

@ManagedBean
@RequestScoped
public class indexBean {

    @ManagedProperty(value="defaultValue")
    private String firstName;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }  
}

So above is a code I expect to create the firstName member variable with a default value of "defaultValue". However the value is null. Why?

Upvotes: 0

Views: 1443

Answers (1)

Daniel
Daniel

Reputation: 37061

Seems that you misunderstood the usage of @ManagedProperty

@ManagedProperty annotation is used to dependency injection (DI) a managed bean into the property of another managed bean.

and note that value should point to an ELxpression , like this : value="#{someBean}"

In your case you should just do the following

private String firstName = "defaultValue";

OR

init the value of firstName in your @PostConstruct

@PostConstruct
public void init() {
  firstName = "defaultValue";
}

take a look at this example...

Injecting Managed beans in JSF 2.0

Upvotes: 1

Related Questions