DataTable中点击link/button修改某行数据...可以通过以下几种方法来实现:
1> 利用<f:param>来传递参数,如id等...
页面代码如下:
<t:dataTable var="emp" .... >
<h:commandLink id="editLink" action="#{employeeAction.prepareEdit}">
<h:outputText value="#{msg.edit}"/>
<f:param name="id" value="#{emp.id}"/>
</h:commandLink>
后台取得参数代码:
FacesContext context = FacesContext.getCurrentInstance();
Map map = context.getExternalContext().getRequestParameterMap();
String employeeID = (String) map.get("id");
1> 利用<f:param>来传递参数,如id等...
页面代码如下:
程序代码
<t:dataTable var="emp" .... >
<h:commandLink id="editLink" action="#{employeeAction.prepareEdit}">
<h:outputText value="#{msg.edit}"/>
<f:param name="id" value="#{emp.id}"/>
</h:commandLink>
后台取得参数代码:
程序代码
FacesContext context = FacesContext.getCurrentInstance();
Map map = context.getExternalContext().getRequestParameterMap();
String employeeID = (String) map.get("id");
2> 通过actionListeners来获得点击的是哪一行,从而获得dataTable,将之转化为YourBeanClass...即可得到那个实例,即那行数据...这样做的好处是:不需要再去数据库拿那条数据...
由此可见,JSF把页面上的控件也是放在session中进行管理的...,具体实现如下:
页面代码如下:
<h:dataTable value="#{ResultsBean.hitSet.hits}" var="hit">
<h:column>
<h:commandLink>
<f:actionListener type="net.java.OrderActionListener" />
<h:outputText value="Order" />
</h:commandLink>
...
</h:column>
</h:dataTable>
后台代码:
public class orderActionListener implements ActionListener {
public void processAction(ActionEvent anEvent) throws AbortProcessingException {
YourBeanClass tmpBean = null;
UIComponent tmpComponent = anEvent.getComponent();
while (null != tmpComponent && !(tmpComponent instanceof UIData)) {
tmpComponent = tmpComponent.getParent();
}
if (tmpComponent != null && (tmpComponent instanceof UIData)) {
Object tmpRowData = ((UIData) tmpComponent).getRowData();
if (tmpRowData instanceof YourBeanClass) {
tmpBean = (YourBeanClass) tmpRowData;
//TODO Implementation of your method
}
}
//TODO Exception Handling if UIData not found or tmpRowBean of wrong type
}
}
由此可见,JSF把页面上的控件也是放在session中进行管理的...,具体实现如下:
页面代码如下:
程序代码
<h:dataTable value="#{ResultsBean.hitSet.hits}" var="hit">
<h:column>
<h:commandLink>
<f:actionListener type="net.java.OrderActionListener" />
<h:outputText value="Order" />
</h:commandLink>
...
</h:column>
</h:dataTable>
后台代码:
程序代码
public class orderActionListener implements ActionListener {
public void processAction(ActionEvent anEvent) throws AbortProcessingException {
YourBeanClass tmpBean = null;
UIComponent tmpComponent = anEvent.getComponent();
while (null != tmpComponent && !(tmpComponent instanceof UIData)) {
tmpComponent = tmpComponent.getParent();
}
if (tmpComponent != null && (tmpComponent instanceof UIData)) {
Object tmpRowData = ((UIData) tmpComponent).getRowData();
if (tmpRowData instanceof YourBeanClass) {
tmpBean = (YourBeanClass) tmpRowData;
//TODO Implementation of your method
}
}
//TODO Exception Handling if UIData not found or tmpRowBean of wrong type
}
}