Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

enumeration convertion in jsf

REDO LOGFeb 15 2013 — edited Feb 17 2013
Hi all
I am about dealing with enumerations in my academic JSF project
so, I have an entity Employee:
package com.entities;

import com.enumeration.Gender;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQuery;


@Entity
@NamedQuery(name="Employee.findAll", query="SELECT e FROM Employee e")
public class Employee implements Serializable {
    
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;
    @Column(name="nom", length=30)
    private String nom;
    @Enumerated(EnumType.STRING)
    @Column(length=1)
    private Gender gender;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getNom() {
        return nom;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }

    public Gender getGender() {
        return gender;
    }

    public void setGender(Gender gender) {
        this.gender = gender;
    }

    

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Employee)) {
            return false;
        }
        Employee other = (Employee) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.entities.Employee[ id=" + id + " ]";
    }
    
}
the enumeration:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.enumeration;

import java.io.Serializable;


public enum Gender implements Serializable{

    M("Male"), F("Female");
    private String description;

    private Gender(String description) {
        this.description = description;
    }

    public String getDescription() {
        return description;
    }
    

    
}
and the index page that shows the list of employees:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <f:view>
            <h:form>
                <h1><h:outputText value="List"/></h1>
                <h:dataTable value="#{employee.employees}" var="item" id="liste">
                    <h:column>
                        <f:facet name="header">
                            <h:outputText value="Id"/>
                        </f:facet>
                        <h:outputText value="#{item.id}"/>
                    </h:column>
                    <h:column>
                        <f:facet name="header">
                            <h:outputText value="Nom"/>
                        </f:facet>
                        <h:outputText value="#{item.nom}"/>
                    </h:column>
                    <h:column>
                        <f:facet name="header">
                            <h:outputText value="Gender"/>
                        </f:facet>
                        <h:outputText value="#{item.gender}"/>
                    </h:column>
                </h:dataTable>
            
                        <h1><h:outputText value="Create/Edit"/></h1>
                        <h:panelGrid columns="2">
                            <h:outputLabel value="Nom:" for="nom" />
                            <h:inputText id="nom" value="#{employee.newEmployee.nom}" title="Nom" />
                            
                            <h:outputLabel value="Gender:" for="gender" />
                            <h:selectOneMenu value="#{employeeBean.newEmployee.gender}" id="gender">
                                <f:selectItem itemLabel="Male" itemValue="Male"/>
                                <f:selectItem itemLabel="Female" itemValue="Female"/>
                            </h:selectOneMenu>
                            
                        </h:panelGrid>
                        <h:commandButton value="ajouter" action="index.xhtml" actionListener="#{employeeBean.ajouter}" />
                    </h:form>
        </f:view>
    </h:body>
</html>
with the ejb associated( not included here)

the problem I am facing is to convert the field selected in the selectOneMenu to an enumeration
I did some search and I found that I have to add a converter class the faces-config.xml file, but still facing the same problem

I am sure that I missed understood something with this but cannot detect what it is wrong with

thanks for any help
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 17 2013
Added on Feb 15 2013
11 comments
8,444 views