2011-10-10 3 views
1

데이터베이스의 항목을 보여주는 Java Server Faces 2.0의 xhtml 페이지를 간단하게 처리하려고합니다. JDBC-MySQL 메서드가 Database.java 인 경우 빈 페이지 만 얻습니다. 그래서 SQL 쿼리가 작동합니다.관리 대상 빈에 의해 MySQL 데이터를 표시하는 방법은 무엇입니까?

이제는 내 프로그래밍을 http://horstmann.com/corejsf/ 예제를 기반으로합니다. 나는 웹 개발 플러그인 이클립스를 사용하고

, 톰캣 (7)와 MySQL 5.5.14

index.xhtml

<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:f="http://java.sun.com/jsf/core" 
     xmlns:h="http://java.sun.com/jsf/html"> 
    <h:head> 
     <title>pageTitle</title> 
    </h:head> 
    <h:body> 
     <h:form> 
     <h:dataTable value="#{database.all}" var="db"> 
      <h:column> 
       #{db.OriginalToken} 
      </h:column> 
      <h:column> 
       #{db.Probability} 
      </h:column> 
      <h:column> 
       #{db.StandardToken} 
      </h:column> 
      <h:column> 
       #{db.Lemma} 
      </h:column> 
      <h:column> 
       #{db.Notes} 
      </h:column>    
     </h:dataTable> 
     </h:form> 
    </h:body> 
</html> 

얼굴-config.xml에

<?xml version="1.0" encoding="UTF-8"?> 

<faces-config 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd" 
    version="2.0"> 
    <managed-bean> 
     <managed-bean-name>database</managed-bean-name> 
     <managed-bean-class>classes.Database</managed-bean-class> 
     <managed-bean-scope>request</managed-bean-scope> 
    </managed-bean> 

</faces-config> 

web.xml

당신의 관리 빈에서 1,363,210
<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> 
    <display-name>cancTestDatabaseExNovo</display-name> 
    <servlet> 
    <servlet-name>Faces Servlet</servlet-name> 
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>Faces Servlet</servlet-name> 
    <url-pattern>/faces/*</url-pattern> 
    </servlet-mapping> 
    <context-param> 
    <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name> 
    <param-value>resources.application</param-value> 
    </context-param> 
    <context-param> 
    <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description> 
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name> 
    <param-value>client</param-value> 
    </context-param> 
    <context-param> 
    <description> 
    This parameter tells MyFaces if javascript code should be allowed in 
    the rendered HTML output. 
    If javascript is allowed, command_link anchors will have javascript code 
    that submits the corresponding form. 
    If javascript is not allowed, the state saving info and nested parameters 
    will be added as url parameters. 
    Default is 'true'</description> 
    <param-name>org.apache.myfaces.ALLOW_JAVASCRIPT</param-name> 
    <param-value>true</param-value> 
    </context-param> 
    <context-param> 
    <description> 
    If true, rendered HTML code will be formatted, so that it is 'human-readable' 
    i.e. additional line separators and whitespace will be written, that do not 
    influence the HTML code. 
    Default is 'true'</description> 
    <param-name>org.apache.myfaces.PRETTY_HTML</param-name> 
    <param-value>true</param-value> 
    </context-param> 
    <context-param> 
    <param-name>org.apache.myfaces.DETECT_JAVASCRIPT</param-name> 
    <param-value>false</param-value> 
    </context-param> 
    <context-param> 
    <description> 
    If true, a javascript function will be rendered that is able to restore the 
    former vertical scroll on every request. Convenient feature if you have pages 
    with long lists and you do not want the browser page to always jump to the top 
    if you trigger a link or button action that stays on the same page. 
    Default is 'false' 
</description> 
    <param-name>org.apache.myfaces.AUTO_SCROLL</param-name> 
    <param-value>true</param-value> 
    </context-param> 
    <listener> 
    <listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class> 
    </listener> 
</web-app> 

Database.java

package classes; 
import javax.faces.bean.ManagedBean; 
import javax.faces.bean.RequestScoped; 

import java.io.FileInputStream; 
import java.io.IOException; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 
import java.util.Properties; 

import javax.sql.DataSource; 
import javax.sql.rowset.CachedRowSet; 
import javax.annotation.Resource; 


@ManagedBean 
@RequestScoped 
public class Database 
{ 

    private static Statement statement; 
    private static Connection connection; 
    private ResultSet result; 

    public static void main(String[] args) 
    { 
     Database database = new Database(); 

     try 
     {  
      ResultSet result = database.getAll(); 
      while(result.next()) 
      { 
       System.out.println(result.getString(1)); 
      } 
      /*System.out.println("\n"); 
      ResultSet result2 = database.getAll(); 
      while(result2.next()) 
      { 
       System.out.println(result2.getString(1)); 
      }*/ 
     } 
     catch (SQLException e) 
     { 
      e.printStackTrace(); 
      System.out.println("SQLException"); 
     } 
     catch(IOException e) 
     { 
      e.printStackTrace(); 
      System.out.println("IOException"); 
     } 
     catch (InstantiationException e) 
     { 
      e.printStackTrace(); 
      System.out.println("InstantiationException"); 
     } 
     catch (IllegalAccessException e) 
     { 
      e.printStackTrace(); 
      System.out.println("IllegalAccessException"); 
     } 
     catch (ClassNotFoundException e) 
     { 
      e.printStackTrace(); 
      System.out.println("class NOT FOUND"); 
     } 
    } 

    public ResultSet getAll() throws SQLException, IOException, InstantiationException, IllegalAccessException, ClassNotFoundException 
    { 
     try 
     { 
      connection = getConnection(); 
      statement = connection.createStatement(); 
      Statement stmt = connection.createStatement();   
      ResultSet result = stmt.executeQuery("SELECT * FROM LAIDict"); 
      CachedRowSet crs = new com.sun.rowset.CachedRowSetImpl(); 
      crs.populate(result); 
      return crs; 
     } 
     finally 
     { 
      connection.close(); 
     } 

    } 

    /*public String[] getAll() throws SQLException, IOException, InstantiationException, IllegalAccessException, ClassNotFoundException 
    { 
     String[] string = {"ciao", "come", "stai"}; 
     return string; 
    }*/ 

    public void setAll() 
    { 

    } 

    /** 
     Gets a connection from the properties specified 
     in the file database.properties 
     @return the database connection 
    * @throws ClassNotFoundException 
    * @throws IllegalAccessException 
    * @throws InstantiationException 
    */ 
    public static Connection getConnection() 
     throws SQLException, IOException, InstantiationException, IllegalAccessException, ClassNotFoundException 
    { 
     Properties props = new Properties(); 
     FileInputStream in = new FileInputStream("/home/caterpillar/workspace/cancTestDatabaseExNovo/database.properties"); 
     props.load(in); 
     in.close(); 

     String drivers = props.getProperty("jdbc.drivers"); 
     if (drivers != null) 
      System.setProperty("jdbc.drivers", drivers); 
     String url = props.getProperty("jdbc.url"); 
     String username = props.getProperty("jdbc.username"); 
     String password = props.getProperty("jdbc.password"); 
     Class.forName(drivers).newInstance(); 
     return DriverManager.getConnection(url, username, password); 
    } 
    private PreparedStatement insertStatement; 
    private PreparedStatement rispostaStatement; 
    private static final String insertString = "INSERT INTO (?) VALUES (?, ?)"; 
    private static final String insertStringV2 = "INSERT INTO (?) VALUES (?)"; 
    private static final String risposta = "SELECT * FROM LAIDict"; 
} 

답변

3

봐는 :

@ManagedBean 
@RequestScoped 
public class Database 
{ 

    private static Statement statement; 
    private static Connection connection; 
    private ResultSet result; 

    public static void main(String[] args) 
    { 
     // ... 

public static void main(String[] args) 방법은 여기에 속하지 않습니다. java.exe을 사용하여 직접 실행될 Java 응용 프로그램에만 사용됩니다. 모든 Java 작업을 시작하는 엔트리 포인트 메소드입니다. 그러나 웹 응용 프로그램의 경우 해당 메소드는 이미 서버 특정 시작 클래스 중 하나에 들어 있습니다. 이 메소드는 JSF 관리 Bean에 배치하면 안됩니다. 서버가 이미 실행중인 의 HTTP 요청에서 실행되지 않습니다.

관리 Bean 클래스의 (post) 생성자에서 작업해야합니다.

@ManagedBean 
@RequestScoped 
public class Database { 

    public Database() { 
     // Put code here which is to be executed when the bean is constructed. 
    } 

    @PostConstruct 
    public void init() { 
     // Or here, if you depend on injected dependencies like EJBs and so on. 
    } 

    // ... 
} 
구체적인 문제에

관련없는

: 당신은 결코 선언 스레드/요청 static로 데이터를 범위해야한다. 동일한 웹 응용 프로그램 내의 모든 요청/세션간에 공유됩니다. 즉, 코드가 스레드 안전하지 않게됩니다. 또한 전체 코드 디자인은 매우 단단하고 비효율적입니다. 책임을 분리 된 수업으로 분리하는 것이 좋습니다. DAO 패턴을 조회하십시오.

관련 문제