簡體   English   中英

無法在PrimeFaces,jsf中獲得數據表行選擇?

[英]Can not get data table row selection in PrimeFaces, jsf?

我正在做一個項目。 我需要從MySql數據庫中獲取一個列表並將其列出。 我正在使用JSF 2.1 Primeface 3.5和Eclipse Juno。 我運行我的代碼,但是它不起作用。 您可以在下面看到我的代碼

     //LOGIN CLASS

    import parts
    @ManagedBean
    @SessionScoped
    public class Login {

private String username, password;
private PreparedStatement ps, ps2;
private ResultSet rs, rs2;
private List<Application> applications = new ArrayList<Application>();;
private Application selectedApplication;

// GETTERS SETTERS



public String login() {
    Connection object = new Connection();

    try {

        ps = nesne
                .getCon()
                .prepareStatement(
                        "select Username, Password from company where Username=? and Password=?");
        ps.setString(1, getUsername());
        ps.setString(2, getPassword());
        rs = ps.executeQuery();

        while (rs.next()) {

            getList();
            return "application";

        }

    } catch (Exception e) {
        System.err.println(e);

    }


    return "confirm";
}


private List<Application> getList() {
    Baglanti nesne = new Baglanti();
    try {

        ps2 = nesne
                .getCon()
                .prepareStatement(
                        "select ApplicationName from application where CompanyID=(select ID from company "
                                + "where Username=? and Password=?)");

        ps2.setString(1, getUsername());
        ps2.setString(2, getPassword());
        rs2 = ps2.executeQuery();


        while (rs2.next()) {
            Application obj = new Application();
            obj.setApplicationName(rs2.getString("ApplicationName"));
            applications.add(obj);

        }

    } catch (Exception e) {
        System.err.println(e);

    }
    return applications;
}

應用類別

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean
@SessionScoped
public class Application {

private int ID;
private int CompanyID;
private String Type;
private Date Date;
private String ApplicationName;
private int CurrentMessageCount;
private int MaxMessage;
private String isPro;


    //GETTERS SETTERS

application.xhtml

   <!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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
   <h:head>
    <title>Login Confirmed</title>
   </h:head>
   <h:body>
    <h1 class="ui-widget-header ui-corner-all" align="center">Application
    List</h1>
<br />
<h:form id="form">

    <p:growl id="msgs" showDetail="true" />

    <p:dataTable id="applications" var="application"
        value="#{login.applications}">

        <p:column headerText="Application" style="width:24%">
            <h:outputText value="#{login.applications}" />
        </p:column>

        <p:column style="width:4%">
            <p:commandButton id="selectButton" icon="ui-icon-search"
                title="View">
                <f:setPropertyActionListener value="#{application}"
                    target="#{login.selectedApplication}" />
            </p:commandButton>
        </p:column>

    </p:dataTable>


</h:form>

我可以在看到該頁面后正確登錄。 在此處輸入圖片說明 現在我的錯誤在哪里?

您的var="application"與引用應用程序上下文( ServletContext )的隱式EL對象沖突。 您可以在此處找到所有隱式EL對象的列表 記住他們。 絕對不要在這些名稱上聲明EL變量。

給它起一個不同的名字。 例如var="app"var="_application"等。

在數據表中,var屬性表示數據庫中的每個項目都可以使用此“ var”值。 即:您上了Foo課:

class Foo{
int number;
String text;
//Setters and getters
}

還有另一個處理Foo對象列表的類(您的模型為CDI Bean):

@Named
class Boo{
List<Foo> list = new ArrayList<>();
//Getter and setters
}

因此,要在jsf頁面中列出所有內容,您應該像這樣使用它:

<p:dataTable id="list" var="listobject" value="#{boo.list}">
   <p:column headerText="Number" style="width:24%">
      <h:outputText value="#{listobject.number}" />
   </p:column>
   <p:column headerText="Text" style="width:24%">
      <h:outputText value="#{listobject.String}" />
   </p:column>
</p:dataTable>

因此,摘要“ var”值是boo對象的訪問者字符串。

另請參閱PrimeFaces數據表演示和此處的Mkyong數據表教程

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM