Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
690 views
in Technique[技术] by (71.8m points)

jsf - 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();
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...