Freemarker调用java静态方法和属性、枚举,支持国际化等

  1. 调用静态方法和属性以及枚举,这个不多说,网上例子很多
  2. FreeMarkerTag这是很有特色的类,通过这个可以实现国际化…各种功能,比如Springmvc、activeWeb等都借助这个支持了国际化功能

示例

package org.javalite.activeweb.freemarker;

import freemarker.template.SimpleScalar;
import org.javalite.activeweb.Messages;

import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import static org.javalite.common.Util.split;

/**
 * The message tag is designed to display messages in view templates. Message values are defined in resource bundle called
 * "activeweb_messages". This means that  this tag will be looking for file called activeweb_messages.properties as default
 * name and others, such as activeweb_messages_fr_FR.properties in case French locale was specified.
 *
 * 

* Examples: *

* *

Simple usage

* Given that there is a file activeweb_messages.properties with content:
*
greeting=Hello!
 *  
* and tag code: * *
<@message key="greeting"/>
 *  
* then the output will be: *
Hello!
 *  
* *

Message with parameters

* Lets say a message in resource bundle is declared like this: * *
meeting=Meeting will take place on {0} at {1}
 * 
* * You can then specify the tag with parameters: *
<@message key="meeting" param0="Wednesday" param1="2:00 PM"/>
 * 
* * When a view template renders, the outcome will be: * *
Meeting will take place on Wednesday at 2:00 PM
 * 
* *

Defaulting to key if value not found

* * In case a resource bundle does not have a key specified, the key is rendered as value verbatim: * *
<@message key="greeting"/>
 *  
* * The output: *
greeting
 *  
* * *

Detection of locale from request

* * If there is a locale on the request supplied by the agent, then this locale is automatically picked up by this tag. * For instance, if a browser supplies locale "fr_FR" and there is a corresponding resource bundle: * "activeweb_messages_fr_FR.properties", with this property: * *
greeting=Bonjour!
 * 
* * then this tag: *
<@message key="greeting"/>
 * 
* will produce: *
Bonjour!
 * 
* *

Overriding request locale

* There is a "locale" argument you can pass to the tag to override the locale from request: *
<@message key="greeting" locale="de_DE"/>
 * 
* * * * @author Igor Polevoy: 8/15/12 3:50 PM */ public class MessageTag extends FreeMarkerTag { @Override protected void render(Map params, String body, Writer writer) throws Exception { if (params.containsKey("key")) { String key = params.get("key").toString(); if(params.containsKey("locale")){ String localeString = params.get("locale").toString(); String language, country; Locale locale; if(localeString.contains("_")){ language = split(localeString, '_')[0]; country = split(localeString, '_')[1]; locale = new Locale(language, country); }else{ language = localeString; locale = new Locale(language); } writer.write(Messages.message(key, locale, getParamsArray(params))); }else{ writer.write(Messages.message(key, getParamsArray(params))); } }else{ writer.write("you failed to supply key for this message tag"); } } private String[] getParamsArray(Map params) { int index = 0; List paramList = new ArrayList(); for (String paramName = "param"; params.containsKey(paramName + index); index++){ String param = ((SimpleScalar) params.get(paramName + index)).getAsString(); paramList.add(param); } return paramList.toArray(new String[]{}); } }

注册一下即可使用

class Config {
	private Configuration config;
	public Config() {
		config.setSharedVariable("message", new MessageTag());
	}
}

在ftl文件中使用
<@message key="index_autoflush" locale="${session.locale!}"/>

你可能感兴趣的:(java基础,模板解析)