引:dwr实例

开发环境:eclipse3.1.2  myeclipse4.1.1   Tomcat5.0.28   Mysql5.0
dwr类包版本是1.1

步骤一:配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
    <servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
        <init-param>
            <param-name>config</param-name>
            <param-value>/WEB-INF/struts-config.xml</param-value>
        </init-param>
        <init-param>
            <param-name>debug</param-name>
            <param-value>3</param-value>
        </init-param>
        <init-param>
            <param-name>detail</param-name>
            <param-value>3</param-value>
        </init-param>
        <load-on-startup>0</load-on-startup>
    </servlet>


    <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>dwr-invoker</servlet-name>
        <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
        <init-param>
            <param-name>debug</param-name>
            <param-value>true</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>dwr-invoker</servlet-name>
        <url-pattern>/dwr/*</url-pattern>
    </servlet-mapping>
    
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml
        </param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>  
      
    <filter>
        <filter-name>EncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>

    </filter>
    <filter-mapping>
        <filter-name>EncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

      <error-page>
        <error-code>404</error-code>
        <location>/404.jsp</location>
    </error-page>

    <error-page>
        <error-code>500</error-code>
        <location>/500.jsp</location>
    </error-page>

    <taglib>
        <taglib-uri>/WEB-INF/struts-bean</taglib-uri>
        <taglib-location>/WEB-INF/tld/struts-bean.tld</taglib-location>
    </taglib>
    <taglib>
        <taglib-uri>/WEB-INF/struts-logic</taglib-uri>
        <taglib-location>/WEB-INF/tld/struts-logic.tld</taglib-location>
    </taglib>
    <taglib>
        <taglib-uri>/WEB-INF/struts-html</taglib-uri>
        <taglib-location>/WEB-INF/tld/struts-html.tld</taglib-location>
    </taglib>
    <taglib>
        <taglib-uri>/WEB-INF/struts-tiles</taglib-uri>
        <taglib-location>/WEB-INF/tld/struts-tiles.tld</taglib-location>
    </taglib>
    <taglib>
        <taglib-uri>/WEB-INF/ntu</taglib-uri>
        <taglib-location>/WEB-INF/tld/ntu.tld</taglib-location>
    </taglib>

</web-app>


二:spring中注入相关service
<bean id="ajaxTestService" class="com.lion.cms.domain.service.AjaxTestServiceImp">
        <property name="commonDAO">
            <ref bean="commonDAO" />
        </property>
    </bean>


三:AjaxTestServiceImp代码
public class AjaxTestServiceImp implements IAjaxTestService {
   
    private ICommonDAO commonDAO;

      
    public void setCommonDAO(ICommonDAO commonDAO) {
        this.commonDAO = commonDAO;
    }

    public List getEmployeeById(String deptid) {
        DetachedCriteria detachedCriteria=DetachedCriteria.forClass(Employee.class);
        detachedCriteria.setFetchMode(Employee.PROP_DEPTID,FetchMode.JOIN);
        detachedCriteria.add(Restrictions.eq(Employee.PROP_DEPTID+".id",deptid));
        List result=commonDAO.findByCriteria(detachedCriteria);
        return result;
        
    }
}


四:dwr.xml配置
<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN" "http://www.getahead.ltd.uk/dwr/dwr10.dtd"> 

<dwr>
 <allow>
  <create creator="spring" javascript="ajaxTestService">
   <param name="beanName" value="ajaxTestService" />
   <include method="getEmployeeById" />
  </create>
        <convert converter="bean" match="com.lion.cms.domain.pojo.Employee"> 
            <param name="exclude" value="com.lion.cms.domain.pojo.Dept"/> 
        </convert>
 </allow>
</dwr>


五:jsp页面
<html:select styleId="deptid" property="deptid" onchange="loadEmployList(this.value)">
                <html:optionsCollection name="deptlist" label="deptname" value="id"/>
            </html:select>
            <html:select property="employee" styleId="employee">
            <html:option value="" >请选择</html:option>
            </html:select>     

<script type="text/javascript">
<!--
    // 加载employee下拉列表。
    function loadEmployList(deptid){
        if(deptid==null||deptid==''){
        clearEmployeeSel();
        return;
        }
        ajaxTestService.getEmployeeById(loadEmployListCallback,deptid);
    }
    var loadEmployListCallback=function(items){
        clearEmployeeSel();
        DWRUtil.addOptions("employee",items,"id","truename");
    }
    function clearEmployeeSel(){
        DWRUtil.removeAllOptions("employee");
        DWRUtil.addOptions("employee",[{label:'请选择',value:''}],"value","label");
    }
//-->
</script>



职工和部门是多对一关系,例子简单,不详细叙述了

你可能感兴趣的:(xml,Web,jsp,struts,DWR)