1、 dwr:easy ajax for java
这个工具能够实现ajax的功能,可以实现javascript直接操作java
(1)引入commons-io、commons-logging、servlet-api、以及dwr这几个包
(2)web.xml中引入dwr的监听器,可以从dwr下载下来的包中的例子中获取,并写一个java的类
①web.xml修改
<listener>
<listener-class>org.directwebremoting.servlet.DwrListener</listener-class>
</listener>
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<!-- This should NEVER be present inlive -->
<init-param>
<param-name>debug</param-name>
<param-value>true</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>
②创建一个java类
package org.konghao.service;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
public classHelloDwr {
public String say(String name){
System.out.println("name:"+name);
return "hello:"+name;
}
public Stringupload(InputStream is,String filename) throws IOException {
//可以获取相应的ServletApi
WebContextwc = WebContextFactory.get();
HttpServletRequestreq = wc.getHttpServletRequest();
StringrealPath = req.getSession().getServletContext().getRealPath("/img");
Stringfn = FilenameUtils.getName(filename);
FileUtils.copyInputStreamToFile(is,newFile(realPath+"/"+fn));
return fn;
}
}
(3)创建一个dwr.xml文件,和web.xml放在同样的位置,其中<create>表示将一个java的对象公布为一个javascript的对象,param中的value设置要公布的java类,只要写了create之后,这个类就会自动创建一个HelloDwr.js的文件,引入了javascript="hello"后可以使用hello.js
<?xml version="1.0"encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAheadLimited//DTD Direct Web Remoting 3.0//EN" "http://getahead.org/dwr/dwr30.dtd">
<dwr>
<allow>
<!-- 此时hello就是这个HelloDwr的对象,在页面中就需要通过hello.js来引入 -->
<create creator="new" javascript="hello">
<param name="class" value="org.konghao.service.HelloDwr"/>
</create>
<create creator="new" javascript="UserService">
<param name="class" value="org.konghao.service.UserService"/>
<include method="list"/>
</create>
<convert converter="bean" match="org.konghao.service.User"/>
<convert converter="bean" match="org.konghao.service.Group"/>
<convert converter="exception" match="java.lang.Exception"/>
<convert converter="bean" match="java.lang.StackTraceElement"/>
</allow>
</dwr>
(4)创建前端页面,前端页面需要引入几个必须的js文件
首先:WebContext/dwr/engine.js
其次在dwr.xml中创建的类。。。。
<%@ page language="java"contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"content="text/html; charset=UTF-8">
<title>Insert title here</title>
<!-- 必须引入dwr的engine.js,其中/dwr/这个文件夹是dwr自带的,会在web.xml中进行过滤,所以无需创建 -->
<script type="text/javascript"src="<%=request.getContextPath()%>/dwr/engine.js"></script>
<!-- 将java的类引入 -->
<script type="text/javascript"src="<%=request.getContextPath()%>/dwr/interface/hello.js"></script>
<script type="text/javascript">
hello.say("张三",function(data){
alert(data);
});
</script>
</head>
<body>
</body>
</html>