如何通过托管bean显示MySQL数据?

时间:2011-10-10 12:14:12

标签: java mysql jdbc jsf-2 facelets

我正在尝试使用Java Server Faces 2.0来管理一个简单的xhtml页面,该页面显示来自数据库的条目JDBC-MySQL 目前我只是得到一个空白页面(没有数据) main中的Database.java方法效果很好,因此SQL查询可以正常工作。

现在我正在编程 http://horstmann.com/corejsf/例子。

我正在使用带有Web开发插件的Eclipse,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>

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

1 个答案:

答案 0 :(得分: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模式。