java.lang.NumberFormatException: Trying to extract rowIndex from clientId - jsf

I'm developing a dynamic form using Mojarra 2.2.5 and Primefaces 4.0 and i need to implement a datatable with dynamic content. Unfortunately when i try to submit form appears this error:
java.lang.NumberFormatException: Trying to extract rowIndex from clientId 'id_of_my_datatable:j_id1:j_id1'
I generated datatable by code using it, inspired by Primefaces Showcase (http://primefaces.org/showcase/ui/datatableDynamicColumns.jsf) and from (Stack Overflow discussion)
private DataTable addList(UIComponent parent, String labelStr, String id, List<ColumnModel> rowItems, List<Map<String, String>> values, String styleClass,
boolean isRequired, String updateComponents)
{
OutputPanel outputPanel = new OutputPanel();
outputPanel.setStyleClass("question_panel col-md-11");
if (labelStr != null)
{
addLabel(outputPanel, id, labelStr, "component_label col-md-11");
}
DataTable dataTable = new DataTable();
dataTable.setValue(values);
//mapModel = values;
//ValueExpression veTable = super.createValueExpression("#{composerTestBean.mapModel}", List.class);
//dataTable.setValueExpression("value", veTable);
dataTable.setVar("model");
dataTable.setId(id);
dataTable.setRowIndexVar("rowIndex");
Columns columns = new Columns();
columns.setValue(rowItems);
//columnModel = rowItems;
//ValueExpression veColumns = super.createValueExpression("#{composerTestBean.columnModel}", List.class);
//columns.setValueExpression("value", veColumns);
columns.setVar("column");
columns.setColumnIndexVar("colIndex");
HtmlOutputText headerText = new HtmlOutputText();
ValueExpression veHeader = super.createValueExpression("#{column.header}", String.class);
headerText.setValueExpression("value", veHeader);
columns.getFacets().put("header", headerText);
HtmlOutputText valueText = new HtmlOutputText();
ValueExpression veValue = super.createValueExpression("#{model[column.property]}", String.class);
valueText.setValueExpression("value", veValue);
columns.getChildren().add(valueText);
dataTable.getChildren().add(columns);
outputPanel.getChildren().add(dataTable);
parent.getChildren().add(outputPanel);
return dataTable;
}
As you can see I've also tried to use valueExpression and the property bean binding, but nothing is changed. setRowIndexVar() and setColumnIndexVar() methods are just attempts to resolve the question.
I also read that it could be a Mojarra bug on this discussion (it should be resolved) and in another one that is a Primefaces bug (I can't apply patches on project libraries).
Also I try to follow this post and this remove this exception but brings others problem to my page about update.
Do You think I'm doing something wrong? Or Is it just an unresolvable case?
Thanks in advance.

Related

MethodExpression not firing in HtmlCommandLink

I have a dynamically generated Datatable, like this
DataTable dataTable = new DataTable();
dataTable.setValue(relatorioVOList);
dataTable.setVar("rVO");
Column checkBoxColumn = new Column();
checkBoxColumn.getChildren().add(this.viewComponentBuilder.createExpressionTextWithLink("#{rVO.iRelatorio}","#{rVO.nNome}"));
dataTable.getColumns().add(checkBoxColumn);
public HtmlForm createExpressionTextWithLink(String iRelatorioExpressionValue, String valueExpressionValue) {
HtmlForm form = new HtmlForm();
HtmlCommandLink link = new HtmlCommandLink();
//config
FacesContext context = FacesContext.getCurrentInstance();
Application application = context.getApplication();
ExpressionFactory ef = application.getExpressionFactory();
ELContext elc = context.getELContext();
//value that is the reports name
ValueExpression nameValueExp = ef.createValueExpression(elc, valueExpressionValue, Object.class);
link.setValueExpression("value", nameValueExp);
//action that goes to method teste when link is clicked
MethodExpression methodExpression = createMethodExpression("#{componenteC.teste(rVO.iRelatorio)}", String.class, Integer.class);
link.setActionExpression(methodExpression);
form.getChildren().add(link);
return form;
}
private static MethodExpression createMethodExpression(String expression, Class<?> returnType, Class<?>... parameterTypes) {
FacesContext facesContext = FacesContext.getCurrentInstance();
return facesContext.getApplication().getExpressionFactory().createMethodExpression(
facesContext.getELContext(), expression, returnType, parameterTypes);
}
In ComponenteC, a RequestScopedBean, the teste function
public String teste(Integer iRelatorio) {
System.out.println("cheguei");
return "componente";
}
The goal is that the teste function will generate an url according to the iRelatorio parameter.
The issue here is that the function is never called. I tried replacing the rVO.iRelatorio with an explicit 10, "#{componenteC.teste(10)}" and even then the action seems to not be fired.
The report name is displayed correctly.
Dynamically created UIInput, UICommand and UINamingContainer components must have a fixed id assigned. Otherwise it will get an autogenerated one which is not necessarily the same when the view is restored. The component ID is used in request parameter names in submitted form data, which JSF would then use to collect the submitted input values and identify the invoked commands during apply request values phase. If the component ID changes, then JSF won't be able to perform the apply request values phase as intented.
Thus, act accordingly:
dataTable.setId("tableId");
// ...
form.setId("formId");
// ...
link.setId("linkId");
There are other potential causes, but they are not visible in the information provided so far in the question. To cover that, take your time to carefully read the following related answers on "dynamically" creating components/views:
Create inputtext dynamically
How does the 'binding' attribute work in JSF? When and how should it be used?
How to create dynamic JSF form fields
That said, you're really better off using XHTML to declare and create components instead of all that Java code mess. XHTML(+XML) is much more declarative and readable and therefore better understandable and maintainable. JSTL may be very helpful in this all.

How to add DataList to panel programmatically with PrimeFaces?

I am new to PrimeFaces and I need little help.
I want to add dynamic dashboard and into each panel add datalist.
I can see dashboard with datalists, but there are no strings in rows.
Thanks for your help
My code:
Panel panel=(Panel)application.createComponent(fc,"org.primefaces.component.Panel","org.primefaces.component.PanelRenderer");
panel.setId("measure_" + i);
panel.setHeader(lists.get(i).getName());
panel.setToggleable(true);
panel.setVisible(true);
DataList dataList = (DataList) application.createComponent(fc, "org.primefaces.component.DataList", "org.primefaces.component.DataListRenderer");
List<String> test = new ArrayList();
test.add("foo");
test.add("foo2");
dataList.setValue(test);
panel.getChildren().clear();
panel.getChildren().add(dataList);
getDashboard().getChildren().add(panel);
DashboardColumn column = model.getColumn(i%getColumnCount());
column.addWidget(panel.getId());
Try to use value expression approach:
#ManagedBean(name="bean")
public class ManagedBean{
public List<String> test;
//getter and setter of test
...
DataList dataList = (DataList) application.createComponent(fc, "org.primefaces.component.DataList", "org.primefaces.component.DataListRenderer");
//init test on PostConstruct Method
this.getTest().add("foo");
this.getTest().add("foo2");
ValueExpression ve = application.getExpressionFactory().createValueExpression(fc.getELContext(), "#{bean.test}", Collection.class);
dataList.setValueExpression("value", ve);
...
}
Actually I doesn't test that codelines, but I used this approch successfully and I think is more useful because you can binding datamodel to bean and doesn't lose control of your data.

Dynamically created input components do not show model values

I have some fields of a bean that I need to show programmatically. Is something like this:
HtmlPanelGrid panel = new HtmlPanelGrid();
panel.setColumns(4);
HtmlOutputLabel fieldOut = new HtmlOutputLabel();
fieldOut.setId("fieldOutId");
fieldOut.setValue("name");
panel.getChildren().add(fieldOut);
HtmlInputText fieldIn = new HtmlInputText();
fieldIn.setId("fieldInId");
fieldIn.setPartialSubmit(true);
fieldIn.setValueExpression(
"field", UtilFaces.createValueExpression("#{newElementBean.fieldName}",String.class));
panel.getChildren().add(fieldIn);
mainForm.getChildren().add(panel);
In newElements.xhtml i've defined a form which is binded to mainForm, in this way:
<ice:form binding="#{newElementBean.mainForm}">
<ice:inputText id="ANOTHERFIELD" value="#{newElementBean.anotherField}"/>
<ice:commandButton action="#{newElementBean.save}" value="Save"/>
</ice:form>
When I click on the save button and I go to the next view, the field "ANOTHERFIELD" has taken the value from the bean, and shows up correctly, but the fields that were dinamically generated shows empty. Its values in the backing bean also are null. It's like the ValueExpression is not working for the HtmlInputText that I created in the backing bean. I'm using Icefaces 3.3 with Mojarra 2.1.17.
How is this caused and how can I solve it?
I solved it. I made two mistakes:
The proper setValueExpression call is like this:
fieldIn.setValueExpression("value", UtilFaces.createValueExpression("#newElementBean.fieldName}");
I was incorrectly using "field1" as 1st argument instead of "value".
This is not visble in the question, but my createValueExpression() helper method was also wrong. The following is working for me:
public static ValueExpression createValueExpression(String expression) {
Application app = FacesContext.getCurrentInstance().getApplication();
ExpressionFactory elFactory = app.getExpressionFactory();
ELContext elContext = FacesContext.getCurrentInstance().getELContext();
return elFactory.createValueExpression(elContext, expression, Object.class);
}

Richfaces 4 - substitute for AjaxActionComponent

We are migrating an application from jsf 1.2 to jsf 2.0, and upgrading Ajax4JSF (ajax4jsf-1.1.1) to Richfaces 4.2.2.
In our old code, there are places where we use org.ajax4jsf.framework.ajax.AjaxActionComponent to programmatically set the 'rerender' attribute of some components. The relevant code is as follows:
public void changeReRender(UIComponent comp){
AjaxActionComponent commandAjax = (AjaxActionComponent)comp;
HashSet values = new HashSet();
values.add("idToBeRerendered");
commandAjax.setReRender(values);
}
But in Richfaces 4 the AjaxActionComponent class was removed.
Is there a class that can be used instead of AjaxActionComponent, or another way to programmatically change the 'rerender' attribute of UIComponent?
In JSF2, there's a more generic approach to programmatically rendering ids using the PartialViewContext object. Try the following snippet
public void changeReRender(UIComponent comp){
// AjaxActionComponent commandAjax = (AjaxActionComponent)comp;
// HashSet values = new HashSet();
// values.add("idToBeRerendered");
// commandAjax.setReRender(values);
FacesContext context = FacesContext.getCurrentInstance();
PartialViewContext partialContext = context.getPartialViewContext(); //obtain a reference to the PartialViewContext object
//add the desired id to the list of render ids
partialContext.getRenderIds().add("idToBeRerendered");
}

Refresh JSF validator attributes on rerender

I have an issue with the attributes values of a validator component.
Apparently the validator is created when I first visit a page.
Please see my code below:
<h:inputText value="#{qsetting.value}" rendered="#{qsetting.dataType=='Double'}">
<mw:validateRange min="#{qsetting.minValue}" max="#{qsetting.maxValue}" />
</h:inputText>
The inputText component is rerendered through ajax but apparently, including the value that is displayed.
Unfortunately, the qsetting.minValue and qsetting.maxValue are not refreshed, causing my validator to not work correctly.
Is there a possibility to refresh the validator, to make sure it re-retrieves its attributes or to just create a new instance of the validator?
The validator class itself is currently implementing "Validator, Serializable".
Also, I'm using jsf1.2 with facelets...
Thanks,
Steven
I've hit this problem in a non-ajax environment a few times over the years, and hit it again today. The addition of Ajax doesn't really change anything since a validator attribute is never evaluated again once the page is initially built, ajax or otherwise.
The only solution I've come up with is to set the validator attribute to a validator expression, then evaluate that expression inside the validate method.
One other issue I hit (also with JSF 1.2 and Facelets) is that not all EL variables worked. I had to use a static managed bean as the root of my expression to access the value. A facelet ui:param value as a root would not work. I haven't tested to see what else may not correctly evaluate. This could be due to another bug in the design of JSF itself. See http://myfaces.apache.org/core12/myfaces-api/apidocs/javax/faces/context/FacesContext.html#getELContext%28%29.
For example, instead of:
max="#{qsetting.maxValue}"
use
maxExpression="qsetting.maxValue"
Then
public String getMax(FacesContext context) {
Application app = context.getApplication();
ExpressionFactory exprFactory = app.getExpressionFactory();
ValueExpression ve = exprFactory.createValueExpression(context.getELContext(),
"#{" + getMaxExpression() + "}",
String.class);
Object result = ve.getValue(context.getELContext());
return (String)result;
}
public String getMaxExpression() {
return this.maxExpression;
}
public void setMaxExpression(String maxExpression) {
this.maxExpression = maxExpression;
}
//// StateHolder
public boolean isTransient() {
return isTransient;
}
public void setTransient(boolean newTransientValue) {
isTransient = newTransientValue;
}
public Object saveState(FacesContext context) {
Object[] state = new Object[1];
state[0] = maxExpression;
return state;
}
public void restoreState(FacesContext context, Object state) {
Object[] values = (Object[]) state;
maxExpression = (String) values[0];
}
UPDATE 2012-09-19:
After investigating how MyFaces Commons solves this problem, the better solution is to change the rules Facelets uses to evaluate validator and converter attribute expressions.
It basically comes down to adding a new validator or converter MetaRule which, when applied, checks to see if the attribute value is non-literal. If it is non-literal, call a special method on your validator or converter which passes in the value expression rather than the current value.
http://svn.apache.org/viewvc/myfaces/commons/trunk/myfaces-commons-validators/src/main/java/org/apache/myfaces/commons/validator/_ValidatorRule.java?view=markup
The validator at that point needs to store the value expression as state and evaluate it when needed. MyFaces commons provides all of the complicated infrastructure to make this happen generically, but you could dump all of that and write a simple custom rule and directly manage the ValueExpression yourself, similar to what I originally posted.

Resources