我写的客户转换器有问题。我从转换器调用我的 DAL 并将其作为托管属性注入。当我从我的一个 ui 组件调用转换器时,一切正常;但是,当我从另一个组件调用它时,注入的 DAL 为空。如果这是一个骗局,我深表歉意,我已经到处寻找答案。
我正在使用primefaces 3.5。
这是转换器:
@ManagedBean(name = "fanlistConverter")
public class FanlistConverter implements Converter, Serializable {
private List<Fanlist> fanlist;
@ManagedProperty(value="#{dAL}")
private DAL dAL;
private static Session session;
public FanlistConverter(){
}
@PostConstruct
public void init(){
session = dAL.getSession();
}
public Object getAsObject(FacesContext facesContext, UIComponent component, String submittedValue) {
if (!submittedValue.equals(null) && !submittedValue.equals("null")) {
Fanlist fanlist = new Fanlist();
try {
fanlist = (Fanlist)session.get(Fanlist.class, UUID.fromString(submittedValue));
} catch(NumberFormatException exception) {
System.out.println("error: " + exception.getMessage());
throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Not a valid player"));
}catch(Exception e){
System.out.println("error get as Object: " + e.getMessage());
}
}
return fanlist;
}
public String getAsString(FacesContext facesContext, UIComponent component, Object value) {
if (value == null || value.equals("")) {
return "";
} else {
return String.valueOf(((Fanlist) value).getId());
}
}
public DAL getdAL() {
return dAL;
}
public void setdAL(DAL dAL) {
this.dAL = dAL;
}
}
这是两个 ui 组件
这个有效:
<p:panelGrid columns="3">
<p:outputLabel for="fanlist" value="Subscription Opt-In List " />
<p:autoComplete id="fanlist" dropdown="true"
value="#{campaignWizard.selectedList}"
completeMethod="#{campaignWizard.complete}" var="f"
itemLabel="#{f.name}" itemValue="#{f}" converter="#{fanlistConverter}" />
<p:commandLink value="Add List"
actionListener="#{campaignWizard.addFanList}" update=":formbox2"
ajax="false" />
</p:panelGrid>
这个不行
<p:selectManyMenu id="fanlist2" value="#{blast.selectedLists}"
var="fl" showCheckbox="true" converter="#{fanlistConverter}"
style="width:250px;height:100px">
<f:selectItems value="#{blast.fanlists}" var="fanl"
itemLabel="#{fanl.name}" itemValue="#{fanl}" />
<p:column>
Name: #{fl.name}
</p:column>
<p:column>
Size: #{fl.name}
</p:column>
</p:selectManyMenu>
@ManagedBean(name = "blast")
@ViewScoped
public class BlastBean implements Serializable {
@ManagedProperty(value="#{globalVars}")
private GlobalVars globalVars;
@ManagedProperty(value="#{dAL}")
private DAL dAL;
@ManagedProperty(value="#{messaging}")
private Messaging messaging;
private User u;
private Blast blast;
private List<Fanlist> fanlists;
private List<Fanlist> selectedLists;
private List<Fanlisttoparent> fltops = new ArrayList<Fanlisttoparent>();
public BlastBean(){
}
@PostConstruct
public void init(){
fetchData();
}
public void fetchData(){
u = globalVars.getUser();
blast = new Blast();
fanlists = dAL.query("FROM Fanlist where client.id ='" + globalVars.getUser().getClient().getId() + "'");
}
...
public String sendBlast(){
System.out.println("blast:" + blast.getMessage());
dAL.upsert(blast);
final List<String> phoneNumbers = new ArrayList<String>();
List<Profile> profiles = new ArrayList<Profile>();
for(Fanlist fl: selectedLists){
Set<Fantolist> fantolist = fl.getFantolists();
for(Fantolist ftol: fantolist){
profiles.add(ftol.getFanprofile().getProfile());
}
for(Profile p: profiles){
phoneNumbers.add("+1"+ p.getPhonenumber());
}
}
...