DWR Demo

Dwr.jar download under the lib on
Medium in web.xml add the following code:

<servlet>
		<servlet-name>dwr-invoker</servlet-name>
		<display-name>DWR Servlet</display-name>
		<servlet-class>org.directwebremoting.servlet.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>

Category deal with the preparation of background: DwrTest.java and the introduction of category DwrTestForm.java


package com.test;

import java.util.List;

import com.test.vo.DwrTestForm;

public class DwrTest {

	public DwrTestForm sayHello(List list) {
	    DwrTestForm dwrTestForm = new DwrTestForm();
	    dwrTestForm.setName(" Name  :"+(String)list.get(0));
	    dwrTestForm.setAge(" Age  :"+(String)list.get(1));
	    dwrTestForm.setSex(" Gender  :"+(String)list.get(2));
	    dwrTestForm.setAddress(" Address  :"+(String)list.get(3));
	    dwrTestForm.setPhone(" Phone  :"+(String)list.get(4));
        return dwrTestForm;
	}

}
package com.test.vo;

public class DwrTestForm
{
    String name;
    String age;
    String sex;
    String address;
    String phone;
    public String getName()
    {
        return name;
    }
    public void setName(String name)
    {
        this.name = name;
    }
    public String getAge()
    {
        return age;
    }
    public void setAge(String age)
    {
        this.age = age;
    }
    public String getSex()
    {
        return sex;
    }
    public void setSex(String sex)
    {
        this.sex = sex;
    }
    public String getAddress()
    {
        return address;
    }
    public void setAddress(String address)
    {
        this.address = address;
    }
    public String getPhone()
    {
        return phone;
    }
    public void setPhone(String phone)
    {
        this.phone = phone;
    }

}

Add dwr.xml with the preparation of a web.xml directory:

<!DOCTYPE dwr PUBLIC
    "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"
    "http://getahead.org/dwr/dwr20.dtd">

<dwr>
  <allow>

    <create creator="new" javascript="Demo">
      <param name="class" value="com.test.DwrTest"/>
      <include   method="sayHello" />
    </create>

    <convert converter="bean" match="com.test.vo.DwrTestForm">
			<param name="include" value="name,age,sex,address,phone"/>

	</convert>
  </allow>
</dwr>

Prepare jsp page

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
String contextPath = request.getContextPath();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript"
    src="../dwr/interface/Demo.js"> </script>
<script type="text/javascript"
    src="../dwr/engine.js"> </script>
    <script type="text/javascript"
    src="../dwr/util.js"> </script>
</head>
<body>
<script type="text/javascript">

    function send()
    {
        var list = new Array();
        list[0] = document.getElementById("text1").value;
        list[1] = document.getElementById("text2").value;
        list[2] = document.getElementById("text3").value;
        list[3] = document.getElementById("text4").value;
        list[4] = document.getElementById("text5").value;
        Demo.sayHello(list, callBack);
    }

    function callBack(backValue)
    {
        document.getElementById("result").innerHTML = backValue.name + " "
                + backValue.age + " " + backValue.sex + " " + backValue.address
                + " " + backValue.phone;
    }
</script>
<p>

  Name:<div><input type="text"/></div>
  Age:<div><input type="text"/></div>
  Sex:<div><input type="text"/></div>
  Address:<div><input type="text"/></div>
  Phone:<div><input type="text"/></div>
  <input  value="Send" type="button"/>
  <br/>
</p>
<div></div>
</body>
</html>

This can be a java call js

If the start times
java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory abnormal
Will have to add in the lib in the package commons-loggin.jar

分类:AJAX 时间:2009-03-10 人气:1292
分享到:
blog comments powered by Disqus

相关文章

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.658 (s). 14 q(s)