p:selectManyMenu report validation error (illegal select options) [duplicate] - jsf

This question already has answers here:
Validation Error: Value is not valid
(3 answers)
Closed 7 years ago.
I use primefaces's p:selectManyMenu. When I submit the value, it always report validation error Illegal selection options. Can you help to solve this issue?
JSF code:
<h:outputLabel for="scenario" value="GivenStories:" />
<p:selectManyMenu id="givenstoryselect" value="#{anotherDynaFormController.selectedGivenStory}" converter="scenarioConverter" var="g" style="width:500px;height:70px;align:left;" showCheckbox="true" >
<f:selectItems value="#{anotherDynaFormController.givenStorys}" var="scenario" itemLabel="#{scenario.name}" itemValue="#{scenario}" />
<p:column>#{g.name}</p:column>
</p:selectManyMenu>
Bean class:
//converter
#FacesConverter(value="scenarioConverter")
public class ScenarioConverter implements Converter {
public static List<Scenario> scenarioDB = new ArrayList<Scenario>();
private ScenarioDao scenarioDao = new ScenarioDao();
public Object getAsObject(FacesContext facesContext, UIComponent component, String submittedValue) {
if (submittedValue.trim().equals("")) {
return null;
} else {
try {
int number = Integer.parseInt(submittedValue);
scenarioDB = scenarioDao.findAll();
for (Scenario p : scenarioDB) {
if (p.getId() == number) {
return p;
}
}
} catch(NumberFormatException exception) {
throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Not a valid scenario"));
}
}
return null;
}
public String getAsString(FacesContext facesContext, UIComponent component, Object value) {
if (value == null) {
return null;
} else {
return String.valueOf(((Scenario) value).getId());
}
}
}
#ManagedBean
#ViewScoped
public class AnotherDynaFormController extends SuperBean implements Serializable {
private List<Scenario> givenStorys;
private List<Meta> selectedMetas;
private List<Scenario> selectedGivenStory;
private List<ProjectTree> parents;
public DynaFormModel getModel() {
givenStorys = scenarioDao.findAll();
...
}
...
}

Try this generic converter and see if problem still there .
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;
}
}

thanks a lot. I have find the reason of this issue. I should overwrite the equals and hashcode method in this object. Then it works.
#Override
public boolean equals(Object obj) {
if(obj == null)
return false;
if(!(obj instanceof Scenario))
return false;
return ((Scenario)obj).getId().equals(this.id);
}
#Override
public int hashCode() {
int hash = 1;
return hash * 31 + name.hashCode();
}

Related

Primefaces OrderList and Converter: getAsObject() called with "[object Object]" String

This can be seen as a continuation of my other question.
In my backend view, I have a list of some POJO. The POJO is converted to JSON with its toString() method:
BackingView.java
#Getter #Setter private List<SomePOJO> pojoList = ...
SomePojo.java
#EqualsAndHashCode(callSuper = false, of = {"id"})
public class SomePOJO implements Serializable {
private static final long serialVersionUID = 1L;
#Getter #Setter private Long id;
#Getter #Setter private Long date;
#Getter #Setter private Date name;
....
#Override
public String toString() {
ObjectMapper mapper = new ObjectMapper();
try {
return mapper.writeValueAsString(this);
} catch (JsonProcessingException e) {
e.printStackTrace();
return null;
}
}
//making a readable string-representation of the object to display on the orderList
public String toStringOrderlistDisplay() {
return "Pojo with id " + id + "and so on... "
}
In my frontend, I want to allow the user to sort this pojo-list using Primefaces orderList :
<p:orderList id="ordList" widgetVar="ordList"
value="#{backingview.pojoList }" var="rfg"
controlsLocation="left" responsive="true" itemValue="#{rfg}"
itemLabel="#{rfg.toStringOrderlistDisplay()}" converter="#{pojoConverter}">
</p:orderList>
PojoConverter.java
#Named
#FacesConverter(value = "pojoConverter")
public class PojoConverter implements Converter {
#Override
public Pojo getAsObject(FacesContext context, UIComponent component, String value) {
if (value != null && value.trim().length() > 0) {
try {
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(value, RechtsaktFallgeschichte.class);
}
catch (NumberFormatException | IOException ex) {
ex.printStackTrace();
throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Not a valid Pojo."));
}
}
else { return null; }
}
#Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
SomePOJO r = (SomePOJO) value;
if (r != null) {
return r.toString();
}
else { return null; }
}
}
Converting to JSON seems to work just fine, if I print the output of the getAsString() method, everything looks as you would expect. However, when the getAsObject() method is called, the value-String-parameter always contains "[object Object]" instead of the POJOs JSON representation. What am I doing wrong here?
I don't know if it is possible to achieve what you are trying to do, but a better way (and the working one) is to pass the POJO id in the getAsString() method, and convert it back to Pojo using a cache map Map<Long, Pojo> pojoCache.
The converter should look like this:
PojoConverter.java
#Named
#FacesConverter(value = "pojoConverter")
public class PojoConverter implements Converter<Pojo> {
#Inject
private PojoService pojoService;
#Override
public Pojo getAsObject(FacesContext context, UIComponent component, String value) {
if (value != null && value.trim().length() > 0) {
return pojoService.getPojoCache().get(Long.valueOf(value));
} else {
return null;
}
}
#Override
public String getAsString(FacesContext context, UIComponent component, Pojo value) {
if (value != null) {
return String.valueOf(value.getId());
} else {
return null;
}}
}
For a more detailed explanation check this: p:orderList converter getAsObject() doesn't call Object.toString()

Cannot add object(s) in an arrayList of object using selectManyCheckbox

I would like to add one or more object from a list of object into an other empty list. I am sure the first list is already initialized as I am able to print it in the p:selectManyCheckbox.
I think it does not copy the object Comite into the list but just the string as I get the error : java.lang.ClassCastException: java.lang.String cannot be cast to com.primasatis.jsf.model.Comite
Thus how can I copy it into the list ?
public class Comite {
private int idComite;
private int idPerimetre;
private String comite;
public Comite(int idComite, int idPerimetre, String comite) {
super();
this.idComite = idComite;
this.idPerimetre = idPerimetre;
this.comite = comite;
}
}
public static boolean addUserComiteRelation(int idUser, List<Comite> comites) {
System.out.println(comites.size()); // print the good number
for(int i=0; i<comites.size();i++) {
System.out.println(comites.get(i).comite); //error here
}
}
<h:form>
<p:outputLabel for="Comites" value="Comités" />
<p:selectManyCheckbox id="Comites" value="#{utilisateur.comites}"
required="true"
requiredMessage="L'utilisateur doit appartenir à au moins un comité">
<f:selectItems value="#{sessionScope.utilisateur.comites}" />
</p:selectManyCheckbox>
<h:message for="Comites"></h:message>
</h:form>
really quick view of my class utilisateur (#Named and #SessionScoped)
public class Utilisateur implements Serializable {
private Perimetre perimetreUtilisateur = null;
private List<Comite> comites = null;
#PostConstruct
private void init() {
perimetreUtilisateur = new Perimetre();
comites = new ArrayList<>();
}
}
The usual way to use p:selectManyCheckbox is to provide a List of javax.faces.model.SelectItem objects.
If you want to use your own Pojos as options in p:selectManyCheckbox, then you would have to write a Converter ( javax.faces.convert.FacesConverter ).
For Example, your converter would look like :
In My ManagedBean(Example: ComiteManagedBean) class I would keep all Comite instances in a MAP(Example: comiteMap) with Comite.idComite as Key.
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
#FacesConverter("comiteConverter")
public class ComiteConverter implements Converter {
public Object getAsObject(FacesContext fc, UIComponent uic, String value) {
if(value != null && value.trim().length() > 0) {
try {
Comite comite = (ComiteManagedBean) fc.getExternalContext().getApplicationMap().get("ComiteManagedBean");
return service.getComiteMap().get(Integer.parseInt(value));
} catch(NumberFormatException e) {
throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Not a valid theme."));
}
}
else {
return null;
}
}
public String getAsString(FacesContext fc, UIComponent uic, Object object) {
if(object != null) {
return ((Comite) object).getComite();
}
else {
return null;
}
}
}

SimpleJSFNavigationHandler cannot be cast to javax.faces.application.ConfigurableNavigationHandler

I'm migrating a JSF 1.2 project to JSF 2 and PrimeFaces 6 with Ultima layout.
When using Ultima layout, I get the below exception:
SimpleJSFNavigationHandler cannot be cast to javax.faces.application.ConfigurableNavigationHandler.
How to fix it?
Below is the SimpleJSFNavigationHandler.
import java.io.IOException;
import javax.faces.FacesException;
import javax.faces.application.NavigationHandler;
import javax.faces.application.ViewHandler;
import javax.faces.component.UIViewRoot;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import org.springframework.web.jsf.DecoratingNavigationHandler;
public class SimpleJSFNavigationHandler extends DecoratingNavigationHandler {
public static final String DEFAULT_REDIRECT_PREFIX = "redirect:";
public static final String DEFAULT_FORWARD_PREFIX = "/";
private String redirectPrefix = DEFAULT_REDIRECT_PREFIX;
private String forwardPrefix = DEFAULT_FORWARD_PREFIX;
public SimpleJSFNavigationHandler() {
}
public SimpleJSFNavigationHandler(NavigationHandler originalNavigationHandler) {
super(originalNavigationHandler);
}
#Override
public void handleNavigation(FacesContext facesContext, String fromAction, String outcome, NavigationHandler originalNavigationHandler) {
if (outcome != null && outcome.startsWith(redirectPrefix)) {
ExternalContext externalContext = facesContext.getExternalContext();
ViewHandler viewHandler = facesContext.getApplication().getViewHandler();
String url = outcome.substring(redirectPrefix.length());
String urlParams="";
if(url.indexOf("?")>0)
urlParams = url.substring(url.indexOf("?"));
String redirectPath = viewHandler.getActionURL(facesContext, url);
try {
//System.out.println("MMMMMMMMMMMMMMMM:::::::::::::::::" + urlParams);
externalContext.redirect(externalContext.encodeActionURL(redirectPath+urlParams));
} catch (IOException e) {
throw new FacesException(e.getMessage(), e);
}
facesContext.responseComplete();
} else if (outcome != null && outcome.startsWith(forwardPrefix)) {
ViewHandler viewHandler = facesContext.getApplication().getViewHandler();
//create new view
String newViewId = outcome.substring(forwardPrefix.length());
if (newViewId.length()>0 && newViewId.charAt(0)!='/') {
newViewId = "/" + newViewId;
}
UIViewRoot viewRoot = viewHandler.createView(facesContext, newViewId);
viewRoot.setViewId(newViewId);
facesContext.setViewRoot(viewRoot);
facesContext.renderResponse();
} else {
callNextHandlerInChain(facesContext, fromAction, outcome, originalNavigationHandler);
}
}
}
You don't need Spring's DecoratingNavigationHandler. You can just use JSF's own ConfigurableNavigationHandlerWrapper.
public class SimpleJSFNavigationHandler extends ConfigurableNavigationHandlerWrapper {
private ConfigurableNavigationHandler wrapped;
public SimpleJSFNavigationHandler(ConfigurableNavigationHandler wrapped) {
this.wrapped = wrapped;
}
#Override
public void handleNavigation(FacesContext facesContext, String fromAction, String outcome) {
if (...) {
// Your original code here.
} else if (...) {
// Your original code here.
} else {
// Update only the last else part as below.
getWrapped().handleNavigation(facesContext, fromAction, outcome);
}
}
#Override
public ConfigurableNavigationHandler getWrapped() {
return wrapped;
}
}
In the upcoming JSF 2.3 this can even be further simplified as per spec issue 1429 which should further reduce boilerplate code in FacesWrapper implementations.
public class SimpleJSFNavigationHandler extends ConfigurableNavigationHandlerWrapper {
public SimpleJSFNavigationHandler(ConfigurableNavigationHandler wrapped) {
super(wrapped);
}
#Override
public void handleNavigation(FacesContext facesContext, String fromAction, String outcome) {
if (...) {
// Your original code here.
} else if (...) {
// Your original code here.
} else {
// Update only the last else part as below.
getWrapped().handleNavigation(facesContext, fromAction, outcome);
}
}
}

javax.el.PropertyNotFoundException: itemLabel="#{projet.nomProjet}": Property 'nomProjet' not found on type java.lang.String

I'm trying to apply a JSF converter to an Entity inside a selectOneMenu,
but the converter is not recognized, I get this warning in my xhtml file,
<<"nomProjet" cannot be resolved>>
and when I run the application I'm getting Error HTTP 500 :
itemLabel="#{projet.nomProjet}": Property 'nomProjet' not found on type java.lang.String
Here is my code:
The selectOneMenu in my view
<p:selectOneMenu id="projet" converter="projetConverter" value="# {affectation.selectedProjet}" >
<f:selectItems var="projet" itemValue="#{projet}" itemLabel="#{projet.nomProjet}" value="#{affectation.projetsAffectablesCollaborateur()}" />
</p:selectOneMenu>
The converter
#Component
#FacesConverter("projetConverter")
public class ProjetConverter implements Converter {
#Autowired
private ProjetRepository projetRepository;
#Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if (value == null || value.isEmpty()) {
return null;
}
try {
Projet projet = projetRepository.findByIdProjet(Long.valueOf(value));
return projet;
} catch (NumberFormatException exception) {
throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erreur de conversion", "ID de projet invalide"));
}
}
#Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
if (value == null) {
return "";
}
if (value instanceof Projet) {
return String.valueOf(((Projet) value).getIdProjet());
} else {
throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erreur de conversion", "Instance de projet invalide"));
}
}
}
And my Entity :
#Entity
#NamedQuery(name = "Projet.findAll", query = "SELECT p FROM Projet p")
public class Projet implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private long idProjet;
private String nomProjet;
#Transient
private List<Role> listRoles = new ArrayList<Role>();
public List<Role> getListRoles() {
return listRoles;
}
public void setListRoles(List<Role> listRoles) {
this.listRoles = listRoles;
}
// bi-directional many-to-one association to AffectationProjetRole
#OneToMany(mappedBy = "projet")
private List<AffectationProjetRole> affectationProjetRoles;
public Projet() {
}
public Projet(String nomProjet) {
this.nomProjet = nomProjet;
}
public long getIdProjet() {
return this.idProjet;
}
public void setIdProjet(long idProjet) {
this.idProjet = idProjet;
}
public String getNomProjet() {
return this.nomProjet;
}
public void setNomProjet(String nomProjet) {
this.nomProjet = nomProjet;
}
public List<AffectationProjetRole> getAffectationProjetRoles() {
return this.affectationProjetRoles;
}
public void setAffectationProjetRoles(List<AffectationProjetRole> affectationProjetRoles) {
this.affectationProjetRoles = affectationProjetRoles;
}
public AffectationProjetRole addAffectationProjetRole(AffectationProjetRole affectationProjetRole) {
getAffectationProjetRoles().add(affectationProjetRole);
affectationProjetRole.setProjet(this);
return affectationProjetRole;
}
public AffectationProjetRole removeAffectationProjetRole(AffectationProjetRole affectationProjetRole) {
getAffectationProjetRoles().remove(affectationProjetRole);
affectationProjetRole.setProjet(null);
return affectationProjetRole;
}
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (int) (idProjet ^ (idProjet >>> 32));
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Projet other = (Projet) obj;
if (idProjet != other.idProjet)
return false;
return true;
}
}
How is this caused and how can I solve it?
itemLabel="#{projet.nomProjet}": Property 'nomProjet' not found on type java.lang.String
This error message tells that #{projet} is during runtime actually a java.lang.String. Let's look where's #{projet} is coming from.
<f:selectItems value="#{affectation.projetsAffectablesCollaborateur()}"
var="projet" itemValue="#{projet}" itemLabel="#{projet.nomProjet}" />
Thus, #{affectation.projetsAffectablesCollaborateur()} actually returned a List<String>. If this is unexpected, then beware of generic type erasure and doublecheck all unchecked casts that the generic type is not incorrectly assumed. Generally, the mistake is in the persitence layer. For example, when you mistakenly execute the query SELECT p.nomProject FROM Project p instead of SELECT p FROM Project p and then performed an unchecked cast against List<Projet> instead of List<String>.
Do note that rendering item labels doesn't involve the converter at all, so showing and blaming it is unnecessary. The converter is only used on item values.

JSF with Enum 'Validation Error: Value is not valid'

I have an enum whose code is like this -
public enum COSOptionType {
NOTAPPLICABLE,
OPTIONAL,
MANDATORY;
private String[] label = { "Not Applicable", "Optional", "Mandatory"};
#Override
public String toString() {
return label[this.ordinal()];
}
public static COSOptionType getCOSOption(String value) {
int ivalue = Integer.parseInt(value);
switch(ivalue) {
case 0:
return NOTAPPLICABLE;
case 1:
return OPTIONAL;
case 2:
return MANDATORY;
default:
throw new RuntimeException("Should not get this far ever!");
}
}
}
I have the converter to convert the enum type
public class COSEnumConverter implements Converter {
public Object getAsObject(FacesContext context, UIComponent comp, String value) {
return COSOptionType.getCOSOption(value);
}
public String getAsString(FacesContext context, UIComponent comp, Object obj) {
if (obj instanceof String) {
return (String) obj;
}
COSOptionType type = (COSOptionType) obj;
int index = type.ordinal();
return ""+index;
}
}
The view looks like this
<h:selectOneMenu value="#{controller.type}" id="smoking">
<f:selectItems value="#{jnyController.choices}" />
</h:selectOneMenu>
Here is the code for create choices
private List<SelectItem> createChoicies() {
List<SelectItem> list = new ArrayList<SelectItem>();
for (COSOptionType cos : COSOptionType.values()) {
SelectItem item = new SelectItem();
item.setLabel(cos.toString());
item.setValue("" + cos.ordinal());
list.add(item);
}
return list;
}
I do not understand why this would throw "validation error" all the time ? I can debug and see that the converter is working fine.
NOTE: I am using JSF 1.1
The root cause is that the converter returns a fullworthy COSOptionType object instead of the string "" + cos.ordinal() in getAsObject(), which thus don't appear at all in the existing options.
At any way, this approach is overcomplicated, especially the enum and the converter. I recommend you to reconsider this approach as follows:
Option:
package com.example;
public enum Option {
NOTAPPLICABLE("Not Applicable"),
OPTIONAL("Optional"),
MANDATORY("Mandatory");
private String label;
private Option(String label) {
this.label = label;
}
public String getLabel() {
return label;
}
}
Bean:
package com.example;
import java.util.ArrayList;
import java.util.List;
import javax.faces.model.SelectItem;
public class Bean {
private List<SelectItem> options = new ArrayList<SelectItem>();
private Option option;
public Bean() {
for (Option option : Option.values()) {
options.add(new SelectItem(option, option.getLabel()));
}
}
public void submit() {
System.out.println(option);
}
public List<SelectItem> getOptions() {
return options;
}
public Option getOption() {
return option;
}
public void setOption(Option option) {
this.option = option;
}
}
OptionConverter:
package com.example;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
public class OptionConverter implements Converter {
#Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
return Option.valueOf(value);
}
#Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
return ((Option) value).name();
}
}
faces-config.xml:
<managed-bean>
<managed-bean-name>bean</managed-bean-name>
<managed-bean-class>com.example.Bean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<converter>
<converter-class>com.example.OptionConverter</converter-class>
<converter-for-class>com.example.Option</converter-for-class>
</converter>
Relevant part of JSF page:
<h:form>
<h:selectOneMenu value="#{bean.option}">
<f:selectItems value="#{bean.options}" />
</h:selectOneMenu>
<h:commandButton value="submit" action="#{bean.submit}" />
<h:messages />
</h:form>
No need to hassle with Enum#ordinal() which is considered evil.
If you were using JSF 1.2 or newer which ships with a builtin generic EnumConverter, the OptionConverter is totally superfluous and can be safely removed.
Try item.setValue(cos) instead of item.setValue("" + cos.ordinal()).

Resources