无法使用搜索参数的名字和姓氏显示搜索结果

罗希特·考沙(Rohit Kaushal)

搜索参数是名字和姓氏,而当我单击搜索时,它得到的值是因为即时消息随后在控制台上打印,但未使用数据表显示。我为此目的编写的代码如下:

studentSearch.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:ui="http://java.sun.com/jsf/facelets"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:f="http://java.sun.com/jsf/core">

    <h:head>
    <title>Search the Student Here</title>
    </h:head>
    <h:body>
        <h:form id="searchForm">
        <center><b>Search the Student Details Here</b></center>

            <h:panelGrid id="searchStudent" columns="4">
                <h:outputLabel id="searchText" value="Enter the firstname of the Student:"></h:outputLabel>
                <h:inputText id="searchfName" value="#{student.firstName}"></h:inputText>

                <h:outputLabel id="searchlText" value="Enter the lastname of the Student:"></h:outputLabel>
                <h:inputText id="searchfName" value="#{student.lastName}"></h:inputText>

                <h:commandButton value="Search Student" action="#{student.fullInfoByname}"></h:commandButton>

                <h:dataTable  value="#{searchBean.lstSearch}" var="student">

                <h:column>
                    <f:facet name="header">
                        Student School ID
                    </f:facet>
                        <h:outputText value="#{student.studentSchoolID}"></h:outputText>
                </h:column>

                <h:column>
                    <f:facet name="header">
                        Student First Name
                    </f:facet>
                        #{student.firstName}
                </h:column>

                <h:column>
                    <f:facet name="header">
                        Student Last Name
                    </f:facet>
                        #{student.lastName}
                </h:column>

                <h:column>
                    <f:facet name="header">
                        Father's Name
                    </f:facet>
                        #{student.fatherName}
                </h:column>

                <h:column>
                    <f:facet name="header">
                        Mother's Name
                    </f:facet>
                        #{student.motherName}
                </h:column>

                <h:column>
                    <f:facet name="header">
                        Gender
                    </f:facet>
                        #{student.gender}
                </h:column>

                </h:dataTable>
            </h:panelGrid>


        </h:form>
    </h:body>
    </html>

用于此目的的方法StudentDao.java方法

    public List<Student> getStudentByName(String fname,String lname)
        {
            Transaction trans=null;
            Session session=HiberUtil.getSessionFactory().openSession();
            try{
                trans=session.beginTransaction();
                String queryString="from Student where firstName = :id or lastName = :idd";
                Query query=session.createQuery(queryString);
                query.setString("id", fname);
                query.setString("idd", lname);
                List<Student> list=query.list();

                if(list.size()>0)
                {
                    return list;
                }
            }catch(Exception e)
            {
                e.printStackTrace();
            }
            return null;
        }




    in Student.java class

    public void fullInfoByname()
        {
            StudentDao dao=new StudentDao();
            List<Student> list=new ArrayList<Student>();
            List<Student> lc=dao.getStudentByName(firstName, lastName);
            for(int i=0;i<lc.size();i++)
            {
                System.out.println(lc.get(0).firstName);

                this.studentSchoolID=lc.get(0).studentSchoolID;
                System.out.println(lc.get(0).studentSchoolID);

                this.lastName=lc.get(0).lastName;
                System.out.println(lc.get(0).lastName);

                this.fatherName=lc.get(0).fatherName;
                System.out.println(lc.get(0).fatherName);

                this.motherName=lc.get(0).motherName;
                System.out.println(lc.get(0).motherName);

                this.gender=lc.get(0).gender;
                System.out.println(lc.get(0).gender);
                this.fee=lc.get(0).fee;
                this.course=lc.get(0).course;
                this.session=lc.get(0).session;
                this.hobbies=lc.get(0).hobbies;
                this.email=lc.get(0).email;
                this.phoneNumber=lc.get(0).phoneNumber;
                this.addressOne=lc.get(0).addressOne;
                this.addressTwo=lc.get(0).addressTwo;
                this.city=lc.get(0).city;
                this.state=lc.get(0).state;
                this.zip=lc.get(0).zip;
                this.country=lc.get(0).country;
                this.status=lc.get(0).status;
            }



            list.addAll(lc);
            System.out.println("list Size="+list.size());
            if(list.size()>0)
            {
                SearchStudent sc=new SearchStudent();
                sc.setLstSearch(list);
            }

        }

现在,在最后一个将列表sc.setLstSearch放入xhtml页面中后,它不显示搜索值,而如果我获得变量lstSearch的大小,则它给出的是正确的大小,而不是去显示它。在searchBean中创建的列表如下。

    package com.school.entity;

    import java.util.Iterator;
    import java.util.List;

    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.SessionScoped;

    @ManagedBean(name="searchBean")
    @SessionScoped

    public class SearchStudent {

        private List<Student> lstSearch;

        public List<Student> getLstSearch() {
            return lstSearch;
        }

        public void setLstSearch(List<Student> lstSearch) {
            this.lstSearch = lstSearch;
            //System.out.println(lstSearch);
            //for checking how many values coming from the list
            /*Iterator<Student> itr=lstSearch.iterator();
            while(itr.hasNext())
            {
                System.out.println(itr.next());
            }*/

        }



    }
开发线

问题出在Student类中,您不应该使用new运算符创建SearchStudent类。尝试使用managedProperty注入它并进行设置。如果您不知道如何使用它,请按照本教程http://www.mkyong.com/jsf2/injecting-managed-beans-in-jsf-2-0/

更新:

在您发表评论之后,我注意到,Student.java是一个实体类。将业务逻辑放在实体类中是不好的,并且在实体类中也不允许注入。因此,如下更新代码

  1. 将方法public void fullInfoByname()移至托管bean(即SearchStudent类)。
  2. 创建两个属性firstNamelastNameSearchStudent类中
  3. 将用于搜索的输入控件的值设置为SearchStudentclass的属性
  4. 将搜索表单中的操作方法更改为fullInfoBynameSearchStudent中的方法
  5. 在此之后,你可以直接调用this.setLstSearch(list);fullInfoByname方法

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

当名字和姓氏存储在不同的列中时,如何搜索全名

来自分类Dev

从LINQ C#中包含全名的字段中搜索名字和姓氏

来自分类Dev

使用表格搜索mysqli表并显示结果

来自分类Dev

无法显示Java查询中的搜索结果

来自分类Dev

MongoDB和Spring Data-按名字/姓氏搜索用户

来自分类Dev

如何在MongoDB中按名字和姓氏搜索用户?

来自分类Dev

在范围内搜索Ruby on Rails中的名字,姓氏或全名

来自分类Dev

如何在给定一个变量的情况下搜索姓氏和名字

来自分类Dev

使用在JTable的JTextfield中键入的名字或姓氏搜索MYSQL数据库过滤

来自分类Dev

输入搜索和iframe以使用html显示结果

来自分类Dev

PHP搜索名字和姓氏

来自分类Dev

正则表达式模式,用于搜索名字和姓氏的首字母

来自分类Dev

在SQL Server中使用Like语句搜索名字和姓氏

来自分类Dev

使用搜索名字和姓氏混合php查询

来自分类Dev

从LINQ C#中包含全名的字段中搜索名字和姓氏

来自分类Dev

使用“ AND”和“ OR”搜索参数

来自分类Dev

使用自定义适配器的Listview搜索无法正确显示搜索结果

来自分类Dev

尝试按名字和姓氏搜索

来自分类Dev

MongoDB和Spring Data-按名字/姓氏搜索用户

来自分类Dev

在PostgreSQL中对串联的名字和姓氏的搜索的优化

来自分类Dev

如何在MongoDB中按名字和姓氏搜索用户?

来自分类Dev

使用姓氏或sed搜索和替换

来自分类Dev

在Angular 2中搜索名字,姓氏和电子邮件

来自分类Dev

搜索名字和姓氏

来自分类Dev

使用 onClick 搜索 Listview 显示错误的结果

来自分类Dev

在 Jquery 自动完成建议中显示额外的属性,例如在搜索过程中显示名字和姓氏

来自分类Dev

在搜索和显示结果上显示按钮

来自分类Dev

使用ajax显示搜索结果

来自分类Dev

使用 DataList 显示搜索结果

Related 相关文章

  1. 1

    当名字和姓氏存储在不同的列中时,如何搜索全名

  2. 2

    从LINQ C#中包含全名的字段中搜索名字和姓氏

  3. 3

    使用表格搜索mysqli表并显示结果

  4. 4

    无法显示Java查询中的搜索结果

  5. 5

    MongoDB和Spring Data-按名字/姓氏搜索用户

  6. 6

    如何在MongoDB中按名字和姓氏搜索用户?

  7. 7

    在范围内搜索Ruby on Rails中的名字,姓氏或全名

  8. 8

    如何在给定一个变量的情况下搜索姓氏和名字

  9. 9

    使用在JTable的JTextfield中键入的名字或姓氏搜索MYSQL数据库过滤

  10. 10

    输入搜索和iframe以使用html显示结果

  11. 11

    PHP搜索名字和姓氏

  12. 12

    正则表达式模式,用于搜索名字和姓氏的首字母

  13. 13

    在SQL Server中使用Like语句搜索名字和姓氏

  14. 14

    使用搜索名字和姓氏混合php查询

  15. 15

    从LINQ C#中包含全名的字段中搜索名字和姓氏

  16. 16

    使用“ AND”和“ OR”搜索参数

  17. 17

    使用自定义适配器的Listview搜索无法正确显示搜索结果

  18. 18

    尝试按名字和姓氏搜索

  19. 19

    MongoDB和Spring Data-按名字/姓氏搜索用户

  20. 20

    在PostgreSQL中对串联的名字和姓氏的搜索的优化

  21. 21

    如何在MongoDB中按名字和姓氏搜索用户?

  22. 22

    使用姓氏或sed搜索和替换

  23. 23

    在Angular 2中搜索名字,姓氏和电子邮件

  24. 24

    搜索名字和姓氏

  25. 25

    使用 onClick 搜索 Listview 显示错误的结果

  26. 26

    在 Jquery 自动完成建议中显示额外的属性,例如在搜索过程中显示名字和姓氏

  27. 27

    在搜索和显示结果上显示按钮

  28. 28

    使用ajax显示搜索结果

  29. 29

    使用 DataList 显示搜索结果

热门标签

归档