转换为p:selectOneMenu错误JSF与EJB

时间:2014-12-10 04:09:17

标签: jsf primefaces ejb

我要转换的课程

@Entity
@Table(name = "mv_uf")
@SequenceGenerator(name = "sq_mv_uf", sequenceName = "sq_mv_uf", allocationSize = 1)
public class UF implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sq_mv_uf")
    @Column(name = "id")
    private Long id;

    @Column(name = "sigla_uf")
    private String siglaUF;

    @Column(name = "nome_uf")
    private String nomeUF;

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }

        if (obj == null) {
            return false;
        }

        if (getClass() != obj.getClass()) {
            return false;
        }

        UF other = (UF) obj;

        if (id == null) {
            if (other.id != null) {
                return false;
            }
        } else if (!id.equals(other.id)) {
            return false;
        }

        return true;
    }
    //get and set
}

我的转换器

@FacesConverter(value = "ufConverter", forClass = UF.class)
public class UFConverter implements Converter {

    @EJB
    private UF uf;
    private MapaEleitoralBeanLocal mapaEleitoralBean;

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {

        if (value != null && !value.isEmpty()) {
            if (!isLong(value)) {
                return "";
            }

            Long id = Long.parseLong(value);
            uf = (UF) mapaEleitoralBean.consultarPorChavePrimaria(uf, id);
            return uf;

        } else {
            return null;
        }
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (value != null && !"".equals(value)) {
            uf = (UF) value;
            return String.valueOf(uf.getId());
        }
        return "";
    }

    private boolean isLong(String valor) {
        try {
            long i = Long.parseLong(valor);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}

我的豆子

@Stateless
public class MapaEleitoralBean extends GenericDao implements MapaEleitoralBeanLocal {

    @Override
    public List<UF> listarTodosOsEstados() {
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT o FROM ").append(UF.class.getSimpleName()).append(" o ");
        sql.append(" order by o.nomeUF");

        List<UF> listaDeTodosOsUF = new ArrayList<UF>();
        listaDeTodosOsUF = (List<UF>) consultarPorQuery(sql.toString(), 0, 0);

        if (listaDeTodosOsUF == null || listaDeTodosOsUF.isEmpty()) {
            return null;
        }

        return listaDeTodosOsUF;
    }

    @Override
    public List<Municipio> listarMunicipiosPorUF(UF uf) {

        StringBuilder sql = new StringBuilder();
        sql.append("SELECT o FROM ").append(Municipio.class.getSimpleName()).append(" o ");
        sql.append("WHERE o.siglaUF = '").append(uf.getSiglaUF()).append("' order by o.nomeMunicipio");

        List<Municipio> listaDeMunicipiosPorUF = new ArrayList<Municipio>();
        listaDeMunicipiosPorUF = (List<Municipio>) consultarPorQuery(sql.toString(), 0, 0);

        if (listaDeMunicipiosPorUF == null || listaDeMunicipiosPorUF.isEmpty()) {

            return null;
        }

        return listaDeMunicipiosPorUF;
    }
}

我的ManagedBean

@ManagedBean(name = "mapaEleitoralControl")
@ViewScoped
public class MapaEleitoralControl {

    private static final long serialVersionUID = 1L;

    @EJB
    private MapaEleitoralBeanLocal mapaEleitoralBean;
    private List<UF> listaUF;
    private UF uf;

    public MapaEleitoralControl() {
        super();
    }

    @PostConstruct
    public void init() {
        listaUF = new ArrayList<UF>();
        listaUF = mapaEleitoralBean.listarTodosOsEstados();
        //mapaEleitoralBean.listarMunicipiosPorUF(uf);
        System.out.println("pare");

    }

    public void handleToggle(ToggleEvent event) {
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Toggled", "Visibility:" + event.getVisibility());
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }
    //get and sets
}

我的xhtml

<p:column>
    <p:selectOneMenu id="selectUF"
                     style="width:195px"
                     value="#{mapaEleitoralControl.uf}"
                     converter="ufConverter" >

        <f:selectItem itemLabel="Selecione a UF
                      " itemValue=""
                      noSelectionOption="true" />

        <f:selectItem value="#{mapaEleitoralControl.listaUF}"
                      var="uf"
                      itemLabel="#{uf.nomeUF}"
                      itemValue="#{uf}" />
    </p:selectOneMenu>
</p:column>

我对JSF,PrimeFaces,EJB和其他东西都不熟悉,我收到了这个错误,我不明白为什么。我已经浏览了谷歌和这里,我无法找到解决方案。

00:47:57,662 WARN  [org.jboss.modules] (MSC service thread 1-6) Failed to define class br.com.maxvoto.converter.EntityConverter in Module "deployment.maxvoto-ear.ear.maxvoto-ejb.jar:main" from Service Module Loader: java.lang.LinkageError: Failed to link br/com/maxvoto/converter/EntityConverter (Module "deployment.maxvoto-ear.ear.maxvoto-ejb.jar:main" from Service Module Loader)
    at org.jboss.modules.ModuleClassLoader.defineClass(ModuleClassLoader.java:487) [jboss-modules.jar:1.3.3.Final-redhat-1]
    at org.jboss.modules.ModuleClassLoader.loadClassLocal(ModuleClassLoader.java:277) [jboss-modules.jar:1.3.3.Final-redhat-1]
    at org.jboss.modules.ModuleClassLoader$1.loadClassLocal(ModuleClassLoader.java:92) [jboss-modules.jar:1.3.3.Final-redhat-1]
    at org.jboss.modules.Module.load

ModuleClass(Module.java:568) [jboss-modules.jar:1.3.3.Final-redhat-1]
....

1 个答案:

答案 0 :(得分:1)

您不应使用use @EJB注释。注入仅适用于托管bean。使用InitialContext获取目标组件。或者,将转换器设置为可以注入EJB的 beans

@ManagedBean
@RequestScoped
public class UFConverter implements Converter {
    @EJB
    private UF uf;
    // ...
}

JSF允许容器在对JSF应用程序进行访问之前,将对容器受管资源的引用注入到托管bean实例中。只有声明处于请求,会话或应用程序范围内的 beans 才能进行资源注入。

另请参阅:How to inject @EJB, @PersistenceContext, @Inject, @Autowired, etc in @FacesConverter?