日期:2014-05-16  浏览次数:20459 次

JSF中配置dwr

本文以实例形式描述在jsf中配置dwr的方法。

?

应用要求:在javascript方法中调用后台Bean的方法和Spring容器中服务的方法。

?

步骤如下:

? ? ?1、??JAR包支持

?

dwr.jarjson-lib-2.3-jdk13.jarezmorph-1.0.6.jar

说明:依赖的jar包:commons-collections-XX.jar,版本必须在3.0以上

?

2、web.xml配置DwrServlet和FacesExtensionFilter

?

<!-- dwr begin -->
  <servlet>
    <servlet-name>dwr-invoker</servlet-name>
    <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
    <init-param>
      <param-name>debug</param-name>
      <param-value>true</param-value>
    </init-param>
    <init-param>
      <param-name>crossDomainSessionSecurity</param-name>
      <param-value>false</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dwr-invoker</servlet-name>
    <url-pattern>/dwr/*</url-pattern>
  </servlet-mapping>
  <!--DWR/Faces 过滤器允许你不在JSF的生命周期里调用FacesContext中的Bean。-->
  <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>
 <!-- dwr end -->

?

?

?

<!--[if !supportLists]-->? ?3、? <!--[endif]-->WEB-INF下建立dwr.xml

?

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.org/dwr/dwr20.dtd">
<dwr>
	<allow>
		<!--调用jsf后台Bean的方法-->
		<create creator="jsf" javascript="BranchMerg">
			<param name="managedBeanName" value="BranchMerg" />
			<param name="class"
				value="cn.ccb.zjtsrmbp.security.web.bb.BranchMerg" />
		</create>
<!--直接调用spring容器内服务的方法-->
<create javascript="billBranch" creator="spring">
			<param name="beanName" value="securityBillBranchNewUCCTarget" />	
		</create>
	</allow>
</dwr>

?

?4、? Service里的方法范例

?

public String getSubBillBranch(String orgNo) {

	List objects=XXX;//可序列化的对象
	JSONArray jsonArray = JSONArray.fromObject(objects);//通过json方式进行序列化
	String jsonStr = jsonArray.toString();
	return jsonStr;
}

?

5、JS方法调用DWR

?

<script type="text/javascript"
		src="<%=request.getContextPath()%>/js/util.js"></script>
	<script type='text/javascript'
		src='<%=request.getContextPath()%>/dwr/engine.js'></script>
	<!---下面的billBranch.js名称对应dwr.xml里的配置--->
	<script type='text/javascript'
		src='&