DataModelSelection related problems
minateeseam Feb 6, 2010 2:12 PMI am new to seam and used @DataModel and @DataModelSelection for first time.
I want to share a couple of my findings about @DataModelSelection as mentioned bellow,
1.I have a data table in my xhtml as follows,
<rich:dataTable value=#{deviceList} var=”device”……> <rich:column> <f:facet …>Serial Number</f:facet> <a4j:commandLink action=”deviceActionBean.populateDeviceDetails” value=”#{device.serialNr}” /> </rich:column> <rich:column> <f:facet …>Model Name</f:facet> …… </rich:column> <rich:column> <f:facet …>Part Number</f:facet> <h:outputText value=”#{deviceActionBean.partNumber}” /> </rich:column> </rich:dataTable> <h:outputText value=”#{ deviceActionBean.selectedDevice }” />
And my corresponding Java class is,
@Name(“deviceActionBean”) @Scope(PAGE) Public class DeviceActionBean { @DataModel private List<Device> deviceList; @DataModelSelection private Device selectedDevice; public void populateDeviceDetails() { System.out.println(selectedDevice.serialNr); } public String getPartNumber() { …. Business logic …. return partNumber; } public Device getSelectedDevice () { System.out.println(selectedDevice.serialNr); return selectedDevice; } }
When I click on the commandlink of a particular row then the action method populateDeviceDetails is being invoked. In the action method I am getting the correct value of the selectedDevice which is annotated with @DataModelSelection. But after that when control comes to the getter method of selectedDevice then the value of selectedDevice is always coming the value of the last row of the dataTable.
This problem ate my head a lot. After one day struggle I found the actual problem. It is like this, I am calling another method as getPartNumber to display part number for every device in the dataTable.
<rich:column> <f:facet …>Part Number</f:facet> <h:outputText value=”#{deviceActionBean.partNumber}” /> </rich:column>
So, BijectionInterceptor again does injection and outjection for the method getPartNumber. At this time the value of selectedDevice is changed as the selectedIndex of DataModel changes. And this method getPartNumber is called for every row, consequently the final value of selectedDevice is always last row of the dataTable.
So, this scenario forces me to conclude that if we are using @DataModelSelection then we should not suppose to use any other method in the dataTable. In other words, no method should be called, after the action method of the link or button in that dataTable has been invoked. But if you want to invoke some other methods then you have to use @BypassInterceptor annotation for those methods otherwise you will loose the selected value. But use of @BypassInterceptor annotation will not be the convenient approach if your method really requires injected values to execute the business logic :(
2.@DataModelSelection does not work if the link doesn’t have any action method as BijectionInterceptor is not being invoked which does the injection of data model selection.
So, I didn’t get any such advantage of using @DataModelSelection. Rather I changed my code like this,
<rich:dataTable value=#{deviceList} var=”device”……> <rich:column> <f:facet …>Serial Number</f:facet> <a4j:commandLink action=”deviceActionBean.populateDeviceDetails(device)” value=”#{device.serialNr}” /> </rich:column> ...... @Name(“deviceActionBean”) @Scope(PAGE) Public class DeviceActionBean { @DataModel private List<Device> deviceList; private Device selectedDevice; public void populateDeviceDetails(Device selectedDevice) { selectedDevice= selectedDevice; }
I passed the device object to my action method as parameter.
Any suggestions about my findings for @DataModelSelection would be appreciated. Please correct me if I am wrong.
Thanks in advance :)
Janaki.