dev@javaserverfaces.java.net

Jsf Hibernate problem in SelectOneList event

From: Giri Prasad <g_pr23_at_yahoo.com>
Date: Tue, 29 Jan 2013 23:22:59 +0800 (SGT)

Hello All,

 I have a very simple jsf page and a backing bean for this jsf, which is implemented using hibernate in Netbeans.

 <Select> button does properly populate the list box. <Details> button does display the debug message in glassfish log, when no entry in list box is chosen.

 After I press <Select>, choose a entry from the list, pressing <Detail> button, is not executing the backing bean function 'studDet()'. On this situation, the screen just refreshes, the list goes empty, and the backing bean function is not executed.

 The same backing bean function ['studDet()']  is executed when nothing is selected from the list. Apparently the logic when implemented via jpa seems to work just perfectly correctly.

Can any one provide your insights, as why this problem is occuring. Thanks in advance.

Regards,

**************
Code
**************
<%--
    Document   : StudentPage.jsp
--%>
 
<%_at_page contentType="text/html" pageEncoding="UTF-8"%>
 
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
 
<f:view>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
            <title>Student Page</title>
            <h:form>   
 
<h:outputLabel value="Teacher Name    "/>
 
<h:inputText id="Student" value="#{studBean.teacherName}" size="20" maxlength="50">
</h:inputText>
               
                <h:selectOneListbox id="studentList" value="#{studBean.selectedItem}" 
                                     size="5" >
                    <f:selectItems value="#{studBean.studentList}" />
                </h:selectOneListbox>
 
<h:commandButton id="Select" action="#{studBean.studSel}" value="Select" />
<h:commandButton id="Details" action="#{studBean.studDet}" value="Details" />
 
               
            </h:form>
        </body>
</html>
</f:view>
 
 
*********************************************************
// Backing bean
 
@ManagedBean
@SessionScoped
public class StudBean implements Serializable  {
 
    private String teacherName;
    private List studentList;
 
    // Getter and setter methods omitted
    //
 
    public String studSel() {
        studentlist = new ArrayList();
        List cList = getStudentDetailForTeacher();
        if(cList.isEmpty())
            return null;
       
        for (int i = 0; i < cList.size(); i++) {
            SelectItem studentid = new SelectItem(cList.get(i).toString());
            studentList.add(studentid);
        }
        return null;
    }
 
 
    public List<String[]> getStudentDetailForTeacher() {
        this.session = HibernateUtil.getSessionFactory().getCurrentSession();
        List<String[]> studentlist = null;
        String strQuery = "select c.id.studentId "
                + "from TeacherStudent c WHERE c.id.teacherId = :teachid1";
        org.hibernate.Transaction tx = session.beginTransaction();
        Query cQuery = session.createQuery(strQuery);
 
        cQuery.setParameter("teachid1", teacherName);
        studentlist = (List<String[]>) cQuery.list();
 
        return studentlist;
    }   
 
    public void studentCodeChanged(ValueChangeEvent e){
        studentId = e.getNewValue().toString();
                System.out.println("studentId = "+studentId);
     }
 
 
    public Boolean studDet() {
        System.out.println("studentId id = "+studentId);   
        return null;
    }       
 
}