@FacesConverter(value = "questionTagConverter", forClass = QuestionTag.class)
public class QuestionTagConverter implements Converter{
@Inject
EntityManager entityManager;
@Override
public Object getAsObject(FacesContext fc, UIComponent uic, String string) {
return entityManager.find(QuestionTag.class, Integer.parseInt(string));
}
@Override
public String getAsString(FacesContext fc, UIComponent uic, Object o) {
return ((QuestionTag)o).getId().toString();
}
}
The Java class which produces the EntityManager is as below.
import javax.enterprise.context.ConversationScoped;
import javax.enterprise.inject.Produces;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import org.jboss.seam.solder.core.ExtensionManaged;
public class EntityMgrProducer {
@ExtensionManaged
@Produces
@PersistenceUnit(name = "PU_NAMEHERE")
@ConversationScoped
EntityManagerFactory entityManager;
}
The related jars should be added to your web application. If you use Maven use the dependencies below.
<dependency>
<groupId>org.jboss.seam.faces</groupId>
<artifactId>seam-faces</artifactId>
<version>3.0.2.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.seam.persistence</groupId>
<artifactId>seam-persistence</artifactId>
<version>3.0.0.Final</version>
</dependency>
Example Usage - xhtml file
<h:panelGrid columns="2">
<p:selectOneMenu value="#{searchMultipleChoiceQuestionAction.questionDataModel.selTag}" converter="questionTagConverter">
<f:selectItems value="#{searchMultipleChoiceQuestionAction.tags}" var="qt" itemLabel="#{qt.qtag}" itemValue="#{qt}"/>
</p:selectOneMenu>
<p:commandButton update="searchresult" action="#{searchMultipleChoiceQuestionAction.search()}" value="Search"/>
</h:panelGrid>