TCM*_*TCM 10 java jsf facelets
我在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>
Run Code Online (Sandbox Code Playgroud)
我的豆子:
@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);
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,即使单击不同的编辑按钮,我总是在服务器日志中获得相同的索引.想象一下,有一个集合提供给数据表.我没有在bean中表明过.
如果我将范围从ViewScope更改为RequestScope,它可以正常工作.可能是什么问题@ViewScoped
?提前致谢 :)
编辑:
<h:column>
<h:commandButton value="Edit" actionListener="#{IndexBean.editBook}" />
</h:column>
Run Code Online (Sandbox Code Playgroud)
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());
}
}
Run Code Online (Sandbox Code Playgroud)
Bal*_*usC 14
您已经将<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.
}
Run Code Online (Sandbox Code Playgroud)
在<f:param>
也这里不需要.有关更多提示,请参阅此文章.
更新:我可以重现您的问题.这可能是一个错误@ViewScoped
.当bean设置为时@RequestScoped
,它按预期工作.此外,当您删除组件绑定并自己从视图中获取组件时,它将按预期工作.我已经提交了1658号问题.
归档时间: |
|
查看次数: |
57600 次 |
最近记录: |