日期:2014-05-17  浏览次数:20832 次

有关搜索的问题,很简单,就是弄不出来
做了一个简单的demo,想要实现搜索一个学生的姓名,然后显示学生的个人信息,感觉输入的信息不能进入到后台,代码如下
Java code

public boolean getStudentsByName(students st) {
        System.out.println("验证数据...");
        String hql = "from students s where s.name = '" + st.getName() + "'";
        List list= this.getHibernateTemplate().find(hql);
        System.out.println("共找到数据:" + list.size());
        if(list.size() > 0 && list != null) {
            return true;
        }

        return false;
    }


这是dao里面的代码,验证数据...后面的hql语句还有list都没有看到输入的name,然后“共找到数据:”就是0
service里面实现了对dao 的调用
然后是action的代码
Java code

public String showStudents() throws Exception {
        students st = new students();
        if(studentsService.getStudentsByName(st)) {
            System.out.println("登录状态:" + studentsService.getStudentsByName(st));
            return SUCCESS;
        }
        return ERROR;
    }


里面students st = new students(); 如果不定义st,那么dao里面的getStudentsByName(students st) ,st会显示为null
这是jsp页面的搜索:
<s:textfield name="st.name" label="请输入学生姓名:"></s:textfield><s:submit value="搜索"></s:submit>

------解决方案--------------------
students st = new students();
if(studentsService.getStudentsByName(st)) {
System.out.println("登录状态:" + studentsService.getStudentsByName(st));
你在这new一个student当然传进去的参数是null的
你这的st应该在form里定义的,这样表单提交的时候就能将页面值传进去,
还有你的action应该有参数ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response或者你这样写的后可以用request获得页面值
------解决方案--------------------
Java code

jsp:
<form id="form1" name="form1" method="post" action="**.action">
<s:textfield name="name" label="请输入学生姓名:"></s:textfield><s:submit value="搜索"></s:submit>

action:
public String name;
public String showStudents() throws Exception {
            if(studentsService.getStudentsByName(name)) {
            System.out.println("登录状态:" + studentsService.getStudentsByName(st));
            return SUCCESS;
        }
        return ERROR;
    }
geter  seter;

service:
public boolean getStudentsByName(String st) {
        System.out.println("验证数据...");
        String hql = "from students s where s.name = '" + st.getName() + "'";
        List list= this.getHibernateTemplate().find(hql);
        System.out.println("共找到数据:" + list.size());
        if(list.size() > 0 && list != null) {
            return true;
        }

        return false;
    }

------解决方案--------------------
探讨
Java code


jsp:
<form id="form1" name="form1" method="post" action="**.action">
<s:textfield name="name" label="请输入学生姓名:"></s:textfield><s:submit value="搜索"></s:submit>
</form>
action:
public String……

------解决方案--------------------
<s:textfield name="st.name" label="请输入学生姓名:"></s:textfield><s:submit value="搜索"></s:submit>

根据你的写法,需要在对应的action类中有一个属性:private String st.name; 然后是get、set方法,这样你提交的时候就能够接收到界面传输过来的数据,然后再写一个方法,
Student st = (Student )getHibernateTemplate().find("from Student whe