How to resolve the jsf datatable java.lang.NumberFormatException: For input string, in bellow my code :
I have SelectItemDTO :
public class SelectItemDTO implements Serializable {
private static final long serialVersionUID = 1L;
private Object value;
private String label;
public SelectItemDTO() {
}
public SelectItemDTO(Object value, String label) {
this.value = value;
this.label = label;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
}
in my dialog.xhtml i have :
<p:dialog id="#{cc.attrs.widgetVar}" widgetVar="#{cc.attrs.widgetVar}" resizable="true" responsive="true" modal="true" appendTo="#(body)" height="580" width="850" dynamic="true">
<h:form>
<p:fieldset>
<p:panel header="Ajout d'une nouvelle règle">
#{cc.attrs.value.parametres}
</p:panel>
</p:fieldset>
</h:form>
</p:dialog>
when i run my application i have the output in dialog.xhtml [com.sfr.medusa.dto.SelectItemDTO#1d591889, com.sfr.medusa.dto.SelectItemDTO#62e36383]
now when i change the dialog to :
<p:dialog id="#{cc.attrs.widgetVar}" widgetVar="#{cc.attrs.widgetVar}" resizable="true" responsive="true" modal="true" appendTo="#(body)" height="580" width="850" dynamic="true">
<h:form>
<p:fieldset>
<p:panel header="Ajout d'une nouvelle règle">
<p:dataTable value="#{cc.attrs.value.parametres}" var="par" emptyMessage="#{msg['datatable.msg.empty']}">
<p:column headerText="Param">
<h:outputText value="#{par.label}" />
</p:column>
<p:column headerText="Value">
<h:inputText value="#{par.value}" />
</p:column>
</p:dataTable>
</p:panel>
</p:fieldset>
</h:form>
</p:dialog>
I have a an exception:
java.lang.NumberFormatException: For input string: "parametres"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at javax.el.ListELResolver.coerce(ListELResolver.java:157)
at javax.el.ListELResolver.getValue(ListELResolver.java:70)
at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
at org.apache.el.parser.AstValue.getValue(AstValue.java:169)
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184)
at com.sun.faces.facelets.el.ContextualCompositeValueExpression.getValue(ContextualCompositeValueExpression.java:158)
at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
at javax.faces.component.UIData.getValue(UIData.java:732)
at org.primefaces.component.api.UIData.getDataModel(UIData.java:764)
at org.primefaces.component.api.UIData.setRowModel(UIData.java:571)
at org.primefaces.component.api.UIData.setRowIndexWithoutRowStatePreserved(UIData.java:564)
at org.primefaces.component.api.UIData.setRowIndex(UIData.java:473)
at javax.faces.component.UIData.invokeOnComponent(UIData.java:1041)
at javax.faces.component.UIComponent.invokeOnComponent(UIComponent.java:1503)
at javax.faces.component.UIComponentBase.invokeOnComponent(UIComponentBase.java:718)
with this code i get the expected result but not with datatable:
<p:panelGrid class="ui-noborder" columns="2" cellpadding="5">
<f:facet name="header">
<p:row>
<p:column headerText="Paramètre" />
<p:column headerText="Valeur" />
</p:row>
</f:facet>
<c:forEach items="#{cc.attrs.value.parametres}" var="param">
<p:row>
<p:column>
<p:outputLabel value="#{param.label}" />
</p:column>
<p:column>
<p:inputText value="#{param.value}" />
</p:column>
</p:row>
</c:forEach>
</p:panelGrid>
I have the same exception with other component :
<p:commandButton icon="ui-icon-check" value="#{msg['btn.add']}" onstart="PF('statusDialog').show()" oncomplete="PF('#{atWidgetVar}').hide();" actionListener="#{cc.attrs.actionListener}" update="#{cc.attrs.update}" >
</p:commandButton>
this code cause the same exception. when i replace update attribute with :
<f:ajax render="#{cc.attrs.update}"/>
this one work fine.
i think the primefaces datatable have many bugs when the datatable is empty.
Ok i found it (not a problem of jsf or priemfaces).
the problem is when we use a composit to pass parmetre we should use a type in attribute when is # string like this:
<ce:attribute name="value" required="true" type="java.util.List"/>
for now i let open this maybe there is other problem :)
I advance in the debug I think the question is :
it is possible to pass value of datatable in composite attribute ?
<ce:attribute name="value" required="true" />
<p:dataTable value="#{cc.attrs.value}"
Related
I have two primefaces dataTables. The first show the instances of a MainEntity.
The second one show a list of Words (just strings) associated with the selected MainEntity on the first datatable.
The printscreen illustrates what I mean.
My problem is, when I edit a string on the Word List, my listener method won't receive the new value. In fact, when I call the event.getNewValue() method, I get old value.
What am I missing?
I'm using JavaServer Faces 2.2, Primefaces 5.0 and Spring Framework 4.0.3.
Thanks in advance for any help.
The code for the xhtml, the managed bean and the MainEntity are as follows:
mainEntity.xhtml:
<h:body>
<h:form id="mainEntityForm">
<p:panelGrid columns="1" fullPage="true" id="dashboard">
<f:facet name="header">
<h2>MainEntity Table</h2>
</f:facet>
<p:panel>
<f:facet name="header">
MainEntity List
<p:commandButton value="New MainEntity"
actionListener="#{mainEntityController.createMainEntityDialog}"
styleClass="header-button">
<p:ajax event="dialogReturn" update=":mainEntityForm:mainEntityTable" />
</p:commandButton>
</f:facet>
<p:dataTable id="mainEntityTable" var="mainEntity" value="#{mainEntityController.mainEntities}"
editable="true" editMode="cell" widgetVar="cellMainEntity"
selectionMode="single" selection="#{mainEntityController.selectedMainEntity}"
rowKey="#{mainEntity.id}" tableStyle="width:auto">
<p:ajax event="rowSelect" update=":mainEntityForm:stringGrid :mainEntityForm:entityAGrid :mainEntityForm:entityBGrid" />
<p:ajax event="cellEdit" listener="#{mainEntityController.onEditMainEntity}" update=":mainEntityForm:mainEntityTable" />
<p:column headerText="ID">
<h:outputText value="#{mainEntity.id}" />
</p:column>
<p:column headerText="Name">
<p:cellEditor>
<f:facet name="output">
<h:outputText id="nameOutput" value="#{mainEntity.name}" />
</f:facet>
<f:facet name="input">
<h:inputText id="atyInput" value="#{mainEntity.qty}" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Commands">
<p:commandButton title="Remove MainEntity" icon="ui-icon-trash"
actionListener="#{mainEntityController.deleteMainEntity(mainEntity)}"
update=":mainEntityForm:mainEntityTable" />
</p:column>
</p:dataTable>
</p:panel>
<p:panelGrid fullPage="true" id="mainEntityDetail">
<f:facet name="header">
<p:row>
<p:column colspan="4">
<h2>MainEntity Detail</h2>
</p:column>
</p:row>
</f:facet>
<p:row>
<p:column>
<p:panel>
<f:facet name="header">
Word List
<p:commandButton value="New Word"
actionListener="#{mainEntityController.addNewWord()}"
styleClass="header-button"
update=":mainEntityForm:wordGrid">
</p:commandButton>
</f:facet>
<p:dataTable id="wordGrid" var="word"
value="#{mainEntityController.selectedMainEntity.wordList}"
tableStyle="width:auto" editable="true" editMode="cell" widgetVar="cellWord">
<p:ajax event="cellEdit" listener="#{mainEntityController.onEditWord}" />
<p:column headerText="Word">
<p:cellEditor>
<f:facet name="output">
<h:outputText id="wordOutput" value="#{word}" />
</f:facet>
<f:facet name="input">
<h:inputText id="wordInput" value="#{word}" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Commands">
<p:commandButton title="Remove Word"
icon="ui-icon-trash"
actionListener="#{mainEntityController.removeWord(word)}"
update=":mainEntityForm:wordGrid" />
</p:column>
</p:dataTable>
</p:panel>
</p:column>
</p:row>
</p:panelGrid>
</p:panelGrid>
</h:form>
</h:body>
MainEntityController.java (managed bean):
#ManagedBean
#SessionScoped
public class MainEntityController {
//...
private MainEntity selectedMainEntity;
public List<MainEntity> mainEntities;
//...
public MainEntity getSelectedMainEntity(){
return selectedMainEntity;
}
public void setSelectedMainEntity(MainEntity mainEntity){
this.selectedMainEntity = mainEntity;
}
//...
public void onEditWord(CellEditEvent event) {
Object oldValue = event.getOldValue();
Object newValue = event.getNewValue();
if(newValue != null && !newValue.equals(oldValue)) {
// THE PROBLEM IS HERE
// I NEVER ACTUALLY REACH THIS CODE
// newValue is always equal to oldValue!
FacesContext context = FacesContext.getCurrentInstance();
String word = context.getApplication().evaluateExpressionGet(context, "#{word}", String.class);
System.out.println(newValue);
System.out.println(word);
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Word Changed", "Old: " + oldValue + ", New:" + newValue);
FacesContext.getCurrentInstance().addMessage(null, msg);
}
}
}
MainEntity.java:
#Entity
public class MainEntity {
#Id
private String id;
#ElementCollection(fetch=FetchType.EAGER)
#CollectionTable(
name="MainEntity_Word",
joinColumns = #JoinColumn(name = "id", referencedColumnName="id")
)
#Column(name = "word")
private Set<String> wordList;
//...
public void addWord(String word) {
this.wordList.add(word);
}
public void removeWord(String word) {
this.wordList.remove(word);
}
public Set<String> getWordList() {
return wordList;
}
public void setWordList(Set<String> wordList) {
this.wordList = wordList;
}
}
Change
<h:inputText id="wordInput" value="#{word}" />
to
<p:inputText id="wordInput" value="#{word}" />
or
<h:inputText id="wordInput" value="#{word}" >
<p:ajax event="change" process="#this"/>
</h:inputText>
You need to ajaxify the input component, and at last update the outputText component.
Edited:
change this column :
<p:column headerText="Word" >
<p:cellEditor>
<f:facet name="output">
<h:outputText id="wordOutput" value="#{word}" />
</f:facet>
<f:facet name="input">
<h:inputText id="wordInput" value="#{word}" />
</f:facet>
</p:cellEditor>
</p:column>
to
<p:column headerText="Word" >
<p:cellEditor>
<f:facet name="output">
<h:outputText id="wordOutput" value="#{word}" />
</f:facet>
<f:facet name="input">
<p:inputText id="wordInput" value="#{word}" >
<p:ajax event="change" update="wordOutput" />
</p:inputText>
</f:facet>
</p:cellEditor>
</p:column>
Edit 2 :
From Chapter 24 Introduction to the Java Persistence API
If an entity instance be passed by value as a detached object, such as
through a session bean’s remote business interface, the class must
implement the Serializable interface.
I finally got it working.
I suspected of the String immutable nature and what I did was to encapsulate the 'word' in a class like the following:
public class Word {
private String word;
Word (String word) {
this.setWord(word);
}
public String getWord() {
return this.word;
}
public void setWord(String word) {
this.word = word;
}
}
After that, I also had to create a set inside the managed bean (controller) to hold the set of words, like 'private Set managedWordList;'.
I also changed the getter for the list, inside the managed bean, to convert from the String set to a Word set like:
public Set<Word> getWordList() {
if (this.selectedMainEntity != null){
this.wordList = new HashSet<Word>();
for (String word : this.selectedMainEntity.getWordList()) {
this.wordList.add(new Word(word));
}
}
return this.wordList;
}
Finally, I changed the dataTable to reference the new set. From:
<p:dataTable ... value="#{mainEntityController.selectedMainEntity.wordList}"... />
To:
<p:dataTable ... value="#{mainEntityController.wordList}"... />
And only then I got it to work as I expected.
i have this problem: if build a p:datatable from ArrayList it's impossible editing and sometimes rowSelectd not fire.
my xhtml code:
<h:form id="form">
<p:messages id="messageWiz" showDetail="true" closable="true" autoUpdate="true"/>
<h:panelGroup>
<p:commandButton id="addPattern" icon="ui-icon-add"
actionListener="#{testController.add}" process="#this"
update="patternsId" />
<p:commandButton id="deletePattern" icon="ui-icon-delete"
actionListener="#{testController.remove}"
process="#this" update="#this, patternsId"
disabled="#{empty testController.patternsSelected}" />
</h:panelGroup>
<p:dataTable id="patternsId" var="pattern" scrollable="true" scrollHeight="200"
value="#{testController.patterns}" editable="true"
editMode="cell" widgetVar="patterns" rowIndexVar="idx"
selection="#{testController.patternsSelected}"
rowKey="#{pattern}"
emptyMessage="#{label['msg.emptytable']}" styleClass="nofooter">
<p:ajax event="rowSelect" update=":form:deletePattern" />
<p:ajax event="rowUnselect" update=":form:deletePattern" />
<p:ajax event="rowSelectCheckbox" update=":form:deletePattern"/>
<p:ajax event="toggleSelect" update=":form:deletePattern" />
<p:ajax event="rowUnselectCheckbox" update=":form:deletePattern" />
<p:ajax event="cellEdit" update=":form:messageWiz" />
<p:column selectionMode="multiple" width="20" />
<p:column>
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{pattern}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{pattern}"
required="true" style="width:96%" autocomplete="off" />
</f:facet>
</p:cellEditor>
</p:column>
</p:dataTable>
</h:form>
controller code:
#ManagedBean
#ViewScoped
public class TestController implements Serializable {
private static final long serialVersionUID = 1L;
private ArrayList<String> patterns = new ArrayList<String>();
private ArrayList<String> patternsSelected;
public ArrayList<String> getPatterns() {
return patterns;
}
public void setPatterns(ArrayList<String> patterns) {
this.patterns = patterns;
}
public ArrayList<String> getPatternsSelected() {
return patternsSelected;
}
public void setPatternsSelected(ArrayList<String> patternsSelected) {
this.patternsSelected = patternsSelected;
}
public void add() {
patterns.add(new String());
}
public void remove() {
}
}
add row and edit, then close edit end inputed vaues disappear, rowSelected not fire.
You cannot achieve this with an arraylist of strings.
You must implement a list of Pattern objects (with at least id and value members)
Backing bean (testController) must implement SelectableDataModel (unique ids will help to be sure to identify and remove the right items).
I have a tree table and two buttons:
one for collapsing the tree and
the other for expanding,
but they don't work. At the backing bean I did root.setExpanded(true); and root.setExpanded(false); but it doesn't work.
<center>
<p:treeTable value="#{roleMB.root}" var="roleTreeTableVar"
binding="#{roleMB.treeTable}" id="roleTreeTable">
<f:facet name="header">
<center>
<p:commandButton value="Réduire tout"
icon="ui-icon-folder-collapsed" style="font-size: 0.9em"
actionListener="#{roleMB.expandAll}"
update=":roleTreeTableForm:roleTreeTable" />
<p:spacer width="30px" />
<p:commandButton value="Développer tout"
icon="ui-icon-folder-open" style="font-size: 0.9em"
actionListener="#{roleMB.collapseAll}"
update=":roleTreeTableForm:roleTreeTable" />
<p:spacer width="30px" />
</center>
</f:facet>
<p:column style="width:150px">
<f:facet name="header">
Nom de Role
</f:facet>
<h:outputText value="#{roleTreeTableVar.nom}" />
</p:column>
<p:column style="width:100px">
<f:facet name="header">
Id de role
</f:facet>
<h:outputText value="#{roleTreeTableVar.id}" />
</p:column>
<p:column style="width:20px">
<p:commandLink oncomplete="dlgAddRole.show()" value="Ajouter"
update=":addRoleForm:selectRolesNamesId">
<f:setPropertyActionListener value="#{roleTreeTableVar}"
target="#{roleMB.selectedRole}" />
</p:commandLink>
<p:commandLink oncomplete="delRole.show()" value="Supprimer">
<f:setPropertyActionListener value="#{roleTreeTableVar}"
target="#{roleMB.selectedRole}" />
</p:commandLink>
<p:commandLink oncomplete="editRole.show()" value="Modifier">
<f:setPropertyActionListener value="#{roleTreeTableVar}"
target="#{roleMB.selectedRole}" />
</p:commandLink>
</p:column>
</p:treeTable>
</center>
This is recursive method for Collapse and Expande.
public void collapsingORexpanding(TreeNode n, boolean option) {
if(n.getChildren().size() == 0) {
n.setSelected(false);
}
else {
for(TreeNode s: n.getChildren()) {
collapsingORexpanding(s, option);
}
n.setExpanded(option);
n.setSelected(false);
}
}
The variable n is the node than you want to expand or colapse.
When The variable option is false, all children of the n is
collapse, in the another case, is expanded
setSelected(false) indicate than this node isn't selected
You should set the expanded property to true not just for the root node but for all the children nodes as well.
A possible solution might look this:
view.xhtml
<h:form id="form">
...
<p:commandButton update=":form:tree" actionListener="#{view.collapseAll}"
icon="fa fa-minus" title="#{msg.collapseAll}"/>
<p:commandButton update=":form:tree" actionListener="#{view.expandAll}"
icon="fa fa-plus" title="#{msg.expandAll}"/>
...
<p:treeTable id="tree" ...>
...
</p:treeTable>
...
</h:form>
View.java
#Named
#ViewScoped
public class View implements Serializable {
private TreeNode root;
...
public void collapseAll() {
setExpandedRecursively(root, false);
}
public void expandAll() {
setExpandedRecursively(root, true);
}
private void setExpandedRecursively(final TreeNode node, final boolean expanded) {
for (final TreeNode child : node.getChildren()) {
setExpandedRecursively(child, expanded);
}
node.setExpanded(expanded);
}
}
For question: "how can this be realized?"
In code...
public void reloadTree() {
assocRoot = new DefaultTreeNode();
assocRoot.setExpanded(true);
TreeNode currentAssocNode;
AbstractDocument parentAssoc = getParentDocAssociation();
if(parentAssoc !=null) {
TreeNode parentAssocNode = new DefaultTreeNode(parentAssoc);
parentAssocNode.setExpanded(true);
....
I have a jsf page that I can't update the value of some checkboxes in a list of userDamain objects, in fact this is a part of my jsf page :
<h:outputText value="USER's rights list: "></h:outputText>
<p:dataTable id="userss" var="userr" value="#{userMB.userListe}">
<p:column>
<f:facet name="header"><h:outputText value="FirstName" /></f:facet>
<h:outputText value="#{userr.firstName}" />
</p:column>
<p:column>
<h:panelGrid columns="1" style="margin-bottom:10px" cellpadding="5">
<h:outputText value="Basic Usage: " />
<p:selectBooleanCheckbox value="#{userr.deletee}" immediate="true" actionListener="#{userr.deletee}" />
</h:panelGrid>
<p:commandButton value="Submit" update="display" oncomplete="dlg.show()" />
<p:dialog header="Selected Values" modal="true" showEffect="fade" hideEffect="fade" widgetVar="dlg">
<h:panelGrid columns="1" id="display">
<h:outputText value="Value 1: #{userr.deletee}" />
</h:panelGrid>
</p:dialog>
</p:column>
when I click on the boolean button 'Submit', the value of the userr.deletee is always false ( the default value ) and it's not updated anyway, I tried to use the Listner and actionListener in the booleanButton but it doesn't work, I also tried the postValidate event, so if someone has any idea of that, I would be graceful.
this is a part of my managed bean:
private List<UserDomain> userListe ;
public List<UserDomain> getUserListe() {
return this.userListe;
}
public void loadUserListe(){
this.userListe = new ArrayList<UserDomain>();
this.userListe.addAll(getUserServicee().getAll());
}
public void setUserListe(List<UserDomain> userListe) {
this.userListe = userListe;
}
a part of the userDomain bean:
...
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
#Transient
private boolean deletee;
public boolean isDeletee() {
return deletee;
}
public void setDeletee(boolean deletee) {
this.deletee = deletee;
}
I found out a solution which seems logic to me, I added to my checkbox an ajax behavior to execute a method and an attribute which called in this method to get the whole userDomain object, so on I can get the user.deletee value, here is a part of the code I m talking about:
<h:panelGrid columns="1" style="margin-bottom:10px" cellpadding="5">
<h:outputText value="Delete Right: " />
<p:selectBooleanCheckbox value="#{userr.deletee}">
<f:ajax execute="#this" listener="#{userMB.saveUserRights}" />
<f:attribute name="user" value="#{userr}" />
</p:selectBooleanCheckbox>
</h:panelGrid>
I hope this will be helpful for you
I have a dataTable that shows data stored in a database. One of the column contains a commandLink (p:commandLink) to edit selected row which is fine.
I have a problem during the rendering of my xhtml page. It seems that the method of backingBean in the actionListener attribute of commandLink is processed for every row in the table, but the actionListener should be processed only when link is clicked.
Here is my xhtml page (part of):
<h:form id="formElenco">
<p:dataTable id="dt1" value="#{myBean.itemList}" var="item">
<f:facet name="header">
<h:outputText value="header" />
</f:facet>
<p:column>
<f:facet name="header">
<h:outputText value="Name" />
</f:facet>
<h:outputText value="#{item.name}"/>
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="value" />
</f:facet>
<h:outputText value="#{item.value}"/>
</p:column>
<p:column>
<p:commandLink id="lnkItemUpdate" value="Edit"
onstart="document.getElementById('divUpdateItem').style.display = 'block';"
actionListener="#{myBean.updateItemAction(item)}" update=":formUpdateItem" />
</p:column>
</p:dataTable>
</h:form>
<div id="divUpdateItem" style="display:none" >
<h:form id="formUpdateItem">
Nome <h:inputText value="#{myBean.name}" /><br/>
Met <h:inputText value="#{myBean.value}" /><br/>
<h:inputHidden value="#{myBean.id}" />
<h:commandButton action="#{myBean.updateItemAction}" value="Save" />
</h:form>
</div>
Here are myBean's method (myBean is requestScoped):
public String updateItemAction(Entity item){
this.setId(item.getId());
this.setName(item.getName());
this.setValue(item.getValue());
return null;
}
public String updateItemAction() {
Entity entity = new Entity();
entity.setId(this.getId());
entity.setName(this.getName());
entity.setVAlue(this.getValue());
updateEntityQueryMethod(entity);
return null;
}
This isn't a valid method signature for an actionListener, so it's treated as a value expression by the <p:commandLink>.
You should be using action instead.
<p:commandLink id="lnkItemUpdate" value="Edit"
onstart="document.getElementById('divUpdateItem').style.display = 'block';"
action="#{myBean.updateItemAction(item)}" update=":formUpdateItem" />
Note that you can also just return void instead of a null String.
public void updateItemAction(Entity item) {
this.setId(item.getId());
this.setName(item.getName());
this.setValue(item.getValue());
}
A valid actionListener method signature would be a void method taking javax.faces.event.ActionEvent argument:
public void actionListener(ActionEvent event) {
// ...
}
See also:
Differences between action and actionListener