Java Code Examples for freemarker.template.Configuration#VERSION_2_3_0

The following examples show how to use freemarker.template.Configuration#VERSION_2_3_0 . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FreeMarkerUtils.java    From liteflow with Apache License 2.0 5 votes vote down vote up
/**
 * 处理字符串
 * @param templateName
 * @param content
 * @param paramMap
 * @return
 */
public static String formatString(String templateName, String content, Object paramMap) {

    Configuration configuration = new Configuration(Configuration.VERSION_2_3_0);
    StringWriter writer = new StringWriter();
    try {
        Template template = new Template(templateName, content, configuration);
        template.process(paramMap, writer);
    } catch (Throwable e) {
        LOG.error("freemarker handler error");
        throw new RuntimeException(e);
    }
    return writer.toString();
}
 
Example 2
Source File: Resources.java    From JApiDocs with Apache License 2.0 5 votes vote down vote up
/**
 * get freemarker template
 *
 * @param fileName
 * @return
 * @throws IOException
 */
public static Template getFreemarkerTemplate(String fileName) throws IOException {
    Configuration conf = new Configuration(Configuration.VERSION_2_3_0);
    if(isDebug){
        conf.setDirectoryForTemplateLoading(new File(sResourcePath));
    }else{
        conf.setClassForTemplateLoading(Resources.class, "/");
    }
    return conf.getTemplate(fileName);
}
 
Example 3
Source File: AbstractFreemarkerTemplateConfigurer.java    From onetwo with Apache License 2.0 5 votes vote down vote up
/****
	 * must be invoke after contruction
	 */
	public void initialize() {
		if(isInitialized())
			return ;
		
		TemplateLoader loader = getTempateLoader();
		Assert.notNull(loader);
		try {
			this.configuration = new Configuration(Configuration.VERSION_2_3_0);
			this.configuration.setObjectWrapper(getBeansWrapper());
			this.configuration.setOutputEncoding(this.encoding);
			//设置默认不自动格式化数字……以防sb……
			this.configuration.setNumberFormat("#");
//			this.cfg.setDirectoryForTemplateLoading(new File(templateDir));
			
			/*if(templateProvider!=null){
				this.configuration.setTemplateLoader(new DynamicTemplateLoader(templateProvider));
			}*/
			if(LangUtils.isNotEmpty(getFreemarkerVariables()))
				configuration.setAllSharedVariables(new SimpleHash(freemarkerVariables, configuration.getObjectWrapper()));
			
			//template loader
			/*if(!LangUtils.isEmpty(templatePaths)){
				TemplateLoader loader = FtlUtils.getTemplateLoader(resourceLoader, templatePaths);
				this.configuration.setTemplateLoader(loader);
			}*/
			this.configuration.setTemplateLoader(loader);
			
			this.buildConfigration(this.configuration);
			
			initialized = true;
		} catch (Exception e) {
			throw new BaseException("create freemarker template error : " + e.getMessage(), e);
		}
	}
 
Example 4
Source File: EventEmail.java    From unitime with Apache License 2.0 4 votes vote down vote up
private String message() throws IOException, TemplateException {
	Configuration cfg = new Configuration(Configuration.VERSION_2_3_0);
	cfg.setClassForTemplateLoading(EventEmail.class, "");
	cfg.setLocale(Localization.getJavaLocale());
	cfg.setOutputEncoding("utf-8");
	Template template = cfg.getTemplate("confirmation.ftl");
	Map<String, Object> input = new HashMap<String, Object>();
	input.put("msg", MESSAGES);
	input.put("const", CONSTANTS);
	input.put("subject", subject());
	input.put("event", event());
	input.put("eventGridStartDay", ApplicationProperty.EventGridStartDay.intValue());
	input.put("operation", request().getOperation() == null ? "NONE" : request().getOperation().name());
	if (response().hasCreatedMeetings())
		input.put("created", EventInterface.getMultiMeetings(response().getCreatedMeetings(), true, false));
	if (response().hasDeletedMeetings())
		input.put("deleted", EventInterface.getMultiMeetings(response().getDeletedMeetings(), true, false));
	if (response().hasCancelledMeetings())
		input.put("cancelled", EventInterface.getMultiMeetings(response().getCancelledMeetings(), true, false));
	if (response().hasUpdatedMeetings())
		input.put("updated", EventInterface.getMultiMeetings(response().getUpdatedMeetings(), true, false));
	if (request().hasMessage())
		input.put("message", request().getMessage());
	if (request().getEvent().getId() != null) {
		if (event().hasMeetings()) {
			input.put("meetings", EventInterface.getMultiMeetings(event().getMeetings(), true, false));
		} else
			input.put("meetings", new TreeSet<MultiMeetingInterface>());
	}
	input.put("version", MESSAGES.pageVersion(Constants.getVersion(), Constants.getReleaseDate()));
	input.put("ts", new Date());
	input.put("link", ApplicationProperty.UniTimeUrl.value());
	if (iRequest.hasSessionId()) {
		Session session = SessionDAO.getInstance().get(iRequest.getSessionId());
		if (session != null)
			input.put("sessionId", session.getReference());
		else
			input.put("sessionId", iRequest.getSessionId());
	}
	
	
	
	StringWriter s = new StringWriter();
	template.process(input, new PrintWriter(s));
	s.flush(); s.close();

	return s.toString();
}