Я пытаюсь работать с простой HTML-страницей с Java Server Faces 2.0, которая показывает записи из базы данных JDBC-MySQL. На данный момент я просто получаю пустую страницу (без данных). Метод main
в Database.java
работает хорошо, поэтому SQL-запрос работает.
Теперь я основываю свое программирование на http://horstmann.com/corejsf/ примерах.
Я использую 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>
face-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";
}