0

我是 jsf 的新手,但我成功地从 db 获取数据并使用 primefaces 在 jsf 页面中显示它。我的麻烦是选择一些行并将它们显示在新页面中。这是我的代码:

<p:dataTable id="listOfPresentation" var="r" value="#{recommendationDAO.recos}">
                        <p:column id="recId" headerText="ID">
                            <h:outputText value="#{r.recId}" />                          
                        </p:column>
                        <p:column id="studentCol" filterBy="#{r.student}" 
                                  headerText="Student's Name" filterMatchMode="contains">                                    
                            <h:outputText value="#{r.student}"/>
                        </p:column>

                        <p:column id="groupCol" filterBy="#{r.groupNo}" 
                                  headerText="GroupNo" filterMatchMode="contains">
                            <h:outputText value="#{r.groupNo}" />
                        </p:column>

                        <p:column id="insCol" filterBy="#{r.instructor}"
                                  filterMatchMode="contains" headerText="Instructor's Name">
                            <h:outputText value="#{r.instructor}" />
                        </p:column>                            

                        <p:column headerText="Subject" ><h:outputText value="#{r.subject}" /></p:column>

                        <p:column headerText="Explanation"><h:outputText value="#{r.exp}"/></p:column>

                        <p:column headerText="Date"><h:outputText value="#{r.date}"/></p:column>                            

                    </p:dataTable>

托管豆:

@ManagedBean(name = "recommendationDAO")
@SessionScoped
public class RecommendationDAO {

  private ArrayList<Recommendation> recos;
  private ArrayList<Recommendation> selectedRows;

  public ArrayList<Recommendation> bringRec() {

        recos = new ArrayList<>();

        try {

            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost/itmopresentation", "root", "root");
            String query = "select r.RecommendationId, "
                    + "concat(s.FirstName, ' ', s.FathersName, ' ', s.LastName) as StudentName, s.GroupNo, "
                    + "concat(l.FirstName, ' ', l.FathersName, ' ', l.LastName) as InstructorName, "
                    + "bkp.SubjectOfPresentation, r.Explanation, r.DateOfExplanation "
                    + "from recommendation r join student s on (r.StudentId = s.StudentId) "
                    + "join lecturer l on (r.InstructorId = l.LecturerId) "
                    + "join bkp on (r.BkpId = bkp.BKP_Id) order by StudentName;";
            PreparedStatement pstmt = con.prepareStatement(query);
            ResultSet rs = pstmt.executeQuery();

            while (rs.next()) {
                Recommendation rec = new Recommendation();                
                rec.setRecId(rs.getInt("RecommendationId"));
                rec.setStudent(rs.getString("StudentName"));
                rec.setGroupNo(rs.getInt("GroupNo"));
                rec.setInstructor(rs.getString("InstructorName"));
                rec.setSubject(rs.getString("SubjectOfPresentation"));
                rec.setExp(rs.getString("Explanation"));
                rec.setDate(rs.getDate("DateOfExplanation"));                
                recos.add(rec);
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return recos;
    }    

    public ArrayList<Recommendation> getRecos() {
        return recos;
    }

    public ArrayList<Recommendation> getSelectedRows() {        
        return selectedRows;
    }

}

领域:

@ManagedBean(name = "recommendation")
@SessionScoped
public class Recommendation {

    private int recId;
    private String student;
    private int groupNo;
    private String instructor;
    private String subject;
    private String exp;
    private Date date;
    private boolean selectedItem;

    public Recommendation() {
    }

    public int getRecId() {
        return recId;
    }

    public void setRecId(int recId) {
        this.recId = recId;
    }

    public String getStudent() {
        return student;
    }

    public void setStudent(String student) {
        this.student = student;
    }

    public int getGroupNo() {
        return groupNo;
    }

    public void setGroupNo(int groupNo) {
        this.groupNo = groupNo;
    }

    public String getInstructor() {
        return instructor;
    }

    public void setInstructor(String instructor) {
        this.instructor = instructor;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getExp() {
        return exp;
    }

    public void setExp(String exp) {
        this.exp = exp;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }        

    public boolean isSelectedItem() {
        return selectedItem;
    }

    public void setSelectedItem(boolean selectedItem) {
        this.selectedItem = selectedItem;
    }


    @Override
    public String toString() {
        int id = getRecId();
        String stu = getStudent();
        String ins = getInstructor();
        String sub = getSubject();
        String ex = getExp();
        Date d = getDate();

        return id + " " + stu + " " + ins + " " + sub + " " + ex + " " + d;
    }
}
4

0 回答 0