jsfeljsf-2.2

How to initialize inputtextfield with a value from database on runtime without the use of @PostContruct?


I know very well that

Any scoped managed bean method annotated with @PostConstruct will be called
after the managed bean is instantiated, but before the bean is placed in scope.

Consider

<h:inputText binding="#{bean.input}" >
</h:inputText>

where the managed bean is

public class Bean {
    private HtmlInputText input; 
    public PreInitializeBean(){
        input = new HtmlInputText();
        input.setMaxlength(15);
        input.setStyle("background: pink;");
        input.setValue(fetchValueFromDatabase());
    }

    private Object fetchValueFromDatabase() {
        String resultValue = null;
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            Connection con = DriverManager.getConnection(
                    "jdbc:oracle:thin:@localhost:1521:xe", "system", "system");


            System.out.println("Connection Object: "+con);
            // retieving data from RESULT table
            PreparedStatement ps = con
                    .prepareStatement("select * from RESULT",
                            ResultSet.TYPE_SCROLL_SENSITIVE,
                            ResultSet.CONCUR_UPDATABLE);

            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                System.out.print("<br>" + rs.getInt(1) + " " + rs.getString(2) + " "
                        + rs.getString(3) + " " + rs.getString(4));
                resultValue = rs.getString(2);
            }

            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultValue;
    }

    public HtmlInputText getInput() {
        return input;
    }

    public void setInput(HtmlInputText input) {
        this.input = input;
    }

}

I get nothing in the inputtext field when i do the initialisation stuff inside the Contructor, but i get the expected(a value in the inputtext box) if what I am doing I place it in a method marked with @PostContruct.
Replace the constructor method with:

    @PostConstruct
    public void init() {
        input = new HtmlInputText();
        input.setMaxlength(15);
        input.setStyle("background: pink;");
        input.setValue(fetchValueFromDatabase());
    }

@Luiggi seems to offer some help here in response to a comment I made.

Note: This also works fine.

private String input;

public Bean(){
    this.input= fetchValueFromDatabase();
}

Solution

  • Actually I couldn't reproduce your issue. It works fine for me. I tested with Mojarra 2.2.8 and Apache Tomcat 7.0.47. Did you see any Errors?`maybe in your Database code? Was the background style applied?


    However I'm not sure whether you really need the binding? You can also try the following approach.

    private String input = fetchValueFromDatabase();
    
    public PreInitializeBean(){
    }
    
    private String fetchValueFromDatabase() {
        String resultValue = "preSetValue";
        return resultValue;
    }
    
    public String getInput() {
        return input;
    }
    
    public void setInput(String input) {
        this.input = input;
    }
    

    And the xml:

    <h:inputText value="#{data.input}" maxlength="15" style="background: pink;">
    </h:inputText>
    

    I think this is more conventional.