开发者

How to show MySQL data by a managed bean?

开发者 https://www.devze.com 2023-04-12 03:26 出处:网络
I am trying to manage to work a simply xhtml page with Java Server Faces 2.0 that shows entries from a database JDBC-MySQL

I am trying to manage to work a simply xhtml page with Java Server Faces 2.0 that shows entries from a database JDBC-MySQL For the moment I simply get a blank page (no data) The main method in Database.java works well, so the SQL query works.

Now I am basing my programming onto http://horstmann.com/corejsf/ examples.

I am using Eclipse with web development plugin, Tomcat 7 and 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");
       S开发者_运维百科tring 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";
}


Look at your managed bean:

@ManagedBean
@RequestScoped
public class Database
{

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

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

The public static void main(String[] args) method doesn't belong here. It is only used for Java applications which are to be executed directly using java.exe. It's the entry point method to start all the Java works. However, in case of a web application that method is already been put in one of the server-specific startup classes. This method should not be placed in JSF managed beans. It won't be executed upon a HTTP request on an already running server.

You need to do the job in the (post)constructor of the managed bean class.

@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.
    }

    // ...
}

Unrelated to the concrete problem: you should never declare thread/request scoped data as static. It's going to be shared among all requests/sessions within the same web application. In other words, it would make your code threadunsafe. Also the whole code design is extremely tight coupled and inefficient. I suggest to decouple the responsibilities into separate classes. Lookup the DAO pattern.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号