DWR 2.0.10之简单测试

前端之家收集整理的这篇文章主要介绍了DWR 2.0.10之简单测试前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1.官网下载 http://directwebremoting.org/dwr/downloads/index.html#stableReleasedwr.jar的java包

http://commons.apache.org/proper/commons-logging/下载 Commons Logging.,因为dwr依赖日志jar包

并将dwr.jar和commons-logging-1.1.3.jar添加到web工程的lib下面

2.在web.xml中添加如下配置代码,如下

<servlet>
  <display-name>DWR Servlet</display-name>
  <servlet-name>dwr-invoker</servlet-name>  
  <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
  <init-param>
      <param-name>crossDomainSessionSecurity</param-name>
     <param-value>false</param-value>
  </init-param>
</servlet>

<servlet-mapping>
  <servlet-name>dwr-invoker</servlet-name>
  <url-pattern>/dwr/*</url-pattern>
</servlet-mapping>


在init-param中有一个属性为crossDomainSessionSecurity,默认为true,设为false的原因在于dwr采用的request使用了秘密的请求,为了防止csrf攻击,详情参看官网描述

http://directwebremoting.org/dwr/security/script-tag-protection.html#scriptTagHack

3.在WEB-INF下面新建dwr的配置文件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>
    <create creator="new" javascript="DWRTestJava">
      <param name="class" value="com.undergrowth.dwrtest.DWRTest" />
    </create>
     <convert converter="bean" match="com.undergrowth.bean.Person" />
  </allow>
  <signatures>
  	<![CDATA[
  		import java.util.List;
  		import java.util.Map;
  	]]>
  </signatures>
</dwr>


在这里稍微注意一下 在官网上头部为

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

这里改成如上 2.0 20因为 我们使用的dwr版本为2.0.10 不然会造成版本不匹配的问题

对于dwr.xml的配置文件里面的涵义分别如下:

create标签主要负责用于声明公开给javascript调用的类和方法

如果在create标签里面没有指明公开的方法的话 默认公开所有方法

convert标签指明了参数传递和返回值时,对应的javabean应该如何映射,对于java基本的数据类型还有数组、列表的话 dwr会自动在javascript和java类型之间转换(意思就是,如果在参数传递和返回值的时候没有javabean的话,可以不用convert标签)

4.编写java端代码,如下

DWRTest.java

package com.undergrowth.dwrtest;

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

import com.undergrowth.bean.Person;

public class DWRTest {
	
	public DWRTest(){}
	
	//传入参数 返回值
	public String dwrReturnString(String name)
	{
		return "你的姓名为:"+name;
	}
	
	//传入参数 返回列表
	public List<Person> dwrReturnList(String name)
	{
		Person person=new Person(name,new Date(),20);
		List<Person>  list=new ArrayList<>();
		list.add(person);
		return list;
	}
	
	//传入参数 返回列表
		public Map<String,Person> dwrReturnMap(String name)
		{
			Person person=new Person(name,22);
			Map<String,Person> map=new HashMap<>();
			map.put("1",person); 
			return map;
		}
   //传入JavaBean 返回JavaBean
		public Person dwrReturnBean(Person p)
		{
			Person person=new Person(p.getName(),p.getAge());
			return person;
		}
}


Person.java

package com.undergrowth.bean;

import java.util.Date;

public class Person {
	private String name;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	private Date birthday;
	private int age;
	public Person(String name,Date birthday,int age) {
		super();
		this.name = name;
		this.birthday = birthday;
		this.age = age;
	}
	public Person() {
		super();
	}
	
}


5.测试首页 index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
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 'index.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">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<script type="text/javascript" src="<%=basePath%>/dwr/interface/DWRTestJava.js"></script>
	<script type="text/javascript" src="<%=basePath%>/dwr/engine.js" ></script>
	<script type="text/javascript" src="<%=basePath%>/dwr/util.js" ></script>
	<script type="text/javascript">
	
	window.onload=init;
	function init()
	{
		document.getElementById("startId").innerHTML="开始dwr调用,路径为:<%=basePath%>";
		//调用返回字符串函数
		DWRTestJava.dwrReturnString("张三",callReturnFunc);
		//调用返回列表函数
		DWRTestJava.dwrReturnList("张三",callReturnFuncList);
		//调用返回map函数
		DWRTestJava.dwrReturnMap("张三",callReturnFuncMap);
		
		//传入javabean进行处理 再返回javabean
		var infoVo={
			name:"张三",age:100
		};
		DWRTestJava.dwrReturnBean(infoVo,callReturnFuncBean);
	}
	
	
		
		function callReturnFunc(data)
		{
			//document.getElementById("startId").innerHTML="开始dwr字符串调用调用";
			document.getElementById("contentId").innerHTML=data;
		}
	
	
		
		function callReturnFuncList(list)
		{
		var data="list总共有:"+list.length+"个元素,";
			for(var i=0;i<list.length;i++)
			{
			    var person=list[i];
				data+=person.name+" "+person.birthday+" "+person.age+"\n";
			}
		  document.getElementById("contentListId").innerHTML=data;	
		}
		
		function callReturnFuncMap(map)
		{
		var count=0;
		var data="";
			for(var i in map)
			{
			    var person=map[i];
				data+=person.name+" "+person.birthday+" "+person.age+"\n";
				count++;
			}
		  document.getElementById("contentMapId").innerHTML="map总共有:"+count+"个元素,"+data;	
		}
		
		function callReturnFuncBean(data)
		{
			document.getElementById("contentVoId").innerHTML=data.name+" "+data.birthday+" "+data.age;	
		}
		
	</script>
  </head>
  
  <body>
   <span id="startId"></span><br/>
    <span id="contentId"></span><br/>
    <span id="contentListId"></span><br/>
    <span id="contentMapId"></span><br/>
    <span id="contentVoId"></span><br/>
  </body>
</html>


6.结果

http://localhost:8080/DwrTest1/index.jsp

如下

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

猜你在找的Ajax相关文章