Ajax框架之DWR学习(异常处理案例、Bean传递参数、多个Service) |
您所在的位置:网站首页 › mapper传入多个参数 › Ajax框架之DWR学习(异常处理案例、Bean传递参数、多个Service) |
Ajax框架之DWR学习(异常处理案例、Bean传递参数、多个Service)-yellowcong
原创
wx59c5bf45605b9 2023-05-11 09:56:49 ©著作权 文章标签 ajax dwr xml java javascript 文章分类 Python 后端开发 ©著作权归作者所有:来自51CTO博客作者wx59c5bf45605b9的原创作品,请联系作者获取转载授权,否则将追究法律责任1、在DWR中处理异常的方式,2、多个Dwr的service调用,3、通过Bean来传递参数 配置多个dwr的Service,同时添加Service的错误处理 Service用户的服务 package com.yellowcong.service; import com.yellowcong.entity.User; public class UserService { public String sayHello(String name){ System.out.println(name); /*Browser.withPage("", new Runnable() { public void run() { // TODO Auto-generated method stub } });*/ return "你好"+name; } public String addUser(User user){ System.out.println(user.getAge()); System.out.println(user.getUsername()); String username = user.getUsername(); if(username == null || "".equals(username == null)){ throw new RuntimeException("用户名不为空"); } return "添加成功"; } }下一个是文件上传的service package com.yellowcong.service; import java.io.File; import java.io.IOException; import java.io.InputStream; import org.apache.commons.io.FileUtils; import org.directwebremoting.WebContext; import org.directwebremoting.WebContextFactory; public class UploadService { /** * 这个导入的是InputStream 的方法,而不是通过 File * * @param in * 输入流 * @param filename * 文件名称 * @return * @throws IOException */ public String upload(InputStream in, String filename) throws IOException { // 对于 服务器,我们需要通过ServletContext 获取我们的绝对路径 ,存储在可操作的文件夹中 //这个对象是DWR给我们封装的 WebContext context = WebContextFactory.get(); //获取文件路径 String path =context.getServletContext().getRealPath("/resources/"); //新建文件 File outFile = new File(path+File.separator+filename); System.out.println(outFile.getAbsolutePath()); //当文件夹不存在的情况新建 if(!outFile.getParentFile().exists()){ outFile.getParentFile().mkdirs(); } //写文件 FileUtils.copyInputStreamToFile(in, outFile); return outFile.getAbsolutePath(); } }Entitypackage com.yellowcong.entity; public class User { private String username; private Integer age; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }界面index.jsp function addUser(age,username){ var user ={age:age,username:username}; UserService.addUser(user,{ callback:function(data){ alert("处理结果\t"+data); }, errorHandler:function(message,error){ //相当于一个Map 中获取key 和 value for(var key in error){ alert(key+"__"+error[key]); } } }); } function uploadFile(){ var file = document.getElementById("file_upload"); var names = file.value.split("\\"); var fileName = names[names.length-1]; UploadService.upload(file,fileName,function(data){ alert("文件路径\t"+data); }); } 异常处理 文件上传 文件上传效果赞 收藏 评论 分享 举报 上一篇:Ajax框架之DWR学习(简单入门案例)-yellowcong 下一篇:SpringCloud之高可用的分布式配置中心(Spring Cloud Config) |第十二章 -yellowcong |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |