2014-12-18 7 views
0

나는 내가 생성자 내부의 초기화 물건을 할 때 나는 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(); 
} 
+0

먼저 바인딩 속성을 사용할 때 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

답변

1

실제로 문제를 재현 할 수 없습니다. 그것은 나를 위해 잘 작동합니다. 필자는 Mojarra 2.2.8 및 Apache Tomcat 7.0.47을 테스트했습니다. 어떤 오류가 있었습니까? "아마도 데이터베이스 코드에서? 배경 스타일이 적용 되었습니까?


그러나 바인딩이 필요한지 확실하지 않습니다. 다음 방법을 시도해 볼 수도 있습니다.

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

하고 XML :

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

나는이보다 전통적인 생각합니다.