我有一个 jsf (jsf + primefaces 4.0 ) 页面。页面上有一个 inputtext 元素和一个命令按钮元素。
这是我的 jsf 文件:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
>
<h:head>
<title>Keresés</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="stylesheet" type="text/css" href="style.css"/>
</h:head>
<ui:debug hotkey="1" />
<h:form>
<h:panelGrid columns="2">
<h:outputText value="ID:"/>
<p:inputText value="#{Kereses.id}" required="true" maxlength="20"/>
<p:commandButton id="addCategoryButton" value="Kereses" onclick="categoryDialog.show();" action="#{Kereses.selectTesztTable}"/>
</h:panelGrid>
<p:dialog id="categoryDialog" header="Category Detail" widgetVar="categoryDialog" closeOnEscape="true" resizable="false" style="width:1000px; height: 500px;" showEffect="explode" hideEffect="bounce">
<p:dataTable value="#{Kereses.values}" var="c"
styleClass="order-table" scrollable="true" scrollWidth="1500" scrollHeight="550">
<p:column style="width: 130px; text-align: center">
<f:facet name="header">OM kód:</f:facet>
<h:outputText value="#{c.omkod}" style="width: 130px; text-align: center"/>
</p:column>
</p:dataTable>
</p:dialog>
</h:form>
</html>
这是我的java代码:
import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean @SessionScoped
public class Kereses implements Serializable {
int id;
String omkod;
String URL = "jdbc:mysql://localhost:3306/osszesito?useUnicode=yes&characterEncoding=UTF-8";
String USER = "User";
String PASSWORD = "Password";
String DRIVER = "com.mysql.jdbc.Driver";
private List<Kereses> values;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getOmkod() {
return omkod;
}
public void setOmkod(String omkod) {
this.omkod = omkod;
}
public List<Kereses> getValues() throws SQLException{
values = selectTesztTable();
System.out.println("++++++++++++++++++++++++++++++++++++++++++++values erteke:" + values + "+++++++++++++++++++++++++++++++");
return values;
}
public Connection getDBConnection() {
Connection dbConnection = null;
try {
Class.forName(DRIVER);
dbConnection= DriverManager.getConnection(URL, USER, PASSWORD);
System.out.println("Connection completed.");
} catch (SQLException e) {
System.out.println(e.getMessage());
}catch(ClassNotFoundException cnfe){
cnfe.printStackTrace();
System.out.println(cnfe.getMessage());
System.exit(-1);
}
return dbConnection;
}
public List<Kereses> selectTesztTable() throws SQLException{
Kereses keres = new Kereses();
List<Kereses> lista = new ArrayList<Kereses>();
ResultSet rs = null;
PreparedStatement pst = null;
Connection con = getDBConnection();
String stmQuery = "SELECT * FROM teljes_osszesito where id like '"+ id + "'";
try {
pst = con.prepareStatement(stmQuery);
rs = pst.executeQuery();
while(rs.next()){
keres.setId(rs.getInt("id"));
keres.setOmkod(rs.getString("omkod"));
System.out.println("id: "+ getId() +" OM kód: " + getOmkod() +"---------------------vege-------------------");
}
return lista;
}catch (SQLException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}finally{
rs.close();
pst.close();
con.close();
}
return lista;
}
}
这是我的问题:
当我搜索时,我在输入文本中写入 id 值,然后单击命令按钮。但页面没有找到值(value)。如果我看到 server.log,我会看到 inputtext 值正确并选择查询正确,但我的 jsf 页面不显示值:(
为什么?谢谢,
最佳答案
看起来您缺少更新数据表。所以该值在bean内部但不显示。
<h:form id="form">
<h:panelGrid columns="2">
<h:outputText value="ID:"/>
<p:inputText value="#{Kereses.id}" required="true" maxlength="20"/>
<p:commandButton id="addCategoryButton" value="Kereses" onclick="categoryDialog.show();" action="#{Kereses.selectTesztTable}" update=":form:data"/>
</h:panelGrid>
...
<p:dataTable id="data" value="#{Kereses.values}" var="c" styleClass="order-table" scrollable="true" scrollWidth="1500" scrollHeight="550">
<p:column style="width: 130px; text-align: center">
<f:facet name="header">OM kód:</f:facet>
<h:outputText value="#{c.omkod}" style="width: 130px; text-align: center"/>
</p:column>
</p:dataTable>
</h:form>
关于java - Primefaces 搜索 mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23610705/