- 调用静态方法和属性以及枚举,这个不多说,网上例子很多
- 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 <code>activeweb_messages.properties</code> as default
* name and others, such as <code>activeweb_messages_fr_FR.properties</code> in case French locale was specified.
*
* <p>
* Examples:
* </p>
*
* <h4>Simple usage</h4>
* Given that there is a file <code>activeweb_messages.properties</code> with content:<br/>
* <pre>
greeting=Hello!
* </pre>
* and tag code:
*
* <pre>
<@message key="greeting"/>
* </pre>
* then the output will be:
* <pre>
Hello!
* </pre>
*
* <h4>Message with parameters</h4>
* Lets say a message in resource bundle is declared like this:
*
* <pre>
meeting=Meeting will take place on {0} at {1}
* </pre>
*
* You can then specify the tag with parameters:
* <pre>
<@message key="meeting" param0="Wednesday" param1="2:00 PM"/>
* </pre>
*
* When a view template renders, the outcome will be:
*
* <pre>
Meeting will take place on Wednesday at 2:00 PM
* </pre>
*
* <h4>Defaulting to key if value not found</h4>
*
* In case a resource bundle does not have a key specified, the key is rendered as value verbatim:
*
* <pre>
<@message key="greeting"/>
* </pre>
*
* The output:
* <pre>
greeting
* </pre>
*
*
* <h4>Detection of locale from request</h4>
*
* 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:
*
* <pre>
greeting=Bonjour!
* </pre>
*
* then this tag:
* <pre>
<@message key="greeting"/>
* </pre>
* will produce:
* <pre>
Bonjour!
* </pre>
*
* <h4>Overriding request locale</h4>
* There is a \"locale\" argument you can pass to the tag to override the locale from request:
* <pre>
<@message key="greeting" locale="de_DE"/>
* </pre>
*
*
*
* @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(\"<span style=\\\"display:none\\\">you failed to supply key for this message tag</span>\");
}
}
private String[] getParamsArray(Map params) {
int index = 0;
List<String> paramList = new ArrayList<String>();
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());
}
}
继续阅读与本文标签相同的文章
-
如何检测 Web 服务请求丢失问题
2026-05-18栏目: 教程
-
基于阿里云服务器ECS的建站过程
2026-05-18栏目: 教程
-
8分钟5个点让你彻底了解负载均衡
2026-05-18栏目: 教程
-
2019年9月16日,开发者社区奖励办法--8月评选结果正式公布
2026-05-18栏目: 教程
-
RocketMQ 主题扩分片后遇到的坑
2026-05-18栏目: 教程
