问题描述
我正在尝试为我的selectonemenu列表编写一个转换器,以便可以从ManagedBean的getter getDatasetList()
中检索要显示的列表.我的ManagedBean是viewScoped.
I'm trying to write a converter for my selectonemenu list so the list that i want to display can be retrieved from a managedBean's getter getDatasetList()
.My managedBean is viewScoped.
这是转换器的代码:
@FacesConverter(value = "datasetConverter")
public class DatasetConverter implements Converter{
@ManagedProperty(value="#{projectCampaignManagementMB}")
private ProjectCampaignManagementMB campaignManagementMB;
@Override
public Object getAsObject(FacesContext context, UIComponent component,
String value) {
List <DataSet> datasetList=campaignManagementMB.getDatasetList();
DataSet dataSet;
String dataSetName;
if (datasetList!=null){
for(int i=0 ;i<datasetList.size();i++)
{
dataSet=datasetList.get(i);
dataSetName=dataSet.getName();
if (dataSetName.equals(value))
return dataSet;
System.out.println("getasobject dataset"+dataSet.getName());
}
}
return null;
}
@Override
public String getAsString(FacesContext context, UIComponent component,
Object value) {
if (value==null) return "";
System.out.println("getastring dataset"+((DataSet) value).getName());
return ((DataSet) value).getName();
}
但是我在此行上收到一个java.lang.NullPointerException
But i'm getting a java.lang.NullPointerException on this line
List <DataSet> datasetList=campaignManagementMB.getDatasetList();
因此,注入的managedBean为Null,
So the injected managedBean is Null,
我尝试了没有意义但可以解决的问题,但是我想做正确的事情.我用了这个
i tried something that has no sense but it works ,but i want something correct .i used this
@ManagedProperty(value="#{projectCampaignManagementMB}")
private ProjectCampaignManagementMB campaignManagementMB =
new ProjectCampaignManagementMB();
代替
@ManagedProperty(value="#{projectCampaignManagementMB}")
private ProjectCampaignManagementMB campaignManagementMB
任何帮助将不胜感激
推荐答案
当您要在转换器中使用托管属性时,请使转换器成为托管bean,就像这样
When you want to use managed properties inside your convertor make your convertor a managed bean, like this
@ManagedBean
@SessionScoped
public class DatasetConverter implements Converter{
比起将其用作xhtml页面中的转换器,只需使用绑定
Than to use it as convertor in your xhtml page just use binding
像这样
<h:inputText converter="#{datasetConverter}"
这篇关于在转换器中注入ManagedBean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!