primefaces `<p:selectOneMenu` [duplicate] - jsf

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 :)

Related

JSF SelectOneMenu not working without converter?

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?

Why do I can alway get label in getAsObject() in Primefaces?

I do not why although I verify everything which is right. Anyone know?
I tried to debug:
- getAsString() return ID (that's right)
- when I submitted - getAsObject() always throw exception because the label values was always passed instead of ID values.
My code as below:
my xhtml file:
<div class="ui-grid-row" style="margin-top: 5px; margin-bottom: 5px;">
<div class="ui-grid-col-2 pdt4">
<p:outputLabel for="txtApprovalScheduler"
value="#{lang['workforce.category.parttimeManagement.approved.scheduler']}"/>
</div>
<div class="ui-grid-col-2">
<p:selectOneMenu value="#{parttimeController.selectedAgentDTO}"
id="txtApprovalScheduler"
filterMatchMode="contains" editable="true"
style="width: 86%;"
required="true"
requiredMessage="#{lang['workforce.category.parttimeManagement.approved.scheduler.missing']}">
<f:converter converterId="agentConverter"/>
<f:selectItem itemValue="" itemLabel="#{lang['wf.common.choose']}"/>
<f:selectItems value="#{parttimeController.agentDTOs}" var="agentItem1"
itemLabel="#{agentItem1.userName}"
itemValue="#{agentItem1}"/>
</p:selectOneMenu>
</div>
<div class="ui-grid-col-2 pdt4">
<p:outputLabel for="txtApprovalRegister"
value="#{lang['workforce.category.parttimeManagement.approved.register']}"/>
</div>
<div class="ui-grid-col-2">
<p:selectOneMenu value="#{parttimeController.selectedAgentDTOForRegister}"
id="txtApprovalRegister" editable="true"
filterMatchMode="contains" style="width: 86%; font-size: 12px !important;"
required="true"
requiredMessage="#{lang['workforce.category.parttimeManagement.approved.register.missing']}">
<f:converter converterId="agentConverter"/>
<f:selectItem itemValue="" itemLabel="#{lang['wf.common.choose']}"/>
<f:selectItems value="#{parttimeController.agentDTOs}" var="item1"
itemLabel="#{item1.userName}"
itemValue="#{item1}"/>
</p:selectOneMenu>
</div>
</div>
My Converter:
#FacesConverter(forClass = AgentDTO.class,value = "agentConverter")
public class AgentConverter implements Converter {
public static List<AgentDTO> listAgentDTOs;
#Override
public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String agentId) {
if (agentId.trim().equals("")) {
return null;
} else {
try {
Long number = Long.parseLong(agentId);
for (AgentDTO a : listAgentDTOs) {
if (a.getAgentId() == number) {
return a;
}
}
} catch (NumberFormatException exception) {
throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Not a valid data"));
}
}
return null;
}
#Override
public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value) {
if (value == null || value.equals("") || "-1".equals(value)) {
return "";
} else {
String result = String.valueOf(((AgentDTO) value).getAgentId());
return result;
}
}
}
My bean:
#Component
#Scope("view")
#ManagedBean(name = "parttimeController")
public class ParttimeController extends BaseController implements Serializable {
private AgentDTO selectedAgentDTO;
private AgentDTO selectedAgentDTOForRegister;
private List<AgentDTO> agentDTOs;
.... getter/setter and initiallize AgentCenvert.listAgentDTOs
My class:
public class AgentDTO{
private Long agentId;
private String userName;
....getter/setter
}
I found out the reason - I only remove editable attribute in selectOneMenu. So getAsObject() will pass a value instead of a label

Conversion Error: Setting value for 'null converter' [duplicate]

This question already has answers here:
Conversion Error setting value for 'null Converter' - Why do I need a Converter in JSF?
(2 answers)
Closed 6 years ago.
Ok, so I get this error and found no solution working for me (I tried around a lot with annotations like #FacesConverter or #Named instead #ManagedBean etc.) If anybody could point me to the right direction, that would be awesome. BalusC mentioned, that this particular error means, that the converter cannot be found, but the Converter is getting called, as I can see in log messages of JBoss (see log.info calls below in the Converter code). At least I don't get NullPointerExceptions from the Converter anymore (although I can't reproduce why). I'm getting a bit frustrated with JSF, but I'm sure it's my fault and I have overseen something obvious?
The code should be simple enough, I want to set zero, one or more Tags for a Link entity (newLink.tags) with means of a selectManyCheckbox:
This is my XHTML form:
<h:form id="create_link_form" rendered="#{authController.loggedIn}">
<h3>Enter a new Link here:</h3>`
<h:panelGrid columns="3" columnClasses="titleCell">
<h:outputLabel for="name" value="Name:"/>
<h:inputText id="name" value="#{newLink.name}"/>
<p:message for="name" errorClass="invalid"/>
<h:outputLabel for="url" value="URL:"/>
<h:inputText id="url" value="#{newLink.url}"/>
<p:message for="url" errorClass="invalid"/>
<h:outputLabel for="description" value="Description:"/>
<h:inputText id="description" value="#{newLink.description}"/>
<p:message for="description" errorClass="invalid"/>
<h:outputLabel for="shared" value="Shared?:"/>
<h:selectBooleanCheckbox id="shared" label="Shared?:" value="#{newLink.shared}"/>
<p:message for="shared" errorClass="invalid"/>
</h:panelGrid>
<h:outputLabel for="tags" value="Tags:"/>
<h:selectManyCheckbox label="Tags:" id="tags" value="#{newLink.tags}" converter="#{tagConverter}">
<f:selectItems value="#{tags}" var="tag" itemLabel="#{tag.name}" itemValue="#{tag}"/>
</h:selectManyCheckbox>
<h:inputHidden id="owner" value="#{newLink.owner}" name="{authController.loggedInUserName}"/>
<p>
<h:panelGrid columns="2">
<h:commandButton id="create" action="#{linkController.create}" value="Create"
styleClass="create">
</h:commandButton>
<p:messages styleClass="messages" autoUpdate="true" globalOnly="true"/>
</h:panelGrid>
</p>
</h:form>
And this is my Converter:
#RequestScoped
#ManagedBean
public class TagConverter implements Converter {
#Inject
private Logger log;
#Inject
private TagService tagService;
#Override
public Object getAsObject(FacesContext fc, UIComponent uic, String value) {
if (value != null && value.trim().length() > 0) {
try {
log.info("TTagConverter.getAsObject() => having " + value);
Long id = Long.parseLong(value);
Tag tag = tagService.getTagWithId(id);
log.info("TagConverter.getAsObject() => Tag converted: " + tag);
return tag;
} catch (NumberFormatException e) {
throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Not a valid tag."));
}
} else {
return null;
}
}
#Override
public String getAsString(FacesContext fc, UIComponent uic, Object object) {
if (object != null) {
if (object instanceof Tag) {
Long id = ((Tag) object).getId();
return id.toString();
} else {
throw new ConverterException(new FacesMessage("There was an Object type error for a "
+ object.getClass() + " in getAsString(" + object + ")"));
}
} else {
return null;
}
}
}
Your managed bean shouldnt implement it.
Create class and implement converter.
#FacesConverter("myConverter")
public class MyConverter implements Converter{
...
}
in your facelets
<h:selectManyCheckbox label="Tags:" id="tags" value="#{newLink.tags}">
<f:selectItems value="#{tags}" var="tag" itemLabel="#{tag.name}" itemValue="#{tag}"/>
<f:converter converterId="myConverter" />
</h:selectManyCheckbox>
Is it clear?

Primefaces remotecommand action called only once

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

p:selectOneRadio converter causing stack heap memory issues

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" })

Resources