示例#1
0
 public static void main(String[] args) throws IOException {
   StringTemplateResourceLoader strl = new StringTemplateResourceLoader();
   Configuration cfg = Configuration.defaultConfiguration();
   String inputStr = "Hello ${myName}";
   GroupTemplate gt = new GroupTemplate(strl, cfg);
   Template t = gt.getTemplate(inputStr);
   t.binding("myName", "Lost World vsked is here");
   String outStr = t.render();
   System.out.println(outStr);
 }
示例#2
0
  public void sendSmtpTestMail(
      String smtpFromMail,
      String smtpHost,
      Integer smtpPort,
      String smtpUsername,
      String smtpPassword,
      String toMail) {
    SystemConfig systemConfig = SystemConfigUtil.getSystemConfig();
    MailConfig mailConfig = TemplateConfigUtil.getMailConfig(MailConfig.SMTP_TEST);
    String subject = mailConfig.getSubject();
    String templateFilePath = mailConfig.getTemplateFilePath();
    try {
      email = new HtmlEmail();
      email.setHostName(systemConfig.getSmtpHost());
      email.setSmtpPort(systemConfig.getSmtpPort());
      email.setAuthenticator(
          new DefaultAuthenticator(systemConfig.getSmtpUsername(), systemConfig.getSmtpPassword()));
      email.setSSLOnConnect(true);

      WebAppResourceLoader resourceLoader = new WebAppResourceLoader();
      Configuration cfg = Configuration.defaultConfiguration();
      GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
      Template template = gt.getTemplate(templateFilePath);
      template.binding("systemConfig", systemConfig);
      String text = template.render();

      email.setFrom(
          MimeUtility.encodeWord(systemConfig.getShopName())
              + " <"
              + systemConfig.getSmtpFromMail()
              + ">");
      email.setSubject(subject);
      email.setMsg(text);
      email.addTo(toMail);
      email.send();

    } catch (EmailException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
示例#3
0
  public boolean sendMail(
      String subject, String templateFilePath, Map<String, Object> data, String toMail) {
    boolean isSend = false;
    try {
      SystemConfig systemConfig = SystemConfigUtil.getSystemConfig();

      email = new HtmlEmail();
      email.setHostName(systemConfig.getSmtpHost());
      email.setSmtpPort(systemConfig.getSmtpPort());
      email.setAuthenticator(
          new DefaultAuthenticator(systemConfig.getSmtpUsername(), systemConfig.getSmtpPassword()));
      email.setSSLOnConnect(true);

      WebAppResourceLoader resourceLoader = new WebAppResourceLoader();
      Configuration cfg = Configuration.defaultConfiguration();
      GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
      Template template = gt.getTemplate(templateFilePath);
      template.binding(data);
      String text = template.render();

      email.setFrom(
          MimeUtility.encodeWord(systemConfig.getShopName())
              + " <"
              + systemConfig.getSmtpFromMail()
              + ">");
      email.setSubject(subject);
      email.setMsg(text);
      email.addTo(toMail);
      email.send();
      isSend = true;
    } catch (EmailException e) {
      e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return isSend;
  }
示例#4
0
  /** 配置常量 */
  public void configConstant(Constants me) {
    log.info("configConstant 缓存 properties");
    new PropertiesPlugin(loadPropertyFile("init.properties")).start();

    log.info("configConstant 设置字符集");
    me.setEncoding(ToolString.encoding);

    log.info("configConstant 设置是否开发模式");
    me.setDevMode(getPropertyToBoolean(DictKeys.config_devMode, false));
    // me.setViewType(ViewType.JSP);//设置视图类型为Jsp,否则默认为FreeMarker

    log.info("configConstant 视图Beetl设置");
    me.setMainRenderFactory(new MyBeetlRenderFactory());

    // 修正Weblogic 11g下beetl web路径获取不正确的bug,Configuration.defaultConfiguration() 会抛出IO异常
    //		try {
    //			MyBeetlRenderFactory.groupTemplate.setConf(Configuration.defaultConfiguration());
    //			MyBeetlRenderFactory.groupTemplate.setResourceLoader(new
    // WebAppResourceLoader(PathKit.getWebRootPath()+"/WEB-INF/view/"));
    //		} catch (IOException e) {
    //			e.printStackTrace();
    //		}
    GroupTemplate groupTemplate = MyBeetlRenderFactory.groupTemplate;

    groupTemplate.registerFunction("hasPrivilegeUrl", new HasPrivilegeUrl());
    groupTemplate.registerFunction("orderBy", new OrderBy());
    groupTemplate.registerFunction("escapeXml", new EscapeXml());
    groupTemplate.registerFunction("i18nFormat", new I18nFormat());

    groupTemplate.registerTag("dict", DictTag.class);
    groupTemplate.registerTag("param", ParamTag.class);

    groupTemplate.registerFormat("dateFormat", new DateFormat());

    log.info("configConstant 视图error page设置");
    me.setError401View("/common/401.html");
    me.setError403View("/common/403.html");
    me.setError404View("/common/404.html");
    me.setError500View("/common/500.html");
  }