梦相随1006

版权归 梦相随1006 所有,未经 https://www.cnblogs.com/xin1006 作者许可,严禁转载

导航

XML预览

 

功能:  实现模板文件的预览

 模板实体类中有一个content字段,它的值是xml形式的,就是要预览它的内容; 

 

实现思路:  在java后台中将这个xml内容以xml文件的形式保存到服务器上,然后将路径返回到前台,前台使用window.open()的方式访问这个文件,这个文件就会在浏览器中打开,浏览器打开xml文件就是一种预览的方式.

 

    /**
     * 预览模板-并生成模板文件
     * 
     * @param parameter
     * @return
     * @throws IOException 
     */
    @SuppressWarnings("deprecation")
    public Object getFileContent4XML(Map<String, DataSet> dataSetMap,Object parameter){
        
        Map<String, Object> outParameters= new HashMap<String,Object>();
        outParameters.put("suc", "true");
        try {
            DataSet dataSet=dataSetMap.get("datasetTemplate");
            // 这里只会有一条数据
            if(dataSet.getRecords()!=null && dataSet.getRecords().size()==1){
// 拿到对象,它里面有content字段 MantisTemplate templete
=(MantisTemplate)(dataSet.getRecords().iterator().next()); // request对象 HttpServletRequest request=((HttpDoradoContext) DoradoContext.getContext()).getRequest() ; String path=request.getRealPath("tmp/"); // templete对象的xml内容包含一些特殊的字符无法解析,需要作转义 ,按照目前的内容,只能对& 作转义 String temp=templete.getTemplateContent(); temp=temp.replaceAll("&", "&amp;"); //生成文件并将xml内容写入文件 File file =new File(path+File.separatorChar+templete.getName()+".xml"); OutputStream out= new FileOutputStream(file); out.write(temp.getBytes()); out.flush(); out.close();
// 返回xml路径 retPath=http://localhost:8080/receipt
String retPath
=request.getRequestURL().toString(); retPath=retPath.replaceFirst(request.getServletPath().toString(), ""); outParameters.put("retPath",retPath); outParameters.put("retFile","/tmp/"+templete.getName()+".xml" ); }else{ outParameters.put("suc", "false"); } } catch (Exception e) { outParameters.put("suc", "false"); logger.error(e, "【异常】 [模板文件导入失败!] " + " 异常信息:" + e.getMessage()); } return outParameters; }

 前台的处理

            var suc = command.outParameters().getValue("suc");
                    if(suc == 'false'){
                        alert("预览模板失败!");
                        return false;
                    }else{
                        var  retPath= command.outParameters().getValue("retPath");
                        var  retFile= command.outParameters().getValue("retFile");
                        // 在当前页面中打开时使用 location.href=retPath+encodeURIComponent(retFile);
                   // 在新的窗口中打开时使用 open()方法
                        open(retPath+encodeURIComponent(retFile),'_blank','top=210,left=220,height=455,width=1120,directories=no,titlebar=no,location=no,toolbar=no,menubar=no,scrollbars=yes,resizable=no');
                    }

 

JavaScript打开新窗口的更多详细的说明,请访问:

http://www.cnblogs.com/yangw/p/4752473.html

 

posted on 2015-08-23 17:04  梦相随1006  阅读(1067)  评论(0编辑  收藏  举报