2011-10-10 43 views
1

我試圖管理工作的一個簡單的XHTML頁面與Java服務器面臨2.0,顯示從數據庫JDBC-MySQL的 條目就目前而言,我只是得到一個空白頁(無數據) 在Database.java作品的main方法好,所以SQL查詢的作品。如何通過託管bean顯示MySQL數據?

現在我基於我的編程 http://horstmann.com/corejsf/的例子。

我使用Eclipse Web開發插件,Tomcat的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> 

faces-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

<?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

看看你的託管bean:

@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作品的切入點方法。但是,對於Web應用程序,該方法已被放入其中一個特定於服務器的啓動類中。此方法不應放置在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。它將在同一個Web應用程序中的所有請求/會話之間共享。換句話說,它會讓你的代碼不安全。整個代碼設計也非常緊密耦合且效率低下。我建議把責任分解成不同的課程。查找DAO模式。