Java 如何在 JSF 数据表中获取选定的行索引?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2779320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get selected row index in JSF datatable?
提问by TCM
I have a databale on index.xhtml
我在 index.xhtml 上有一个数据库
<h:dataTable style="border: solid 2px black;"
value="#{IndexBean.bookList}" var="item"
binding="#{IndexBean.datatableBooks}">
<h:column>
<h:commandButton value="Edit" actionListener="#{IndexBean.editBook}">
<f:param name="index" value="#{IndexBean.datatableBooks.rowIndex}"/>
</h:commandButton>
</h:column>
</h:dataTable>
My bean:
我的豆子:
@ManagedBean(name="IndexBean")
@ViewScoped
public class IndexBean implements Serializable {
private HtmlDataTable datatableBooks;
public HtmlDataTable getDatatableBooks() {
return datatableBooks;
}
public void setDatatableBooks(HtmlDataTable datatableBooks) {
this.datatableBooks = datatableBooks;
}
public void editBook() throws IOException{
int index = Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("index").toString());
System.out.println(index);
}
}
My problem is that I always get the same index in server log even though I click the different edit buttons. Imagine that there is one collection which is supplied to the datatable. I have not shown that in bean.
我的问题是,即使我单击了不同的编辑按钮,我总是在服务器日志中获得相同的索引。想象一下,有一个提供给数据表的集合。我没有在 bean 中显示它。
If I change scope from ViewScope to RequestScope it works fine. What can be the problem with @ViewScoped
? Thanks in advance :)
如果我将范围从 ViewScope 更改为 RequestScope,它可以正常工作。可能有什么问题@ViewScoped
?提前致谢 :)
EDIT:
编辑:
<h:column>
<h:commandButton value="Edit" actionListener="#{IndexBean.editBook}" />
</h:column>
public void editBook(ActionEvent ev) throws IOException{
if (ev.getSource() != null && ev.getSource() instanceof HtmlDataTable) {
HtmlDataTable objHtmlDataTable = (HtmlDataTable) ev.getSource();
System.out.println(objHtmlDataTable.getRowIndex());
}
}
采纳答案by BalusC
You've already bound the <h:dataTable>
component to the bean. All you need to do is:
您已经将<h:dataTable>
组件绑定到 bean。您需要做的就是:
public void editBook() throws IOException{
int index = datatableBooks.getRowIndex(); // Actually not interesting info.
Book book = (Book) datatableBooks.getRowData(); // This is what you want.
}
The <f:param>
is also not needed here. For more hints also see this article.
在<f:param>
也这里不需要。有关更多提示,请参阅这篇文章。
Update: I can reproduce your problem. This is likely a bug with @ViewScoped
. When the bean is set to @RequestScoped
, it works as expected. Also when you remove the component binding and obtain the component from the viewroot yourself, it works as expected. I've filed issue 1658about this.
更新:我可以重现您的问题。这很可能是@ViewScoped
. 当 bean 设置为 时@RequestScoped
,它会按预期工作。此外,当您删除组件绑定并自己从 viewroot 获取组件时,它会按预期工作。我已经提交了关于这个的issue 1658。
回答by Romain Linsolas
What you can do is to use the [getRowData()][1]
method on the Java bean to directly get the object located on the line that hosts the button on which the user clicked.
您可以做的是使用[getRowData()][1]
Java bean 上的方法直接获取位于承载用户单击的按钮的行上的对象。
An example of code:
代码示例:
public void editBook(ActionEvent evt) {
// We get the table object
HtmlDataTable table = getParentDatatable((UIComponent) evt.getSource());
// We get the object on the selected line.
Object o = table.getRowData();
// Eventually, if you need the index of the line, simply do:
int index = table.getRowIndex();
// ...
}
// Method to get the HtmlDataTable.
private HtmlDataTable getParentDatatable(UIComponent compo) {
if (compo == null) {
return null;
}
if (compo instanceof HtmlDataTable) {
return (HtmlDataTable) compo;
}
return getParentDataTable(compo.getParent());
}
Edit编辑
The JSF code now looks like:
JSF 代码现在看起来像:
<h:commandButton value="Edit" actionListener="#{IndexBean.editBook}"/>
In addition, do not forget to change the signature of editBook()
method, by setting a javax.faces.event.ActionEvent
argument.
此外,不要忘记editBook()
通过设置javax.faces.event.ActionEvent
参数来更改方法的签名。
回答by amanas
If you use EL 2.2, for example with Tomcat7 you can try
如果您使用 EL 2.2,例如使用 Tomcat7,您可以尝试
<h:commandLink action="#{IndexBean.editBook(item)}" immediate="true">
I hope to help
我希望有所帮助