JSF不会在setter和getter中获取数据(CRUD App)

时间:2015-08-25 13:23:44

标签: java jsf servlets web-applications crud

我正在通过开发crud app学习jsf并且完全迷失在一个愚蠢的问题中,即我无法将数据添加到数据库中,而我的sql命令和数据库都是正确的。用我微薄的知识我假设是我的xhtml页面方法不同的对象来设置数据和jdbc类使用不同来获取。请一些帮助。

以下是我的index.xhtml代码,其中显示了学生数据库的所有数据:

 <h:head>
    <title>MY CRUD Application</title>
 </h:head>
 <h:body>
    <center>
        <h:form>
            <h:dataTable var="list" value="#{dBQueries.allStudents}" cellpadding="2" cellspacing="2" border="1">

                <h:column>
                <f:facet name="header">
                Student Id
                </f:facet>
                <h:inputText value="#{list.id}"/>
                </h:column>

                <h:column>
                <f:facet name="header">
                Student Name
                </f:facet>
                <h:inputText value="#{list.name}"/>
                </h:column>

            </h:dataTable>
            <h:commandLink value="Add New Student" action="add"/>

        </h:form>


    </center>
 </h:body>
 </html>

以下是我的add.xhtml页面

<h:head>
    <title>Add New Student</title>
</h:head>
<h:body>
    <f:view>
        <h:form>
  <h:panelGrid  border="1" cellpadding="20" cellspacing="20" columns="3">                                              


                <h:outputLabel value="student Id" />
   <h:inputText value="#{studentManagedBean.id}"  required="true"  requiredMessage="Student id is Required" id="eId">
                </h:inputText><h:message for="eId"/>


                <h:outputLabel value="student Name" />
                <h:inputText value="#{studentManagedBean.name}" required="true" requiredMessage="Student Name is Required" id="eName">
                </h:inputText><h:message for="eName"/>


                <h:commandLink  value="saveAdd"  action="index" actionListener="#{dBQueries.add()}" onclick="confirm('Are you Sure')"  />
            </h:panelGrid>
        </h:form>
    </f:view>
</h:body>

以下是我的java Managed Bean Class

@ManagedBean
@RequestScoped
public class StudentManagedBean {
private  int id;
private  String name;

 public StudentManagedBean() {
}


public int getId() {
    return id;
}


public void setId(int id) {
    this.id = id;

}


public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

}

我最后一个DBQueries的java类执行两种方法,1)获取所有学生数据,2)添加新的学生数据。代码如下:

@ManagedBean
@RequestScoped
public class DBQueries  {

String query = null;
public PreparedStatement ps = null;
public ResultSet resultSet = null;
StudentManagedBean bean = new StudentManagedBean();
DBConnectivity connectivity = new DBConnectivity();

public DBQueries() {
}

public ArrayList<StudentManagedBean> getAllStudents() {
    ArrayList<StudentManagedBean> arrayList = new ArrayList();
    Connection con = connectivity.connect();
    query = "SELECT id, name FROM student_record";
    try {
        ps = con.prepareStatement(query);
        resultSet = ps.executeQuery();

        while (resultSet.next()) {

            bean.setId(resultSet.getInt("id"));
            bean.setName(resultSet.getString("name"));
            arrayList.add(bean);
        }
    } catch (SQLException ex) {
        Logger.getLogger(DBQueries.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        connectivity.closeConnection(con, resultSet, ps);
    }

    return arrayList;

}

public void add() {

    Connection con1 = connectivity.connect();
    query = "INSERT INTO student_record(id, name) VALUES(?, ?)";
    try {
        ps = con1.prepareStatement(query);

        ps.setInt(1, bean.getId());
        ps.setString(2, bean.getName());
        ps.executeUpdate();

    } catch (SQLException ex) {
        Logger.getLogger(DBQueries.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        connectivity.closeConnection(con1, resultSet, ps);
    }

}

}

当我点击添加新学生并在add.xhtml上提供学生ID和名称时,它不会将数据保存到数据库中,而且index.xhtml中调用的getAllStudent()也不会显示该数据。我哪里错了?请帮忙

1 个答案:

答案 0 :(得分:2)

您尝试插入数据库的Student实例为空,因为StudentManagedBeanDBQueries中定义为经典java类,而不是其他托管bean,这就是方法{的原因{ {1}}和bean.getId()不返回任何内容。 我假设问题在于bean.getName()无法识别您通过视图使用的其他托管bean。为此,您应该将其注入需要其服务的bean中,在DBQueries bean中的Student bean类之前添加此注释。

DBQueries

此外,对于没有问题的注入,您应该呈现Student bean @ManagedProperty StudentManagedBean bean = new StudentManagedBean();