---
title: "richfaces <a4j:support>控件简单使用"
author: "stvanstar"
date: "2009-10-22T22:05:53Z"
tags: []
slug: "richfaces-a4j-support"
draft: false
lastmod: "2009-10-22T22:05:53Z"
id: 501171
url: "https://stvanstar.iteye.com/blog/501171"
---

### ・ richfaces配置

1.导入richfaces-api-3.3.0.GA.jar richfaces-impl-3.3.0.GA.jar richfaces-ui-3.3.0.GA.jar，放到WEB-INF\\lib下面

2.web.xml里加入

```
	<filter>
		<display-name>RichFaces Filter</display-name>
		<filter-name>richfaces</filter-name>
		<filter-class>org.ajax4jsf.Filter</filter-class>
	</filter>
	
	<filter-mapping>
		<filter-name>richfaces</filter-name>
		<url-pattern>/faces/*</url-pattern>
		<dispatcher>FORWARD</dispatcher>
		<dispatcher>INCLUDE</dispatcher>
		<dispatcher>REQUEST</dispatcher>
	</filter-mapping>
```

 有了这段配置，页面中用到的richfaces tag所需要的js，css等就可以从jar包里load到jsp里

3.jsp里导入taglib

```
<%@ taglib uri="http://richfaces.org/a4j" prefix="a4j"%>
<%@ taglib uri="http://richfaces.org/rich" prefix="rich"%>
```

### ・ &lt;a4j:support&gt;控件说明

The &lt;a4j:support&gt; component is the most important core component in the RichFaces library. It enriches any existing non-Ajax JSF or RichFaces component with Ajax capability. All other RichFaces Ajax components are based on the same principles &lt;a4j:support&gt; has.

     两个主要的属性：

[*event ： 定义javascript事件，当此事件被触发时，触发*]() [Ajax support来实现和后台的交互]()

[*reRender : 定义当后台返回请求数据后，画面上需要刷新的组建的 ID，复数时用逗号【，】分隔*]()

     其他常用属性：

action，actionListener：指定后台事件

onsubmit：指定在提交时执行的javascript代码

oncomplete：指定当请求处理完成后需要执行的javascript代码

focus:指定当请求处理完成后需要设定focus的控件的ID

 

### ・ 代码

    1.datatable的row click 事件实现

    jsp代码

 

```
<rich:dataTable id="table1" border="0" cellpadding="0" 
   cellspacing="0" width="100%"value="#{bean.datalist}" 
   var="vardatalist" rows="10">
    <a4j:support event="onRowClick" actionListener="#{bean.doRowClickAction}" 
      reRender="id_name" onsubmit="return confirm();" oncomplete="return info();"/>
   <rich:columnGroup>
       <rich:column width="20%" >
             <h:outputText styleClass="outputText" id="id060"
							value="#{vardatalist.userNm}">
	      </h:outputText>
              ...
       </rich:column>
    </rich:columnGroup>
</rich:dataTable>
```

   onRowClick 代码

```
	public String doRowClickAction(ActionEvent event)
	{
		try
		{
			UIComponent comp = event.getComponent();
			Object obj = comp.getParent();
			if (obj instanceof HtmlDataTable) {
				HtmlDataTable table = (HtmlDataTable) obj;
			        // 取得选择的行
				int rowindex = table.getRowIndex();
                                // 省略。。。
			}
		}
		catch (Exception e)
		{
			errorProcess(e);
		}
		return Constants.EMPTY_STRING;
	}
```

 

2.comboBox的选择事件

 

```
	<h:selectOneMenu styleClass="selectOneMenu" id="item" value="#{bean.itemCd}"
		     valueChangeListener="#{bean.handleItemValueChange}">
		<f:selectItems value="#{bean.itemPullDownList}" />
               <a4j:support event="onchange"  reRender="id1,id2"></a4j:support>
	</h:selectOneMenu>
			
```

 

**参考文档**

[http://docs.jboss.org/richfaces/latest\_3\_3\_X/en/devguide/html/a4j\_support.html](http://docs.jboss.org/richfaces/latest_3_3_X/en/devguide/html/a4j_support.html)

[http://livedemo.exadel.com/richfaces-demo/richfaces/support.jsf?tab=info&cid=815798](http://livedemo.exadel.com/richfaces-demo/richfaces/support.jsf?tab=info&cid=815798 "http://livedemo.exadel.com/richfaces-demo/richfaces/support.jsf?tab=info&cid=815798")