如何在java jsf中点击按钮显示数据库中的数据

发布于 2024-11-06 08:27:23 字数 2761 浏览 0 评论 0原文

<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;
}

请检查我上面的代码,我试图在单击按钮(listOfMarks)时从数据库获取值并将它们显示在数据表中。 使用上面的代码它不会获取数据。如果我在某些地方做错了,请帮助我......

<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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

假面具 2024-11-13 08:27:23

一些常规事项:

commandButton 中的操作方法应返回 voidString字符串用于导航。如果您想重新加载同一页面,请返回null

您将 markList 的 getter 方法与操作方法混合在一起。

您应该有一个操作方法,例如 fillMarkList() 来填充列表,返回一个空字符串并重新加载当前页面您应该有一个 getter 方法 getMarkList( ) 返回数据表的列表:

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

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

然后您的按钮应该调用 fillMarkList() 作为操作方法:

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

更新:
刚刚注意到您对 h:panelGrid 的使用。我尝试了你的版本,它呈现为正确的表格。但我认为正确的用法是没有列。您必须将列号定义为 h:panelGrid 的属性,然后将包含的元素简单地放入网格内:

<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>   

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 e.g. 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>   
难理解 2024-11-13 08:27:23

虽然“请检查我的代码”在这里通常不起作用,但我发现一件事:

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

你没有在此处将任何内容放入 markList 中,是吗?

编辑:另请注意,这些在执行方面是相当相同的: #{stuBean.getMarkList}#{stuBean.markList} 因为后者可能会被翻译为StudentBean.getMarkList() 由框架提供,如下 #{stuBean.getMarkList}

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}.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文