【发布时间】:2011-08-31 18:35:45
【问题描述】:
我有一个视图范围的 bean ManageFoo.java:
@ManagedBean(name = "ManageFoo")
@ViewScoped
public class ManageFoo {
@ManagedProperty(value = "#{currentRow}")
private Foo currentRowBean;
.
.
public void setCurrentRowBean(Foo foo) {...}
public Foo getCureentRowBean() {...}
public void edit(ActionEvent e) {
getCurrentRowBean();
.
.
}
}
然后我有 facelets 文件 ManageFoo.xhtml:
<h:dataTable value=#{ManageFoo.foos} var="currentRow">
<h:column>
<h:commandLink actionListener="#{ManageFoo.edit}"/>
.
.
但是,当单击“commandLink”时,“getCurrentRowBean”返回 null。
我使用 FacesContext 和 getBean("currentRow") 辅助方法可以正常工作,但我正在尝试通过使用 ManagedProperty、ManagedBean 等来“JSF 2ify”我的 Web 应用程序。我只是没有正确实现它还是我是否试图以没有意义的方式使用 ManageProperty?
根据 Balus 的反馈,他的解决方案适用于我的操作方法,但我无法让它适用于返回值的方法(例如布尔值)。以下方法用于每一行的一堆“渲染”属性。例如,如果您正在编辑某一行,则其他行上的所有其他编辑按钮都会消失,并且当前行的编辑按钮会被取消编辑按钮和保存按钮所取代。所以,这个方法每一行的返回结果都不一样:
public boolean isCurrentRowEditing() {
if(getCurrentRowDataBean().equals(getCurrentDataEditing())) {
return true;
} else {
return false;
}
}
由于我试图在任何地方消除“getCurrentRowDataBean()”方法,我需要一种方法来实现它。
【问题讨论】: