model.getRowData() always return first element

JSP:

<h:dataTable value='#{ordersmanager.openOrders}' var='item' border="1" cellpadding="2" cellspacing="0">

<h:column>

<f:facet name="header">

<h:outputText value="Cerrar pedido"/>

</f:facet>

<h:commandLink value="Cerrar pedido" action="#{ordersmanager.end}">

<f:param name="idpedido" value="#{item.idpedido}"/>

</h:commandLink>

</h:column>

</h:dataTable>

Ordersmanager BackingBean

public String end(){

Pedido order =new Pedido();

order = (Pedido) model.getRowData();

System.out.println("Row index: " + model.getRowIndex())

order.setFechaFin(new Date());

pedidoFacade.edit(order);

returnnull;

}

I always get "RowIndex: 0" no matter wich column i have selected and model.getRowData() always returns first order

I read this thread but it didn't help me:

http://forum.java.sun.com/thread.jspa?threadID=738070

Could you please help me with that?

Thanks,

Bartolom?Molina

[1612 byte] By [elbartoa] at [2007-11-27 0:11:54]
# 1

You're not using model in the datatable. You're using openOrders in the datatable.

Do it as follows:<h:dataTable value="#{myBean.dataModel}" var="item">

MyBeanprivate HtmlDataModel dataModel; // + getter + setter

public void action() {

Order order = (Order) dataModel.getRowData();

}

Or:<h:dataTable binding="#{myBean.dataTable}" value="#{myBean.dataList}" var="item">

MyBeanprivate HtmlDataTable dataTable; // + getter + setter

private List dataList; // + getter + setter

public void action() {

Order order = (Order) dataTable.getRowData();

}

By the way, if you're not using navigation cases for actions, you can just declare them as void instead of returning null or empty string.

BalusCa at 2007-7-11 21:53:36 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

> You're not using model in the datatable. You're using

> openOrders in the datatable.

Yes, I am using model. openOrders returns a dataModel object!!:

private DataModel model;

public DataModel getOpenOrders() {

FacesContext context = FacesContext.getCurrentInstance();

Farmacia pharmacy = (Farmacia) context.getExternalContext().getSessionMap().get(PHARMACY_SESSION_KEY);

Residencia nursing = (Residencia) context.getExternalContext().getSessionMap().get(NURSING_SESSION_KEY);

ResidenciaHasFarmacia nursingHasPharmacy = residenciaHasFarmaciaFacade.find(new ResidenciaHasFarmaciaPK(pharmacy.getIdfarmacia(), nursing.getIdresidencia()));

model = new ListDataModel(pedidoFacade.findOpenOrders(nursingHasPharmacy));

return model;

}

And that code works ok because the table shows the data properly

elbartoa at 2007-7-11 21:53:36 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

You're renewing the DataModel everytime when the getter is invoked.

Change it to public DataModel getOpenOrders() {

if (model == null) {

FacesContext context = FacesContext.getCurrentInstance();

Farmacia pharmacy = (Farmacia) context.getExternalContext().getSessionMap().get(PHARMACY_SESSION_KEY);

Residencia nursing = (Residencia) context.getExternalContext().getSessionMap().get(NURSING_SESSION_KEY);

ResidenciaHasFarmacia nursingHasPharmacy = residenciaHasFarmaciaFacade.find(new ResidenciaHasFarmaciaPK(pharmacy.getIdfarmacia(), nursing.getIdresidencia()));

model = new ListDataModel(pedidoFacade.findOpenOrders(nursingHasPharmacy));

}

return model;

}

BalusCa at 2007-7-11 21:53:36 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

> You're renewing the DataModel everytime when the

> getter is invoked.

That is what I want because the user can add new orders and deliver open orders, so the table must update on every request. Despite, I tried to do what you said (check if model is null before update) and still get: "Row Index: 0" everytime I select a row.

elbartoa at 2007-7-11 21:53:36 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
Which JSF version are you using?Try it the other way, by HtmlDataTable#getRowData(). Also see http://balusc.xs4all.nl/srv/dev-jep-dat.html
BalusCa at 2007-7-11 21:53:36 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

Thanks a lot. When i put

binding='#{ordersmanager.openOrdersDataTable}'

in the JSP datatable and add

private HtmlDataTable openOrdersDataTable;

(+ getters/setters), it worked fine.

One last question: if I have 2 or more tables in the same jsp, should i have one DataModel/HtmlDataTable for each, or one for all? (in the backing bean)

elbartoa at 2007-7-11 21:53:36 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
One for each. The componentbindings ought to be unique.
BalusCa at 2007-7-11 21:53:36 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...