有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Primefaces搜索mysql

我有一个jsf(jsf+primefaces 4.0)页面。页面上有一个inputtext元素和一个command button元素

这是我的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;

    }


}

我的问题是:

enter image description here

搜索时,我在inputtext中写入id值,然后单击命令按钮。但是页面没有找到值。如果我看到服务器。日志中,我看到inputtext value right和select query right,但我的jsf页面不显示值:(

为什么

谢谢——


共 (1) 个答案

  1. # 1 楼答案

    看起来您缺少更新数据表的功能。因此,值在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>