Really odd one...
I have a converter which works when I use a p:SelectOneMenu, but when I switch to a p:SelectOneRadio, I get a major crash with a java heap space errors. The stacktrace seems to be of no use, just a java.lang.OutOfMemeoryError.
This works:
<p:selectOneMenu id="regions" value="#{aDMSBean.selectedRegion}">
<f:selectItem itemLabel="Global" itemValue="#{null}" />
<f:selectItems value="#{aDMSBean.adminRegions}" var="adminRegion" itemLabel="# {adminRegion.regionName}" itemValue="#{adminRegion}" />
<f:converter id="adminRegionConverter" converterId="regionConverter" />
<p:ajax listener="#{aDMSBean.regionSelect}" update="unassignedTasks"></p:ajax>
</p:selectOneMenu>
This crashes and burns:
<p:selectOneRadio id="regions" value="#{aDMSBean.selectedRegion}">
<f:selectItem itemLabel="Global" itemValue="#{null}" />
<f:selectItems value="#{aDMSBean.adminRegions}" var="adminRegion" itemLabel="# {adminRegion.regionName}" itemValue="#{adminRegion}" />
<f:converter id="adminRegionConverter" converterId="regionConverter" />
<p:ajax listener="#{aDMSBean.regionSelect}" update="unassignedTasks"></p:ajax>
</p:selectOneRadio>
I can only assume the converter is OK, as it works with the selectOneMenu.
#FacesConverter("regionConverter")
public class RegionConverter implements Converter {
#Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
Region region = null;
if (value != null && value.length() > 0) {
region = Region.findRegion(new Long(value));
}
return region;
}
#Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
String val = "";
if (value != null && value instanceof Region) {
val = ((Region) value).getId().toString();
}
return val;
}
}
Regards
i
In the end it was a recursive #RooToString method being called. I had to examine the data model relationships and add an annotation to #RooToString to avoid the cycle in a few entities
#RooToString(excludeFields = { "adminRegion" })
Related
So I have the following xhtml code:
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{interview.interviewer.name}" />
</f:facet>
<f:facet name="input">
<p:selectOneMenu value="#{interview.interviewer}" style="width:100%" converter="interviewerConverter">
<f:selectItems value="#{interviewerController.allInterviewers}" var="s" itemLabel="#{s.name}" itemValue="#{s}" />
</p:selectOneMenu>
</f:facet>
</p:cellEditor>
When I try to edit the data in the cell, I get the following error:
Conversion Error setting value 'com.jpa.entities.Interviewer#288002c2' for 'null Converter'.
I took a look at the similar posts but did not find out what is wrong here.
Also I have created the following converter, with this I do not get a converter error:
#FacesConverter("interviewerConverter")
public class InterviewerConverter implements Converter {
public Object getAsObject(FacesContext fc, UIComponent uic, String value) {
if(value != null && value.trim().length() > 0) {
try {
InterviewerController service = (InterviewerController) fc.getExternalContext().getApplicationMap().get("interviewerController");
return service.getallInterviewers().get(Integer.parseInt(value));
} catch(NumberFormatException e) {
throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Not a valid"));
}
}
else {
return null;
}
}
public String getAsString(FacesContext fc, UIComponent uic, Object object) {
if(object != null) {
return String.valueOf(((Interviewer) object).getId());
}
else {
return null;
}
}
}
After implementing the converter, I get a NullPointerException on the following list: (service.getallInterviewers())
return service.getallInterviewers().get(Integer.parseInt(value));
but in the controller, the list is initialized like this:
#PostConstruct
public void init(){
initAllInterviewers();
}
What can be wrong here?
I have a p:selectOneMenu that changing value should run a listener to update the value of another p:selectOneMenu (Type: state -> city). In addition to the items have the item "All" in the first p:selectOneMenu. When you select any item other than the "All" the listener is not called, but when I select the "All" option.
I also realized that the model will not be updated. That is the value of the first p:selectOneMenu is not set in the entity attribute,
The following codes:
First
<p:selectOneMenu id="pesquisaCategoria" style="width:100%;"
value="#{inscricaoFaces.entity.categoriaSelecionada}"
styleClass="input-block-level" converter="#{categoriaConverter}">
<f:selectItem itemLabel="#{msgs['label.dropdown.todos']}"
noSelectionOption="true" itemValue="#{null}" />
<f:selectItems value="#{categoriaFaces.listarTodasCategorias()}"
var="categoria" itemValue="#{categoria}"
itemLabel="#{categoria.descricao}" />
<p:ajax update="pesquisaAreaAtuacao" event="change"
listener="#{inscricaoFaces.atualizarAreasAtuacao}" />
</p:selectOneMenu>
Second
<p:selectOneMenu id="pesquisaAreaAtuacao" style="width:100%;"
value="#{inscricaoFaces.entity.areaAtuacao}"
styleClass="input-block-level" converter="#{areaAtuacaoConverter}">
<f:selectItem itemLabel="#{msgs['label.dropdown.todos']}"
noSelectionOption="true" itemValue="#{null}" />
<f:selectItems value="#{inscricaoFaces.areasAtuacao}" var="area"
itemValue="#{area}" itemLabel="#{area.descricao}" />
</p:selectOneMenu>
Convert
#ManagedBean
#RequestScoped
public class CategoriaConverter implements Converter, Serializable {
/**
*
*/
private static final long serialVersionUID = -1073909086688882110L;
#EJB
private CategoriaService categoriaService;
#Override
public Object getAsObject(FacesContext fc, UIComponent uic, String value) {
if (value != null && value.trim().length() > 0 && !"Todos".equals(value)) {
return categoriaService.consultarPorId(new Categoria(Integer.valueOf(value)));
} else {
return null;
}
}
#Override
public String getAsString(FacesContext fc, UIComponent uic, Object value) {
if(value != null){
return value instanceof Categoria ? String.valueOf(((Categoria) value).getCodigo()) : null;
}else{
return null;
}
}
}
This question already has an answer here:
<f:selectItems> only shows toString() of the model as item label
(1 answer)
Closed 6 years ago.
I am trying to use an converter for custom objects, that are used in a primefaces' selectCheckboxMenu.
This is the JSF part:
<p:outputLabel value="#{msg.cars}: " for="cars" />
<p:selectCheckboxMenu id="cars"
value="#{controller.selected.cars}"
converter="carConverter" label="#{msg.cars}"
filter="true" filterMatchMode="startsWith"
panelStyle="width:200px">
<f:selectItems
value="#{controller.available.cars}" />
<f:converter converterId="carConverter" />
</p:selectCheckboxMenu>
And this is my converter:
#FacesConverter("carConverter")
public class CarConverter implements Converter {
#Override
public Object getAsObject(FacesContext context, UIComponent component, String newValue) {
return null;
}
#Override
public String getAsString(FacesContext context, UIComponent component, Object object) {
if (object == null) {
return "";
}
if (object instanceof Car) {
Car car = (Car) object;
String name = car.getName();
return name;
} else {
throw new ConverterException(new FacesMessage(object + " is not a valid car"));
}
}
}
getAsString() returns the correct String. But the selectCheckboxMenu still lists the objects and not the Strings.
Am I missing something?
If you need to show the car name in checkboxMenu label you have to use the selectItems' itemLabel attribute
<p:outputLabel value="#{msg.cars}: " for="cars" />
<p:selectCheckboxMenu id="cars"
value="#{controller.selected.cars}"
converter="carConverter"
filter="true" filterMatchMode="startsWith"
panelStyle="width:200px">
<f:selectItems value="#{controller.available.cars}" var="car" itemLabel="#{car.name}" itemValue="#{car}"/>
</p:selectCheckboxMenu>
BTW don't declare two converters (one via converter attribute and the other via f:converter), and override correctly the getAsObject method (it's needed during the Apply Request Values phase). Check the docs for the details
This question already has answers here:
Validation Error: Value is not valid
(3 answers)
Closed 8 years ago.
I keep getting this error addAddress:states: Validation Error: Value is not valid when using <p:selectOneMenu. I tried using the id and got rid of the converter, its doing the same. I tried debugging. Found that the converted is running twice and the last time it is checking blank value and returning null/false. What am I doing wrong? If need any more details let me know?
Code is as follows
Converter
#FacesConverter(value = "statemasterconverter", forClass = Statemaster.class)
public class StateConverter implements Converter {
public String getAsString(FacesContext context, UIComponent component,
Object value) {
return ConversionHelper.getAsString(value);
}
public Object getAsObject(FacesContext context, UIComponent component,
String value) {
return ConversionHelper.getAsObject(Statemaster.class, value);
}
}
Conversion Helper
public final class ConversionHelper {
private ConversionHelper() {
}
#SuppressWarnings("unchecked")
public static <T> T getAsObject(Class<T> returnType, String value) {
BigDecimal id = BigDecimal.ZERO;
if (returnType == null) {
throw new NullPointerException(
"Trying to getAsObject with a null return type.");
}
if (value == null) {
throw new NullPointerException(
"Trying to getAsObject with a null value.");
}
try {
id = BigDecimal.valueOf(Long.parseLong(value));
} catch (NumberFormatException nfe) {
return null;
}
Session session = HibernateUtil.getSessionFactory().openSession();
try {
T r = (T) session.load(returnType, id);
if (r != null)
Hibernate.initialize(r);
return r;
} catch (HibernateException e) {
e.printStackTrace();
} finally {
session.close();
}
return null;
}
public static String getAsString(Object value) {
if (value instanceof Gendermaster) {
Gendermaster result = (Gendermaster) value;
return String.valueOf(result.getGenderid());
} else if (value instanceof Salutationmaster) {
Salutationmaster result = (Salutationmaster) value;
return String.valueOf(result.getSalutationid());
} else if (value instanceof Countrymaster) {
Countrymaster result = (Countrymaster) value;
return String.valueOf(result.getCountryid());
} else if (value instanceof Statemaster) {
Statemaster result = (Statemaster) value;
return String.valueOf(result.getStateid());
}
return null;
}
}
the xhtml code
<p:row>
<p:column>
Country
</p:column>
<p:column>
<p:selectOneMenu id="country"
value="#{customerBean.country.countryid}" required="true">
<f:selectItem itemLabel="Select Country" itemValue="" />
<f:selectItems value="#{customerBean.countrydropdown}"
var="countrymaster"
itemLabel="#{countrymaster.countryname} - #{countrymaster.countrycodeNn}"
itemValue="#{countrymaster.countryid}" />
<p:ajax update="states"
listener="#{customerBean.updateStates()}" />
</p:selectOneMenu>
</p:column>
</p:row>
<p:row>
<p:column>
State
</p:column>
<p:column>
<p:selectOneMenu id="states" required="true"
value="#{customerBean.state}" converter="statemasterconverter">
<f:selectItem itemValue="" itemLabel="Select State" />
<f:selectItems value="#{customerBean.statedropdown}"
var="statemaster"
itemLabel="#{statemaster.statename} - #{statemaster.statecode}"
itemValue="#{statemaster}" />
</p:selectOneMenu>
</p:column>
</p:row>
<p:commandButton id="saveBtn" value="Save Salutation"
update="msgs"
style="float: left;" icon="ui-icon-disk"
actionListener="#{customerBean.saveAddress()}" ajax="true" />
</p:column>
Use event="valueChange" in Ajax. and try to updated whole form by update="#form".
if above code is not work then try update=":id:id" update tag search id within the tag.
let consider this code.
<h:form id="myForm">
<h:sometag id="ineer1">
<p:ajax update="ineer3"/>// it is **not work**
<p:ajax update=":myForm:ineer2:ineer2"/>// it is **work**
</h:sometag>
<h:sometag id="ineer2">
<h:someoutfield id="ineer3"/>
</h:sometag>
<h:/form>
it is working at my end if not work then let me know.
Happy to help :)
I have
<p:remoteCommand name="updateSelectedTarget" update="partToUpdate" actionListener="#{bean.onSelectedTarget}"/>
bean.java
public void onSelectedTarget() {
System.out.println("here");
}
With this configuration, the action is only fired once. Do I need to use actionListener or action? And is there something to do with the process parameter of remoteCommand?
I just want to add that it was working before. I just changed the updated part below
<h:panelGroup id="partToUpdate" layout="block">
<p:panelGrid rendered="#{bean.selectedTarget == null ? false : true}">
...
<h:selectOneMenu value="#{bean.stuffID}">
...
</h:selectOneMenu>
...
</p:panelGrid>
</h:panelGroup>
Before there was just an ID one the selectOneMenu
<h:selectOneMenu value="#{bean.stuffID}">
and now I have the object
<h:selectOneMenu value="#{bean.stuff}">
the java.lang.ClassCastException comes from the line
<f:selectItem itemLabel="None" itemValue="null" />
Could you verify this
#FacesConverter("user")
public class UserConverter implements Converter{
private List<User> users;
public UserConverter(){
this.users = myController.getAllUsers();
}
public Object getAsObject(FacesContext context, UIComponent component, String value) {
return this.getUser(value);
}
public String getAsString(FacesContext context, UIComponent component, Object value) {
return String.valueOf(((User) value).getId()).toString();
}
public User getUser(String id) {
Iterator<User> iterator = this.users.iterator();
while(iterator.hasNext()) {
User user = iterator.next();
if(user.getId() == Integer.valueOf(id)) {
return user;
}
}
return null;
}
}
I think my converter is good because I use it somewhere else.
Maybe the mistake is how to use it with the h:selectOneMenu
I have this
<h:selectOneMenu value="#{bean.selectedUser}">
<f:selectItem itemLabel="None" itemValue="null" />
<f:selectItems value="#{bean.allusers}" var="user" itemValue="#{user.id}" itemLabel="#{user.name}"/>
<f:converter converterId="user"/>
</h:selectOneMenu>
So to conclude I changed my selectOneMenu to
<h:selectOneMenu value="#{bean.selectedUser}">
<f:selectItem itemLabel="None" itemValue="#{null}" />
<f:selectItems value="#{bean.allusers}" var="user" itemValue="#{user}" itemLabel="#{user.name}"/>
<f:converter converterId="user"/>
</h:selectOneMenu>
And in my converter
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if(!StringUtils.isInteger(value)) {
return null;
}
return this.getUser(value);
}
public String getAsString(FacesContext context, UIComponent component, Object value) {
if(value == null) {
return null;
}
return String.valueOf(((User) value).getId()).toString();
}
With the method below because I am looking for id and getAsObject value returned the label "None"
public static boolean isInteger(String s) {
try {
Integer.parseInt(s);
} catch(NumberFormatException e) {
return false;
}
return true;
}
And problem solved.
Thnks
EDIT
Don't forget to add the equals override method to the User class.
it happens to me many times like this and I find that the problem comes from previous page that call the current one