我无法从SelectOneMenu中获得选中的项目。我向菜单提供了一个Set,并希望用户从中选择一个。我将菜单放入一个表单中,因此我有一个commandButton,我打算使用它来执行选择。这个实现给出了这个错误:java.util。LinkedHashSet不能被强制转换为com.gestion. project .domain. project我想得到一个项目对象,而不是项目的列表下面是我的trace:
mai 18, 2014 1:06:54 PM com.sun.faces.lifecycle.ProcessValidationsPhase execute
Avertissement: java.util.LinkedHashSet cannot be cast to com.gestion.projet.domain.Projet
java.lang.ClassCastException: java.util.LinkedHashSet cannot be cast to com.gestion.projet.domain.Projet
at com.gestion.projet.dao.ProjetDAOImpl.findProjetByNom(ProjetDAOImpl.java:124)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy42.findProjetByNom(Unknown Source)
at com.gestion.projet.web.jsf.PhaseComponentImpl.getAsObject(PhaseComponentImpl.java:298)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:196)
at $Proxy56.getAsObject(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:131)
at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:119)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy9.getAsObject(Unknown Source)
at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:167)
at com.sun.faces.renderkit.html_basic.MenuRenderer.convertSelectOneValue(MenuRenderer.java:198)
at com.sun.faces.renderkit.html_basic.MenuRenderer.getConvertedValue(MenuRenderer.java:315)
at javax.faces.component.UIInput.getConvertedValue(UIInput.java:1023)
at javax.faces.component.UIInput.validate(UIInput.java:953)
at javax.faces.component.UIInput.executeValidate(UIInput.java:1204)
at javax.faces.component.UIInput.processValidators(UIInput.java:693)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1081)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1081)
at org.primefaces.component.panel.Panel.processValidators(Panel.java:281)
at javax.faces.component.UIForm.processValidators(UIForm.java:240)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1081)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1081)
at org.primefaces.component.layout.Layout.processValidators(Layout.java:233)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1081)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1081)
at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1159)
at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:72)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:308)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:113)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
mai 18, 2014 1:06:54 PM org.apache.catalina.core.StandardWrapperValve invoke
Grave: Servlet.service() for servlet [Faces Servlet] in context with path [/HCP] threw exception [java.util.LinkedHashSet cannot be cast to com.gestion.projet.domain.Projet] with root cause
java.lang.ClassCastException: java.util.LinkedHashSet cannot be cast to com.gestion.projet.domain.Projet
at com.gestion.projet.dao.ProjetDAOImpl.findProjetByNom(ProjetDAOImpl.java:124)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy42.findProjetByNom(Unknown Source)
at com.gestion.projet.web.jsf.PhaseComponentImpl.getAsObject(PhaseComponentImpl.java:298)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:196)
at $Proxy56.getAsObject(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:131)
at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:119)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy9.getAsObject(Unknown Source)
at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:167)
at com.sun.faces.renderkit.html_basic.MenuRenderer.convertSelectOneValue(MenuRenderer.java:198)
at com.sun.faces.renderkit.html_basic.MenuRenderer.getConvertedValue(MenuRenderer.java:315)
at javax.faces.component.UIInput.getConvertedValue(UIInput.java:1023)
at javax.faces.component.UIInput.validate(UIInput.java:953)
at javax.faces.component.UIInput.executeValidate(UIInput.java:1204)
at javax.faces.component.UIInput.processValidators(UIInput.java:693)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1081)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1081)
at org.primefaces.component.panel.Panel.processValidators(Panel.java:281)
at javax.faces.component.UIForm.processValidators(UIForm.java:240)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1081)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1081)
at org.primefaces.component.layout.Layout.processValidators(Layout.java:233)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1081)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1081)
at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1159)
at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:72)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:308)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:113)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
下面是生成selectOneMenu的xhtml部分:
<h:selectOneMenu id="selectProjet" value="#{PhaseComponent.projet}" required="true" converter="#{PhaseComponent}">
<f:selectItem itemLabel="Select One" itemValue="" />
<f:selectItems value="#{PhaseComponent.findNomProjets()}" var="projet" itemValue="#{projet}" itemLabel="#{ProjetComponent.projet.nomprojet}" />
</h:selectOneMenu>
这个是转换器类
@Transactional
public Projet findProjetByName(String nomprojet)
{
return (Projet)(projetDAO.findProjetByNom(nomprojet));
}
public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
projet=(Projet) projetDAO.findProjetByNom(arg2);
return projet ;
}
当我添加转换器时,我得到了这个错误这是PhaseComponent类:
@Scope("session")
@Component("PhaseComponent")
public class PhaseComponentImpl implements PhaseComponent ,Converter {
private Phase phase;
private Projet projet;
private Tache taches;
private Livrable livrables;
@Autowired
private LivrableDAO livrableDAO;
@Autowired
private PhaseDAO phaseDAO;
@Autowired
private ProjetDAO projetDAO;
@Autowired
private TacheDAO tacheDAO;
@Autowired
private PhaseService phaseService;
private Set<SelectItem> selected;
/**
*/
/**
*/
public Set<SelectItem> getSelectOneItemsProj(){
this.selected= new LinkedHashSet<SelectItem>();
Set<Projet> projets=projetDAO.findAllProjets();
for(Projet proj:projets)
{
SelectItem selectItem=new SelectItem(proj.getIdprojet(),proj.getNomprojet());
this.selected.add(selectItem); }
return selected;
}
public PhaseComponentImpl() {
}
@Transactional
public String selectPhaseTaches(Integer phase_idphase, Integer related_taches_idtache) {
taches = tacheDAO.findTacheByPrimaryKey(related_taches_idtache, -1, -1);
return "/jsf/phase/taches/viewTaches.xhtml";
}
@Transactional
public String editPhaseLivrables(Integer phase_idphase, Integer related_livrables_idlivrable) {
livrables = livrableDAO.findLivrableByPrimaryKey(related_livrables_idlivrable, -1, -1);
return "/jsf/phase/livrables/editLivrables.xhtml";
}
@Transactional
public String newPhaseTaches(Integer phase_idphase) {
taches = new Tache();
return "/jsf/phase/taches/createTaches.xhtml";
}
@Transactional
public Tache getTaches() {
return taches;
}
@Transactional
public String savePhaseProjet(Integer phase_idphase, Projet projet) {
phase = phaseService.savePhaseProjet(phase_idphase, projet);
return "/jsf/phase/viewPhase.xhtml";
}
@Transactional
public Projet getPhaseProjet(Integer idphaseKey) {
return phaseDAO.findPhaseByPrimaryKey(idphaseKey).getProjet();
}
@Transactional
public Projet getProjet() {
return projet;
}
@Transactional
public String confirmDeletePhaseProjet(Integer phase_idphase, Integer related_projet_idprojet) {
projet = projetDAO.findProjetByPrimaryKey(related_projet_idprojet);
return "/jsf/phase/projet/deleteProjet.xhtml";
}
@Transactional
public String newPhaseLivrables(Integer phase_idphase) {
livrables = new Livrable();
return "/jsf/phase/livrables/createLivrables.xhtml";
}
@Transactional
public String confirmDeletePhaseTaches(Integer phase_idphase, Integer related_taches_idtache) {
taches = tacheDAO.findTacheByPrimaryKey(related_taches_idtache);
return "/jsf/phase/taches/deleteTaches.xhtml";
}
@Transactional
public List<Livrable> listPhaseLivrables(Integer idphaseKey) {
return new java.util.ArrayList<Livrable>(phaseDAO.findPhaseByPrimaryKey(idphaseKey).getLivrables());
}
@Transactional
public String deletePhaseProjet(Integer phase_idphase, Integer related_projet_idprojet) {
phase = phaseService.deletePhaseProjet(phase_idphase, related_projet_idprojet);
return "/jsf/phase/viewPhase.xhtml";
}
@Transactional
public String confirmDeletePhaseLivrables(Integer phase_idphase, Integer related_livrables_idlivrable) {
livrables = livrableDAO.findLivrableByPrimaryKey(related_livrables_idlivrable);
return "/jsf/phase/livrables/deleteLivrables.xhtml";
}
@Transactional
public List<Phase> listPhases() {
return new java.util.ArrayList<Phase>(phaseService.loadPhases());
}
@Transactional
public String savePhaseTaches(Integer phase_idphase, Tache tache) {
phase = phaseService.savePhaseTaches(phase_idphase, tache);
return "/jsf/phase/viewPhase.xhtml";
}
@Transactional
public Livrable getLivrables() {
return livrables;
}
@Transactional
public String savePhaseLivrables(Integer phase_idphase, Livrable livrable) {
phase = phaseService.savePhaseLivrables(phase_idphase, livrable);
return "/jsf/phase/viewPhase.xhtml";
}
@Transactional
public String editPhase(Integer idphaseKey) {
phase = phaseDAO.findPhaseByPrimaryKey(idphaseKey);
return "/jsf/phase/editPhase.xhtml";
}
@Transactional
public String editPhaseProjet(Integer phase_idphase, Integer related_projet_idprojet) {
projet = projetDAO.findProjetByPrimaryKey(related_projet_idprojet, -1, -1);
return "/jsf/phase/projet/editProjet.xhtml";
}
@Transactional
public String selectPhaseLivrables(Integer phase_idphase, Integer related_livrables_idlivrable) {
livrables = livrableDAO.findLivrableByPrimaryKey(related_livrables_idlivrable, -1, -1);
return "/jsf/phase/livrables/viewLivrables.xhtml";
}
@Transactional
public String savePhase(Phase phase) {
phaseService.savePhase(phase);
return "/jsf/phase/listPhases.xhtml";
}
@Transactional
public String confirmDeletePhase(Integer idphaseKey) {
phase = phaseDAO.findPhaseByPrimaryKey(idphaseKey);
return "/jsf/phase/deletePhase.xhtml";
}
@Transactional
public String editPhaseTaches(Integer phase_idphase, Integer related_taches_idtache) {
taches = tacheDAO.findTacheByPrimaryKey(related_taches_idtache, -1, -1);
return "/jsf/phase/taches/editTaches.xhtml";
}
@Transactional
public String selectPhase(Integer idphaseKey) {
phase = phaseDAO.findPhaseByPrimaryKey(idphaseKey);
return "/jsf/phase/viewPhase.xhtml";
}
@Transactional
public String selectPhaseProjet(Integer phase_idphase, Integer related_projet_idprojet) {
projet = projetDAO.findProjetByPrimaryKey(related_projet_idprojet, -1, -1);
return "/jsf/phase/projet/viewProjet.xhtml";
}
@Transactional
public Phase getPhase() {
return phase;
}
@Transactional
public String newPhaseProjet(Integer phase_idphase) {
projet = new Projet();
return "/jsf/phase/projet/createProjet.xhtml";
}
@Transactional
public String deletePhaseLivrables(Integer phase_idphase, Integer related_livrables_idlivrable) {
phase = phaseService.deletePhaseLivrables(phase_idphase, related_livrables_idlivrable);
return "/jsf/phase/viewPhase.xhtml";
}
@Transactional
public List<Tache> listPhaseTaches(Integer idphaseKey) {
return new java.util.ArrayList<Tache>(phaseDAO.findPhaseByPrimaryKey(idphaseKey).getTaches());
}
@Transactional
public String deletePhase(Integer idphaseKey) {
Phase phase = phaseDAO.findPhaseByPrimaryKey(idphaseKey);
phaseService.deletePhase(phase);
return "/jsf/phase/listPhases.xhtml";
}
@Transactional
public String newPhase() {
phase = new Phase();
return "/jsf/phase/createPhase.xhtml";
}
@Transactional
public String deletePhaseTaches(Integer phase_idphase, Integer related_taches_idtache) {
phase = phaseService.deletePhaseTaches(phase_idphase, related_taches_idtache);
return "/jsf/phase/viewPhase.xhtml";
}
@Transactional
public void affectPhaseProjet(Integer phase_idphase_3,
Integer phase_idprojet) {
projet = projetDAO.findProjetByPrimaryKey(phase_idprojet, -1, -1);
phase.setProjet(projet);
}
@Transactional
public Projet findNomProjet()
{
return (Projet) projetDAO.findNomProjets();
}
public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
projet=(Projet) projetDAO.findProjetByNomprojet(arg2);
return projet ;
}
public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
// TODO Auto-generated method stub
return null;
}
public List<Projet> findNomProjets() {
// TODO Auto-generated method stub
return projetDAO.findNomProjets();
}
}
这是我的XHTML使用了这个类也使用了转换器
<h:panelGrid id="detail" columns="2" styleClass="grid" columnClasses="label,value">
<h:outputText value="#{phasemsgs['phase.datedebut.title']}:" />
<p:calendar value="#{PhaseComponent.phase.datedebut}" id="datedebut" converter="dateConverterForCalendar"/>
<h:outputText value="#{phasemsgs['projet.datefineffective.title']}:" />
<p:calendar value="#{PhaseComponent.phase.datefin}" id="datefin" converter="dateConverterForCalendar"/>
<h:outputText value="#{phasemsgs['phase.description.title']}:" />
<h:inputText id="phase_description" value="#{PhaseComponent.phase.description}" required="false" label="phase_description" />
<h:outputLabel value="Projet: *" />
<h:selectOneMenu id="selectProjet" value="#{PhaseComponent.projet}" required="true" converter="#{entityConverter}">
<f:selectItem itemLabel="Select One" itemValue="" />
<f:selectItems value="#{PhaseComponent.findNomProjets()}" />
</h:selectOneMenu>
<h:panelGroup>
<p:commandButton image="save" ajax="false" style="margin-right:20px;" value="#{phasemsgs['navigation.save']}" action="#{PhaseComponent.savePhase(PhaseComponent.phase)}"/>
</h:panelGroup>
</h:panelGrid>
您不需要创建自己的converter
。
如果你想要一个generic converter
,那么Omnifaces SelectItemsConverter
是最好的。
Omni Faces Converter
如果你不想使用Omnifaces
,那么使用下面的通用转换器
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import java.util.WeakHashMap;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
@FacesConverter(value = "entityConverter")
public class EntityConverter implements Converter {
private static Map<Object, String> entities = new WeakHashMap<Object, String>();
@Override
public String getAsString(FacesContext context, UIComponent component, Object entity) {
synchronized (entities) {
if (!entities.containsKey(entity)) {
String uuid = UUID.randomUUID().toString();
entities.put(entity, uuid);
return uuid;
} else {
return entities.get(entity);
}
}
}
@Override
public Object getAsObject(FacesContext context, UIComponent component, String uuid) {
for (Entry<Object, String> entry : entities.entrySet()) {
if (entry.getValue().equals(uuid)) {
return entry.getKey();
}
}
return null;
}
}