ulquiorra
ulquiorra

Reputation: 945

pass form values in managed bean

i want to pass my form value into my managed bean in order to process it but i always got a null value when i try to retrieve the value in my action method

My bean

@ManagedBean(name="datas")
@SessionScoped

public class Datas implements Serializable {

    private static final long serialVersionUID = 1L;
    private String ID_primary;

    public Datas(){

    }

    public Datas (String ID_primary){   
        this.ID_primary=ID_primary;
    }
    public String getID_primary() {
        return ID_primary;
    }
    public void setID_primary(String ID_primary) {
        this.ID_primary= ID_primary;
    }

    public void process() {

        Map<String, String> request=FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();

        String value = request.get("ID_primary"); // always return null
        System.out.println("ID" + value);

    }
}

My form

<h:form>
    <div align="left">                      
            <h:inputText id="ID_primary" name="ID_primary" value="#{datas.ID_primary}" />                           
    </div>
    ..........


    <div align="right">

                <h:commandButton action="#{datas.process()}" value="Create" type="submit" />
  </div>
</h:form>

Thank you very much for your help

Upvotes: 2

Views: 989

Answers (1)

mike_m
mike_m

Reputation: 1546

You have two different form elements, when you click commandButton in the bottom form, values from the top form are not send with the request.

You can have tags like div inside a form, so you can make on big form with divs inside if it is the reason why you have have split them.

Also add @ManagedProperty annotation to ID_primary field:

public class Datas implements Serializable {

    private static final long serialVersionUID = 1L;
    @ManagedProperty(value = "#{Datas.ID_primary}")
    private String ID_primary;
    [..]

EDIT:

Ok, my bad, I have not looked at the code carefully. In ManagedBean with ManagedProperty this property will be set automatically by JSF, so you can read it like this:

public void process() {
        System.out.println("ID" + getID_primary());
}

Upvotes: 2

Related Questions