初次使用spring+dwr的记录

1,在jsp引入以下js,其中custService是自己命的名
<script type='text/javascript' src='<%=request.getContextPath() %>/dwr/engine.js'> </script>
    <script type='text/javascript' src='<%=request.getContextPath() %>/dwr/util.js'> </script>
    <script type='text/javascript' src='<%=request.getContextPath() %>/dwr/interface/custService.js'> </script>
   
2,在web.xml配置
<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>activeReverseAjaxEnabled</param-name>
      <param-value>true</param-value>
    </init-param>
    <init-param>
      <param-name>initApplicationScopeCreatorsAtStartup</param-name>
      <param-value>true</param-value>
    </init-param>
    <init-param>
      <param-name>maxWaitAfterWrite</param-name>
      <param-value>100</param-value>
    </init-param>
    <!--
    <init-param>
      <param-name>org.directwebremoting.extend.ServerLoadMonitor</param-name>
      <param-value>org.directwebremoting.impl.PollingServerLoadMonitor</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>
 
 3,dwr.xml,放在web-info下,其中 custService为spring中的bean名
 <?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>
  <create creator="spring" javascript="custService">
   <param name="beanName" value="custService" />
  </create>
 </allow>
</dwr>

4,
function refreshSelect(){
var type =$("seltype").value;
if('dept'==type){
getDeptList();
}
else if('xm'==type){
getXmList();
}
}

function getDeptList() {
  custService.getDeptList(function(data) {
  var deptlist =document.getElementById("selectlist");
dwr.util.removeAllOptions("selectlist");
  dwr.util.addOptions("selectlist",data);
  });
}

function getXmList() {
  custService.getXmList(function(data) {
  var deptlist =document.getElementById("selectlist");
dwr.util.removeAllOptions("selectlist"); //dwrutil的特有方法
  dwr.util.addOptions("selectlist",data); //
  });
}

5,spring中service的方法:
 public Map getDeptList() {
    Map map = new TreeMap();//排序,不用Hashmap
   map.put("", "选择一个部门");
  List l = this.typeService
    .getList("from Dept a where a.enabled = '0' order by a.ordernum");
  for (Iterator iterator = l.iterator(); iterator.hasNext();) {
   Dept d = (Dept) iterator.next();
   map.put(d.getDeptid(), d.getDeptname());
  }
  return map;
 }
 
6,
<select name="" id="seltype" onchange="refreshSelect();">
            <option value="">
             --选择方式--
            </option>
            <option value="xm">
             按项目
            </option>
            <option value="dept">
             按部门
            </option>
           </select>
7,返回为布尔类型的处理
<script  type="text/javascript">
    function showResult(b) {//b为布尔值
        if (b) {
            alert("已经有重复的登录名了!");
        } else {
            alert("登录名尚未注册过!");
        }
    }function checkRepeat(s) {
        var o = document.getElementById(s);
        if (o) {
            UserManager.getUser(o.value, showResult);//js不允许重载
        }
    }
 
</script>

<input type="text" name="user" id="user">
<input type="button" onclick="checkRepeat('user')" value="检查是否重复">


public boolean getUser(String username)
{
    String sql="select count(*) from user where userName=? ";
 int i=getJdbcTemplate().queryForInt(sql,new Object[]{username});
    if(i>0)
    return true;
        else
     return false;
}


 

你可能感兴趣的:(初次使用spring+dwr的记录)