示例#1
0
 @Override
 public int doStartTag() throws JspException {
   StringBuilder buffer = new StringBuilder();
   buffer.append("<a ");
   if (TmStringUtils.isNotEmpty(href)) {
     buffer.append(" href=\"" + href + "\"");
   }
   if (TmStringUtils.isNotEmpty(onclick)) {
     buffer.append(" onclick=\"" + onclick + "\"");
   }
   if (TmStringUtils.isNotEmpty(id)) {
     buffer.append(" id=\"" + id + "\"");
   }
   if (TmStringUtils.isNotEmpty(className)) {
     buffer.append(" className=\"" + className + "\"");
   }
   if (TmStringUtils.isNotEmpty(title)) {
     buffer.append(" title=\"" + title + "\"");
   }
   buffer.append(">");
   buffer.append(text);
   buffer.append("</a>");
   System.out.println(buffer.toString());
   return SKIP_BODY;
 }
示例#2
0
 public void setHref(String href) {
   if (TmStringUtils.isNotEmpty(href)) {}
 }
  public static String staticContent(
      Integer id,
      String link,
      String templatePath,
      HttpServletRequest request,
      HttpServletResponse response) {
    // 写入的静态目标文件
    File targetFile = null;
    // 返回的相对路径
    String fileNamePath = null;
    if (TmStringUtils.isNotEmpty(link)) { // 你已经生成了静态页面,就用原来生成静态页面文件进行覆盖操作
      String rootPath = request.getRealPath("/"); // 获取根目录
      File rootFile = new File(rootPath);
      targetFile = new File(rootFile, link); // 目标文件
      File parentPath = targetFile.getParentFile(); // 防止父级目录丢失,而造成静态化失败,判断是否父亲文件夹存在
      if (!parentPath.exists()) parentPath.mkdirs(); // 如果不存在,就创建父级目录
      fileNamePath = link; // 返回相对路径
    } else {
      // 如果没有静态页面
      // 长生一个新的目录
      String filePath = "pages/" + TmStringUtils.formatDate(new Date(), "yyyy/MM/dd");
      // 获取服务器根目录
      String rootPath = request.getRealPath(filePath);
      File rootFile = new File(rootPath);
      // 如果不存在,就创建
      if (!rootFile.exists()) rootFile.mkdirs();
      // 长生新的静态文件名称
      String name = getRandomString(20) + ".html";
      targetFile = new File(rootFile, name);
      // 返回的名称
      fileNamePath = filePath + "/" + name;
    }

    FileOutputStream fos = null;
    ServletContext sc = request.getServletContext();
    // 组装返回的路径
    try {
      // 设定你的模板路径,内容模板三个,显示风格都不一样,
      RequestDispatcher rd = sc.getRequestDispatcher(templatePath);
      final ByteArrayOutputStream os = new ByteArrayOutputStream();
      final ServletOutputStream stream = new ServletOutputStream() { // 匿名内部类,回调函数
            public void write(byte[] data, int offset, int length) {
              os.write(data, offset, length);
            }

            public void write(int b) throws IOException {
              os.write(b);
            }
            //				@Override
            //				public boolean isReady() {
            //					return false;
            //				}
            //				@Override
            //				public void setWriteListener(WriteListener arg0) {
            //
            //				}
          };
      final PrintWriter pw = new PrintWriter(new OutputStreamWriter(os, "utf-8"));
      HttpServletResponse rep =
          new HttpServletResponseWrapper(response) {
            public ServletOutputStream getOutputStream() {
              return stream;
            }

            public PrintWriter getWriter() {
              return pw;
            }
          };

      // 将id放入到作用域中
      request.setAttribute("id", id);
      request.setAttribute("username", "keke");
      request.setAttribute("age", 30);

      rd.include(request, rep); // 发起请求
      pw.flush();
      fos = new FileOutputStream(targetFile);
      os.writeTo(fos); // 讲源代码写入磁盘文件中
      return fileNamePath;
    } catch (FileNotFoundException e) {
      e.printStackTrace();
      return null;
    } catch (ServletException e) {
      e.printStackTrace();
      return null;
    } catch (IOException e) {
      e.printStackTrace();
      return null;
    } finally {
      try {
        fos.close();
      } catch (Exception e) {
      }
    }
  }