AppFuse的SiteMesh

AppFuse中使用SiteMesh,怎样设置SiteMesh才能够使某些页面不被它装饰?这个文章讲述了怎样实现这个功能!
因为AppFuse中使用的默认的JSP模板为SiteMesh,默认情况下,所有的请求都会被SiteMesh所装饰,所以我要实现点击文件链接,直接下载文件时却得不到正确格式的文件,得到的是一个html文件,即使制定了下载文件的类型:如:application/msexcel或者其他的类型都不行,我分析是因为,我首先向输出流中写了些东西,后来被SiteMesh使用getWriter方法给冲掉了,所以得到的是默认的网页文件,我折腾了2天,痛不欲生,刚刚解决掉这个问题,解决方法如下:

1.将下载文件的jsp放到一个特殊的目录下,我的放在download目录,这样可以区分需要被sitemesh装饰和不需要被它装饰的文件。

2.配置sitemesh的decorators.xml:
<decorators defaultdir="/decorators">
    <excludes>
        <pattern>/download/*</pattern>
    </excludes>
</decorators>

这个配置指明download下的文件不需要被sitemesh装饰。



我的下载文件的页面代码是试验代码,稍加修改就可以正式使用,代码如下:
<%@ page import= "java.io.*,java.net.*" %><%
    try{
        //取得虚拟的路径
        String fn = "attachment; filename=a.xls";  //必须改为UniCode编码的字符串
        System.out.println(fn);
        //把标题、内容写到输出流中
        response.setHeader("Content-Disposition", new String(fn.getBytes("GB2312"),
                                        "ISO-8859-1"));
        createOutput( response.getOutputStream(),"c:\\ltf.xls");
    }catch( Exception ee ){
        ee.printStackTrace();
    }
%><%!
public void createOutput( OutputStream out,String realpath ) throws IOException {
    int b;
    BufferedInputStream  m_input =
                new BufferedInputStream( new  FileInputStream(realpath) );
    while( (b = m_input.read()) != -1 ){
        out.write(b);
    }
    m_input.close();
    out.flush();
    out.close();
}
%>
<%--
  // 得到文件名字和路径
  String filename = request.getParameter("zipfilename");
  //String jsppath = "";             getServletConfig().getServletContext().getRealPath("") +
   String filepath = "c:\\ltf.xls";        //jsppath.substring(0,jsppath.lastIndexOf("")) + "reports\\";
  System.out.println("---------------=================" + filepath);
  java.io.FileInputStream fileInputStream = null;
  //out.println(filepath);
  // 设置响应头和下载保存的文件名
  response.setHeader("Content-Disposition",
  "attachment; filename=" + "a.xls");
  response.setContentType("application/msexcel");
 
  // 打开指定文件的流信息
  try{
  fileInputStream =
   new java.io.FileInputStream(filepath);
  
  // 写出流信息
  int i;
  while ((i=fileInputStream.read()) != -1) {
   out.write(i);
  }
  fileInputStream.close();
  out.close();
  System.out.println("---OK---------------");
  
  }catch(Exception ee){
    ee.printStackTrace();
  }
--%>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值