dwr与spring一起使用的方法

前端之家收集整理的这篇文章主要介绍了dwr与spring一起使用的方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

文章内容摘抄至http://www.cnblogs.com/linjiqin/archive/2011/03/28/1998125.html

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    
    <!-- Spring上下文 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:resource/app*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <!-- 配置DWR前端控制器 -->
    <servlet>
        <servlet-name>dwrServlet</servlet-name>
        <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
        <!-- 指定配置文件 -->
        <init-param>
            <param-name>config</param-name>
            <!-- 如果有多个用","分开 -->
            <param-value>
                /WEB-INF/classes/config/dwr.xml                
            </param-value>
        </init-param>
        <init-param>
            <param-name>debug</param-name>
            <param-value>true</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>dwrServlet</servlet-name>
        <url-pattern>/dwr/*</url-pattern>
    </servlet-mapping>
</web-app>

<?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配置 -->
<dwr>
    <allow>
        <!-- 建立JS对象,将目标对象的方法转换成JS对象的方法 -->
        <create javascript="helloSrv" creator="new">
            <param name="class" value="services.HelloServices"></param>
        </create>
        <!-- 从Spring中获取Java对象 -->
        <create javascript="deptSrv" creator="spring">
            <param name="beanName" value="deptServices"></param>
            <!-- 禁止执行 -->
            <exclude method="deleteDept" />
        </create>
        <create javascript="loginSrv" creator="spring">
            <param name="beanName" value="loginSrv"></param>
        </create>
        <!-- 指定针对于特定对象的转换器 -->
        <convert match="entity.*" converter="bean"></convert>
        <convert match="java.lang.Throwable" converter="bean">
            <param name="include" value="message"></param>
        </convert>
    </allow>
</dwr>

creator="spring"说明创建对象的方式是交由“spring”来管理的

<param name="beanName" value="deptServices"></param>

表示要由spring创建的类的名字根据“deptServices”唯一标识去spring的配置文件中查找,形成对应的关系

<convert match="entity.*" converter="bean"></convert>

其中convert="bean"是说明传递的对象和返回的对象的转换是使用bean的方式,这个里面的值是不变的

<?xml version="1.0" encoding="UTF-8"?>
<!--
    配置系统基础环境
 -->
<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
    >            
    <bean id="deptServices" class="services.DeptServices"></bean>
    <bean id="loginSrv" class="services.LoginService"></bean>
</beans>
package entity;

public class Dept {
    private Long id;
    private String name;

    public Dept() {

    }

    public Dept(Long id,String name) {
        this.id = id;
        this.name = name;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

package services;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import entity.Dept;

@SuppressWarnings("unchecked")
public class DeptServices {

    public List findDept() {
        throw new RuntimeException("查找失败!");
    }

    public void deleteDept(Long id) {
        System.out.println("Delete dept " + id);
    }

    public List getDeptsForPo() {
        List depts = new ArrayList();
        depts.add(new Dept(1l,"教质部"));
        depts.add(new Dept(2l,"学术部"));
        depts.add(new Dept(3l,"就业部"));
        depts.add(new Dept(4l,"咨询部"));
        return depts;
    }

    
    public void saveDept(List<Dept> depts) {
        // System.out.println(dept.getId() + ":" + dept.getName());
        System.out.println(depts);
    }

    public List getDepts() {
        List depts = new ArrayList();
        Map map = new HashMap();
        map.put("id","01");
        map.put("name","教质部");
        depts.add(map);

        map = new HashMap();
        map.put("id","02");
        map.put("name","学术部");
        depts.add(map);

        map = new HashMap();
        map.put("id","03");
        map.put("name","就业部");
        depts.add(map);

        map = new HashMap();
        map.put("id","04");
        map.put("name","咨询部");
        depts.add(map);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return depts;
    }
    
    public void test(){
    	System.out.println("tests");
    }
    
    public void testPar(String s){
    	System.out.println("testPar " + s);
    }
    
    public void testObj(Dept d){
    	System.out.println(d.getId());
    	System.out.println(d.getName());
    }
}
package services;

/**
 * @author dlwu
 *
 */
public class HelloServices {
	
    public String sayHello(String name){
        System.out.println("Hello now!");
        return "Hello " + name + "!";
    }
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'hello.jsp' starting page</title>
    
    <Meta http-equiv="pragma" content="no-cache">
    <Meta http-equiv="cache-control" content="no-cache">
    <Meta http-equiv="expires" content="0">    
    <Meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <Meta http-equiv="description" content="This is my page">

  </head>
  <!-- 记得引入js,测试地址: http://localhost:8083/dwrweb/dwr/ -->
  <script type="text/javascript" src="dwr/engine.js"></script>
  <script type="text/javascript" src="dwr/interface/deptSrv.js"></script>
  <script type="text/javascript" src="dwr/util.js"></script>
  <script type="text/javascript">
          
          function test(){
              //声明dept对象
             
              deptSrv.test();
          }
          function test1(){
              //传递普通的字符
              var temp = "test parameter String";
              deptSrv.testPar(temp);
          }
          function test2(){
              //传递对象
              var obj = {id:"1234",name:"huangbiao"};
              deptSrv.testObj(obj);
          }
          function test3(){
              //得到对象
              deptSrv.getDepts(function(data){
				for(var i = 0; i < data.length; i++ ){
					alert(data[i].id);
				}
              });
          }

          
  </script>
  <body>
      <input value="spring测试" type="button" onclick="test();"/>
      <input value="传入一个字符串" type="button" onclick="test1();"/>
      <input value="传入一个对象" type="button" onclick="test2();"/>
      <input value="接受一个对象" type="button" onclick="test3();"/>
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'hello.jsp' starting page</title>
    
    <Meta http-equiv="pragma" content="no-cache">
    <Meta http-equiv="cache-control" content="no-cache">
    <Meta http-equiv="expires" content="0">    
    <Meta http-equiv="keywords" content="keyword1,keyword3">
    <Meta http-equiv="description" content="This is my page">
  </head>
  <!-- 记得引入js,测试地址: http://localhost:8083/dwrweb/dwr/ -->
  <script type="text/javascript" src="dwr/engine.js"></script>
  <script type="text/javascript" src="dwr/interface/helloSrv.js"></script>
  <script type="text/javascript" src="dwr/util.js"></script>
  <script type="text/javascript">
          function hello(){
              //方法一
              //返回处理后的结果信息
            /*var fn = function(result){
                $("msg").innerHTML = result;
            }
            helloSrv.sayHello($("name").value,fn);*/
            
            //方法二
            helloSrv.sayHello($("name").value,function(result){
                $("msg").innerHTML=result;
            });
            
            //方法三
            //使用如下的好处为:不用导入如上三个js        
            //第一个参数: dwr访问路径,在web.xml中配置,如: <url-pattern>/dwr/*</url-pattern>        
            //第二个参数: dwr与java服务器通信变量,在dwr.xml中声明        
            //第三个参数: 服务器方法名
            //第四个参数: 页面请求参数,即服务器方法名得参数
            //第五个参数: 回调函数        
            //dwr.engine._execute("dwr",'helloSrv','sayHello',$("name").value,fn);
            
          }
  </script>
  <body>
      <div id="msg"></div>
    <input type="text" id="name" />
    <input type="button" value="Hello" onclick="hello();" />
  </body>
</html>

将请求设置为同步的方法 —— DWREngine.setAsync(true);//异步,false为同步

原文链接:https://www.f2er.com/ajax/165510.html

猜你在找的Ajax相关文章