Mango
Mango

Reputation: 660

refresh a data in a datatable on the screen(jsp)

i have a datatable in my jsp, which fetches data from database. i've included a link in each row of the datatable which on click goes to another jsp(history.jsp) taking 2 input parameters.

Student.jsp

<h:dataTable id="dt1" value="#{Student.stuList}" var="stuList" >
                    <h:column>
                    <f:facet name="header">
                        <h:outputText value="Student Name"/>
                    </f:facet>
                    <h:outputText value="#{stuList.stuName}"></h:outputText>
                </h:column>
                <h:column>
                    <f:facet name="header">
                        <h:outputText value="Student Number"/>
                    </f:facet>
                    <h:outputText value="#{stuList.stuNum}"></h:outputText>
                </h:column>

                <h:column>
                    <f:facet name="header">
                        <h:outputText value="Student History"/>
                    </f:facet>
                    <h:form>
                        <h:commandLink id = "historyBtn" value="Student History" action="#{stuBean.showHistory}">
                            <f:param name="sName" value="#{stuList.stuName}" />
                            <f:param name="sNumber" value="#{stuList.stuNum}" />
                        </h:commandLink></h:form>
                </h:column>
            </h:dataTable>

history.jsp

<h:dataTable id="dt2" value="#{StuHistory.stuHistory}" var="stuHistory" >

                <h:column>
                    <f:facet name="header">
                        <h:outputText style=""value="Marks in test1" />
                    </f:facet>
                    <h:outputText style="" value="#{stuHistory.test1}" ></h:outputText>
                </h:column>
                <h:column>
                    <f:facet name="header">
                        <h:outputText value="Marks in test2"/>
                    </f:facet>
                    <h:outputText value="#{stuHistory.test2}" ></h:outputText>
                </h:column>
              </h:dataTable>
             <h:form>
           <h:commandButton id = "backBtn" value="Back to StuList" action="#{stuBean.backToStuList}">
             </h:commandButton> </h:form>

StudentBean

public String backToStuList() {
    return "stuList";
}

public String showHistory() {
    sHistory.getStuHistory();
    return "stuHistory";
}

StudentHistory.java (action class)

private List stuHistory = new ArrayList();
String sNum;
String sName;

public List getStuHistory() {
    getStuHistoryFromDb(sNum, sName);
    return stuHistory;
}    

faces-config.xml

<managed-bean>
    <managed-bean-name>stuBean</managed-bean-name>
    <managed-bean-class>
        com.klit.bean.StudentBean
    </managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<managed-bean>
    <managed-bean-name>Student</managed-bean-name>
    <managed-bean-class>
        com.klit.action.Student
    </managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<managed-bean>
    <managed-bean-name>StuHistory</managed-bean-name>
    <managed-bean-class>
        com.klit.action.StuHistory
    </managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<managed-bean>
<navigation-rule>
    <from-view-id>Student.jsp</from-view-id>
    <navigation-case>
        <from-action>#{stuBean.showHistory}</from-action>
        <from-outcome>stuHistory</from-outcome>
        <to-view-id>/StudentHistory.jsp</to-view-id>
    </navigation-case>
</navigation-rule>
<navigation-rule>
    <from-view-id>/StudentHistory.jsp</from-view-id>
    <navigation-case>
        <from-action>#{stuBean.backToStuList}</from-action>
        <from-outcome>stuList</from-outcome>
        <to-view-id>/Student.jsp</to-view-id>
    </navigation-case>
</navigation-rule>

The problem I'm facing is first time when i run student.jsp it works fine but when I click on history and come back the data is appended to the existing data i.e., I have double the list size now.each time i come back it is same issue. it is similar with my history.jsp too, there also the list is getting appended with existing data. Any ideas to refresh the page or datatable ????

Upvotes: 0

Views: 1307

Answers (1)

Matt Handy
Matt Handy

Reputation: 30025

That sounds like a problem of scopes or list reset in your bean.

You have to make sure that either your bean and the lists are recreated each time you switch pages. This can be done by using a view scoped or request scoped bean.

Or if you need a session scoped bean you can reset the lists in your action methods.

It depends on how your lists are populated. If you post the part of your bean that fills the list, we could go further.

UPDATE: You have to empty your list in your getStuHistoryFromDb method. Put this at the beginning of the method:

stuHistory = new ArrayList();

Then your list is empty before it is filled (again). Do the same for the other list.

Upvotes: 2

Related Questions