我的项目有问题。 我有一个xhtml网站,其元素需要一个FacesConverter,但是在这个转换器中,我需要一个服务来注入,但是我在这个论坛中所有的红色并尝试过,返回null,所以有人可以帮助我吗?
那就是守则 网站:
<?xml version="1.0" encoding="UTF-8"?>
<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:c="http://java.sun.com/jstl/core" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:p="http://primefaces.org/ui" template="/WEB-INF/templates/default.xhtml">
<ui:define name="content">
<h:form>
<p:messages autoUpdate="true" />
<h:panelGrid columns="2" style="margin-bottom:10px" cellpadding="5">
<p:outputLabel for="seminar" value="Seminar" />
<p:selectOneMenu id="seminar" value="#{addSeminarPhaseController.seminar}" converter="seminarConverter" panelStyle="width:180px" effect="fade" var="s" style="width:160px" filter="true" filterMatchMode="startsWith">
<f:selectItem itemLabel="auswählen" itemValue="" />
<f:selectItems value="#{addSeminarController.allSeminare}" var="seminar" itemValue="#{seminar}" itemLabel="#{seminar.seminarName}"/>
<p:column style="width:10%">
<h:outputText styleClass="ui-theme ui-theme-#{s.id}" />
</p:column>
<p:column>
<h:outputText value="#{s.seminarName}" />
</p:column>
</p:selectOneMenu>
转换器:
import javax.enterprise.context.RequestScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.inject.Inject;
import javax.inject.Named;
import de.basf.gshev.sucw.beans.SeminarBean;
import de.basf.gshev.sucw.services.SUCWService;
import lombok.Getter;
import lombok.Setter;
@RequestScoped
@ManagedBean
@Named
@Getter
@Setter
@FacesConverter(forClass = SeminarBean.class)
public class SeminarConverter implements Converter {
@Inject
private SUCWService sucwService;
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if(value != null && value.trim().length()>0){
try {
if (sucwService == null)
sucwService = (SUCWService) context.getExternalContext().getApplicationMap().get("sucwService"); // Also isn't working
System.out.println("get " + context.getExternalContext().getApplicationMap().get("sucwService"));
return sucwService.findSeminarBeanById(Integer.parseInt(value));
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
value.toString();
if(value != null && value instanceof SeminarBean)
return String.valueOf(((SeminarBean) value).getId());
return null;
}
}
就是这样,我希望你能帮助我,谢谢你! 我也尝试在Controller中创建转换器,但后来我得到了这个错误:
Expression Error: Named Object: SeminarConverter not found.
转换器:
package de.basf.gshev.sucw.converter;
import javax.faces.bean.ManagedBean;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.inject.Inject;
import javax.inject.Named;
import de.basf.gshev.sucw.beans.SeminarBean;
import de.basf.gshev.sucw.services.SUCWService;
import lombok.Getter;
import lombok.Setter;
//@RequestScoped
@ManagedBean(name="seminarConverter")
@Named(value="seminarConverter")
@Getter
@Setter
public class SeminarConverter implements Converter {
@Inject
private SUCWService sucwService;
public SeminarConverter() {
// TODO Auto-generated constructor stub
}
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if(value != null && value.trim().length()>0){
try {
System.out.println("get " + context.getExternalContext().getApplicationMap().get(SUCWService.class));
return sucwService.findSeminarBeanById(Integer.parseInt(value));
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
value.toString();
if(value != null && value instanceof SeminarBean)
return String.valueOf(((SeminarBean) value).getId());
return null;
}
}
XHTML:
<p:selectOneMenu id="seminar" value="#{addSeminarPhaseController.seminar}" converter="#{addSeminarPhaseController.converter}" panelStyle="width:180px" effect="fade" var="s" style="width:160px" filter="true" filterMatchMode="startsWith">
<f:selectItem itemLabel="auswählen" itemValue="" />
<f:selectItems value="#{addSeminarController.allSeminare}" var="seminar" itemValue="#{seminar}" itemLabel="#{seminar.seminarName}"/>
<p:column style="width:10%">
<h:outputText styleClass="ui-theme ui-theme-#{s.id}" />
</p:column>
<p:column>
<h:outputText value="#{s.seminarName}" />
</p:column>
</p:selectOneMenu>
控制器:
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.inject.Inject;
import javax.inject.Named;
import org.primefaces.model.DualListModel;
import de.basf.gshev.sucw.beans.MitarbeiterBean;
import de.basf.gshev.sucw.beans.SeminarBean;
import de.basf.gshev.sucw.beans.SeminarPhaseBean;
import de.basf.gshev.sucw.services.SUCWService;
import lombok.Getter;
import lombok.Setter;
@RequestScoped
@Getter
@Setter
@Named
public class AddSeminarPhaseController implements Serializable {
/**
*
*/
private static final long serialVersionUID = -2356022465339358536L;
private List<SeminarBean> allSeminare;
private SeminarBean seminar=null;
private Date beginn;
private Date ende;
// private List<MitarbeiterBean>teilnehmer;
private DualListModel<MitarbeiterBean> teilnehmer;
private List<MitarbeiterBean> sourceTeilnehmer;
private List<MitarbeiterBean> targetTeilnehmer;
@Inject
private SUCWService sucwService;
@PostConstruct
public void init() {
allSeminare = new ArrayList<SeminarBean>(sucwService.getAllSeminare());
sourceTeilnehmer = sucwService.getAllMitarbeiter();
targetTeilnehmer = new ArrayList<MitarbeiterBean>();
teilnehmer = new DualListModel<MitarbeiterBean>(sourceTeilnehmer, targetTeilnehmer);
}
public Converter getConverter()
{
return FacesContext.getCurrentInstance().getApplication().createConverter("SeminarConverter");
}
public void addSeminarPhase() throws Exception{
// SeminarPhaseBean spb = new SeminarPhaseBean(seminar.getId(), beginn, ende);
if(seminar == null || beginn == null || ende == null || teilnehmer.getTarget().size() <1)
return;
SeminarPhaseBean spb = new SeminarPhaseBean();
spb.setBeginn(beginn);
spb.setEnde(ende);
spb.setSeminarId(seminar.getId());
spb.setTeilnehmer(teilnehmer.getTarget());
sucwService.persistSeminarphaseBean(spb);
}
}