①、在正常配置DWR(包括reverse ajax配置)及JSF之后在web.xml中做如下定义:
<
filter
>
< filter-name > DwrFacesFilter </ filter-name >
< filter-class >
org.directwebremoting.faces.FacesExtensionFilter
</ filter-class >
</ filter >
< filter-mapping >
< filter-name > DwrFacesFilter </ filter-name >
< url-pattern > /dwr/* </ url-pattern >
</ filter-mapping >
< filter-name > DwrFacesFilter </ filter-name >
< filter-class >
org.directwebremoting.faces.FacesExtensionFilter
</ filter-class >
</ filter >
< filter-mapping >
< filter-name > DwrFacesFilter </ filter-name >
< url-pattern > /dwr/* </ url-pattern >
</ filter-mapping >
②、在jsp页面中,引入DWR的一些js脚本,并编写你自己的javascript,例如:
<
script type
=
'
text/javascript
'
src
=
'
/DWRTest/dwr/engine.js
'
>
</
script
>
< script type = ' text/javascript ' src = ' /DWRTest/dwr/util.js ' > </ script >
< script >
function alertTest(msg) {
alert(msg);
}
</ script >
< script type = ' text/javascript ' src = ' /DWRTest/dwr/util.js ' > </ script >
< script >
function alertTest(msg) {
alert(msg);
}
</ script >
在<body>标签中增加一个reverse ajax的显式设置:<body onload="dwr.engine.setActiveReverseAjax(true);">
③、ManagedBean中,写一个actionListener方法
FacesContext facesContext
=
FacesContext.getCurrentInstance();
ServletContext servletContext = (ServletContext)facesContext.getExternalContext().getContext();
ServerContext sctx = ServerContextFactory.get(servletContext);
Collection sessions = sctx.getScriptSessionsByPage( " /DWRTest/test.faces " );
Util pages = new Util(sessions);
pages.addFunctionCall( " alertTest " , " hello dwr " );
ServletContext servletContext = (ServletContext)facesContext.getExternalContext().getContext();
ServerContext sctx = ServerContextFactory.get(servletContext);
Collection sessions = sctx.getScriptSessionsByPage( " /DWRTest/test.faces " );
Util pages = new Util(sessions);
pages.addFunctionCall( " alertTest " , " hello dwr " );
注意,不能采用DWR在普通jsp中获取ServletContext的方法,因为在那种方式中,URL的解析是通过DWRServlet解析的,而在JSF中是由FacesServlet来解析的,所以使用WebContextFactory.get().getServletContext() 会得到NullPointerException.
此处不用在dwr.xml中定义相应的内容,因为客户端不需要调用服务器端,而是服务器端调用客户端。