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();
}