简体   繁体   中英

how to display the data from database on a button click in java jsf

<f:view>
        <h:form>
            <h:panelGrid>
                <f:facet name="header">
                    <h:outputText value="Student Mark List"/>
                </f:facet>
                <h:column>
                    <h:outputText value="Student Number : "></h:outputText>
                    <h:inputText value="#{stuBean.stuNumber}"/>
                </h:column>
                <h:column>
                    <h:commandButton id = "getStuMarkList" value="Get Mark List" action="#{stuBean.listOfMarks}" >
                    </h:commandButton> </h:column>
            </h:panelGrid>                          
            <h:panelGrid  bgcolor="#9AC8E6" width="100%">
              <h:dataTable id="datatable" value="#{stuBean.marksList}" var="marksList">
                    <h:column>
                        <f:facet name="header">
                            <h:outputText style=""value="Maths Marks" />
                        </f:facet>
                        <h:inputText value="#{marksList.mMarks}" > </h:inputText>
                    </h:column>
                    <h:column>
                        <f:facet name="header">
                            <h:outputText style=""value="English Marks" />
                        </f:facet>
                        <h:inputText value="#{marksList.eMarks}" > </h:inputText>
                    </h:column>
                    <h:column>
                        <f:facet name="header">
                            <h:outputText style=""value="Physics Marks" />
                        </f:facet>
                        <h:inputText value="#{marksList.pMarks}" > </h:inputText>
                    </h:column>
                    <h:column>
                        <f:facet name="header">
                            <h:outputText style=""value="Social Marks" />
                        </f:facet>
                        <h:inputText value="#{marksList.sMarks}" > </h:inputText>
                    </h:column>
                </h:dataTable>
            </h:panelGrid>
        </h:form>
    </f:view>

StudentBean.java

    //... getters and setters

public String listOfMarks(){
    student.marksListFromDb(stuNum);
    return null;
}

private List marksList= new ArrayList();

public List getMarksList() {       
    return marksList;
}

Please check my above code where I'm trying to get the values from database on button click (listOfMarks) and display them in the datatable. Using the above code it is not fetching the data.Kindly help me if I'm wrong doing wrong some where...

Some general things:

An action method in a commandButton should return void or String . The String is used for navigation. If you want to reload the same page return null .

You are mixing the getter method for your markList with an action method.

You should have an action method eg fillMarkList() that fills the list, returns a null String and reloads the current page and you should have a getter method getMarkList() that returns the list for the dataTable:

public String fillMarkList() {
   // fill the list:
   student.markListFromDb(stuNum);
   // reload current page:
   return null;
}

public List getMarkList() {
   return markList();
}

Your button then should call fillMarkList() as action method:

<h:commandButton id="getStuMarkList" 
                 value="Get Mark List" 
                 action="#{stuBean.fillMarkList}" />

UPDATE: Just noticed your usage of h:panelGrid . I tried your version and it renders as correct table. But I think correct usage is without columns. You have to define column number as attribute of h:panelGrid and simply put the containing elements inside the grid:

<h:panelGrid columns="3">
   <f:facet name="header">
       <h:outputText value="Student Mark List"/>
   </f:facet>
   <h:outputText value="Student Number : "></h:outputText>
   <h:inputText value="#{stuBean.stuNumber}"/>
   <h:commandButton id = "getStuMarkList" value="Get Mark List" 
                     action="#{stuBean.listOfMarks}" >
   </h:commandButton> </h:column>
</h:panelGrid>   

Although that "please check my code" doesn't generally work here, one thing I spotted:

public List getMarkList() {
 student.markListFromDb(stuNum);
 return markList;
}

You don't put anything into markList here, do you?

Edit: also note that those are quite equal in terms of execution: #{stuBean.getMarkList} and #{stuBean.markList} since the latter is likely to be translated to StudentBean.getMarkList() by the framework, as will be #{stuBean.getMarkList} .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM