java ee - selectOneMenu Converter -


this question has answer here:

hi every body i'm working on primfaces pages , have make selectonemenu wich items database tried make way still have problem's converter

source codes folowing :

selectonemenu :

<p:selectonemenu id="devises" required="true" value="#{pret.devise}" effect="fade" converter="devise">       <f:selectitem itemlabel="select one" itemvalue="" />       <f:selectitems value="#{devise.listdevise()}" var="devise" itemlabel="#{devise.nomdevise}" itemvalue="#{devise}"/>   </p:selectonemenu> 

converter code:

@facesconverter(value = "devise") public class deviseconverter implements converter{        public static list<devise> devises = devise.listdevise();        public object getasobject(facescontext facescontext, uicomponent component, string submittedvalue) {           if (submittedvalue.trim().equals("")) {               return null;           } else {               try {                   int iddevise = integer.parseint(submittedvalue);                 (devise p : devises) {                       if (p.getiddevise()== iddevise) {                           return p;                       }                   }             } catch(numberformatexception exception) {                   throw new converterexception(new facesmessage(facesmessage.severity_error, "conversion error", "not valid devise"));               }           }         return null;       }        public string getasstring(facescontext facescontext, uicomponent component, object value) {           if (value == null || value.equals("")) {               return "";           } else {               return string.valueof(((devise) value).getiddevise());           }       } } 

the error code : "devises: validation error: value not valid"

your object devise need contain equals() , hashcode() methods.

also can use generic converter workd types of object , won't need write converter select lists.

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) {         (entry<object, string> entry : entities.entryset()) {             if (entry.getvalue().equals(uuid)) {                 return entry.getkey();             }         }         return null;     }  } 

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -