나는 내가 생성자 내부의 초기화 물건을 할 때 나는 InputText]를 현장에서 아무것도 얻을 관리 빈은@PostContruct를 사용하지 않고 런타임시 데이터베이스의 값으로 inputtextfield를 초기화하는 방법은 무엇입니까?
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;
}
}
입니다
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.
이
<h:inputText binding="#{bean.input}" >
</h:inputText>
을 고려하는 것이 매우 잘 알고있다 ,하지만 내가 (Inputtext 상자에 값) @ PostContruct 표시된 메서드에서 배치하는 경우 예상되는 얻을.
함께 생성자 메서드를 교체 :
@PostConstruct
public void init() {
input = new HtmlInputText();
input.setMaxlength(15);
input.setStyle("background: pink;");
input.setValue(fetchValueFromDatabase());
}
@Luiggi은 내가 만든 코멘트에 대한 응답으로 도움 here을 제공 할 것으로 보인다.
참고 : 이것은 또한 정상적으로 작동합니다.
private String input;
public Bean(){
this.input= fetchValueFromDatabase();
}
먼저 바인딩 속성을 사용할 때 bean은'@ RequestScoped '여야합니다. 둘째, HtmlInputText가 각 요청에 대해 JSF에 의해 인스턴스화되기 때문에'input = new HtmlInputText(); '를 사용하면 안됩니다 (http://stackoverflow.com/questions/14911158/how-does-the-binding-attribute-work 참조). -in-jsf-when-and-how-it-be-be-used-used – Tarik