0

我正在做一个项目。我需要从 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

应用程序.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>

看到这个页面后,我可以正确登录。 在此处输入图像描述 现在我的错误在哪里?

4

2 回答 2

3

var="application"与引用应用程序上下文 () 的隐式 EL 对象冲突ServletContext您可以在此处找到所有隐式 EL 对象的列表。记住它们。您永远不应该在这些名称上声明 EL 变量。

给它一个不同的名字。例如var="app",var="_application"等。

于 2013-09-02T11:31:56.003 回答
0

在数据表中 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 数据表教程

于 2013-09-02T09:43:05.140 回答