JSF与DWR的整合 --- 如何让JSF的ManagedBean来进行Reverse AJAX调用
①、在正常配置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
>
②、在jsp页面中,引入DWR的一些js脚本,并编写你自己的javascript,例如:
<
scripttype
=
'
text/javascript
'
src
=
'
/DWRTest/dwr/engine.js
'
>
</
script
>
<
scripttype
=
'
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方法
FacesContextfacesContext
=
FacesContext.getCurrentInstance();
ServletContextservletContext
=
(ServletContext)facesContext.getExternalContext().getContext();
ServerContextsctx
=
ServerContextFactory.get(servletContext);
Collectionsessions
=
sctx.getScriptSessionsByPage(
"
/DWRTest/test.faces
"
);
Utilpages
=
new
Util(sessions);
pages.addFunctionCall(
"
alertTest
"
,
"
hellodwr
"
);
注意,不能采用DWR在普通jsp中获取ServletContext的方法,因为在那种方式中,URL的解析是通过DWRServlet解析的,而在JSF中是由FacesServlet来解析的,所以使用WebContextFactory.get().getServletContext() 会得到NullPointerException.
①、在正常配置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,例如:
<
scripttype
=
'
text/javascript
'
src
=
'
/DWRTest/dwr/engine.js
'
>
</
script
>
< scripttype = ' text/javascript ' src = ' /DWRTest/dwr/util.js ' > </ script >
< script >
function alertTest(msg) ... {
alert(msg);
}
</ script >
< scripttype = ' 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方法
FacesContextfacesContext
=
FacesContext.getCurrentInstance();
ServletContextservletContext = (ServletContext)facesContext.getExternalContext().getContext();
ServerContextsctx = ServerContextFactory.get(servletContext);
Collectionsessions = sctx.getScriptSessionsByPage( " /DWRTest/test.faces " );
Utilpages = new Util(sessions);
pages.addFunctionCall( " alertTest " , " hellodwr " );
ServletContextservletContext = (ServletContext)facesContext.getExternalContext().getContext();
ServerContextsctx = ServerContextFactory.get(servletContext);
Collectionsessions = sctx.getScriptSessionsByPage( " /DWRTest/test.faces " );
Utilpages = new Util(sessions);
pages.addFunctionCall( " alertTest " , " hellodwr " );
注意,不能采用DWR在普通jsp中获取ServletContext的方法,因为在那种方式中,URL的解析是通过DWRServlet解析的,而在JSF中是由FacesServlet来解析的,所以使用WebContextFactory.get().getServletContext() 会得到NullPointerException.