如何在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;
}
请检查我上面的代码,我试图在单击按钮(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一些常规事项:
commandButton 中的操作方法应返回 void 或 String。 字符串用于导航。如果您想重新加载同一页面,请返回null。
您将 markList 的 getter 方法与操作方法混合在一起。
您应该有一个操作方法,例如
fillMarkList()
来填充列表,返回一个空字符串并重新加载当前页面您应该有一个 getter 方法getMarkList( )
返回数据表的列表:然后您的按钮应该调用
fillMarkList()
作为操作方法:更新:
刚刚注意到您对
h:panelGrid
的使用。我尝试了你的版本,它呈现为正确的表格。但我认为正确的用法是没有列。您必须将列号定义为 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 methodgetMarkList()
that returns the list for the dataTable:Your button then should call
fillMarkList()
as action method: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:虽然“请检查我的代码”在这里通常不起作用,但我发现一件事:
你没有在此处将任何内容放入 markList 中,是吗?
编辑:另请注意,这些在执行方面是相当相同的:
#{stuBean.getMarkList}
和#{stuBean.markList}
因为后者可能会被翻译为StudentBean.getMarkList()
由框架提供,如下#{stuBean.getMarkList}
。Although that "please check my code" doesn't generally work here, one thing I spotted:
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 toStudentBean.getMarkList()
by the framework, as will be#{stuBean.getMarkList}
.