dwr-Ajax开源框架

前端之家收集整理的这篇文章主要介绍了dwr-Ajax开源框架前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


dwr全称Direct Web Remoting原理将java代码转化成js对象,实现跨域访问

web.xml

<?xmlversion="1.0"encoding="UTF-8"?>
<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID"version="2.5">

<display-name>DWR(DirectWebRemoting)</display-name>
<description>ASimpleDemoDWR</description>

<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>

<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>
</web-app>

dwr.xml

<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEdwrPUBLIC"-//GetAheadLimited//DTDDirectWebRemoting3.0//EN""http://getahead.org/dwr/dwr30.dtd">

<dwr>

<allow>

<!--
<filterclass="com.example.dwr.monitor.MonitoringAjaxFilter"/>
<filterclass="org.directwebremoting.filter.ExtraLatencyAjaxFilter">
<paramname="delay"value="200"/>
</filter>
-->

<!--intro-forthetestonindex.html-->
<createcreator="new">
<paramname="class"value="org.konghao.dwr.model.MyDwr"/>
</create>
<convertconverter="bean"match="org.konghao.dwr.model.User"/>
<convertconverter="bean"match="org.konghao.dwr.model.Group"/>

<convertmatch="java.lang.Exception"converter="exception"/>
<convertconverter="bean"match="java.lang.StackTraceElement"/>


</allow>

</dwr>


packageorg.konghao.dwr.model;

importjava.io.File;
importjava.io.IOException;
importjava.io.InputStream;
importjava.util.ArrayList;
importjava.util.List;

importjavax.servlet.http.HttpServletRequest;

importorg.apache.commons.io.FileUtils;
importorg.apache.commons.io.FilenameUtils;
importorg.directwebremoting.WebContext;
importorg.directwebremoting.WebContextFactory;

publicclassMyDwr{
publicStringhello(Stringworld){
System.out.println("hello"+world);
return"hello"+world;
}

publicUserload(){
Useru=newUser(1,"张三",newGroup(1,"财务处"));
returnu;
}

publicList<User>list(){
List<User>users=newArrayList<User>();
users.add(newUser(1,"财务处")));
users.add(newUser(2,"李四",newGroup(2,"科技处")));
users.add(newUser(3,"王五",newGroup(3,"宣传部")));
returnusers;
}

publicvoidadd(Useruser){
System.out.println(user);
}

publicvoiddeleteUser(){
thrownewMyException("在删除用户的时候有错");
}

publicintadd(inta,intb){
returna+b;
}

publicStringupload(InputStreamis,Stringfilename)throwsIOException{
//WebContext可以获取HttpServlet的对象
WebContextwc=WebContextFactory.get();
HttpServletRequestreq=wc.getHttpServletRequest();
Stringrealpath=req.getSession().getServletContext().getRealPath("upload");
Stringfn=FilenameUtils.getName(filename);
Stringfilepath=realpath+"/"+fn;
FileUtils.copyInputStreamToFile(is,newFile(filepath));
returnfilepath;
}
}



packageorg.konghao.dwr.model;

publicclassMyExceptionextendsRuntimeException{

publicMyException(){
super();
//TODOAuto-generatedconstructorstub
}

publicMyException(Stringmessage,Throwablecause){
super(message,cause);
//TODOAuto-generatedconstructorstub
}

publicMyException(Stringmessage){
super(message);
//TODOAuto-generatedconstructorstub
}

publicMyException(Throwablecause){
super(cause);
//TODOAuto-generatedconstructorstub
}

}



packageorg.konghao.dwr.model;

publicclassUser{
privateintid;
privateStringusername;
privateGroupgroup;
publicintgetId(){
returnid;
}
publicvoidsetId(intid){
this.id=id;
}
publicStringgetUsername(){
returnusername;
}
publicvoidsetUsername(Stringusername){
this.username=username;
}
publicGroupgetGroup(){
returngroup;
}
publicvoidsetGroup(Groupgroup){
this.group=group;
}
publicUser(intid,Stringusername,Groupgroup){
super();
this.id=id;
this.username=username;
this.group=group;
}
publicUser(){
super();
}
@Override
publicStringtoString(){
return"User[id="+id+",username="+username+",group="+group
+"]";
}
}


packageorg.konghao.dwr.model;

publicclassGroup{
privateintid;
privateStringname;


publicintgetId(){
returnid;
}
publicvoidsetId(intid){
this.id=id;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicGroup(intid,Stringname){
super();
this.id=id;
this.name=name;
}
publicGroup(){
super();
}
@Override
publicStringtoString(){
return"Group[id="+id+",name="+name+"]";
}


}



无刷新上传文件

<%@pagelanguage="java"contentType="text/html;charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<Metahttp-equiv="Content-Type"content="text/html;charset=UTF-8">
<title>Inserttitlehere</title>
<scripttype="text/javascript"src=\'#\'" /dwr/engine.js"></script>
<scripttype="text/javascript"src=\'#\'" /dwr/util.js"></script>
<scripttype="text/javascript"src=\'#\'" /dwr/interface/MyDwr.js"></script>
<scripttype="text/javascript">
functionupload(){
varfile=dwr.util.getValue("myfile");
alert(file.value);
alert(file);
MyDwr.upload(file,file.value,function(data){
alert(data);
});
}
</script>
</head>
<body>
<inputtype="file"id="myfile"/>
<inputtype="button"value="上传文件"onclick="upload()"/>
</body>
</html>
原文链接:https://www.f2er.com/ajax/162363.html

猜你在找的Ajax相关文章