
java 中实现/user/edit?id=${id}=${type} 的替换;
引入包: freemark.jar ,以及
类代码如下:
public class FreeMarkerTextTemplateUtils { public static String processTextTemplateToString(String textTemplate,
Object model) throws IOException, TemplateException {
Configuration cfg = new Configuration();
StringTemplateLoader loader = new StringTemplateLoader();
String name = "kk_tempate";
loader.putTemplate(name, textTemplate);
cfg.setTemplateLoader(loader);
Template template = cfg.getTemplate(name);
return FreeMarkerTemplateUtils.processTemplateIntoString(template,
model);
}
}
单元测试代码如下:
String url="/user/edit?id=${id}&&type=${type}";
HashMap<String,Object> modelMap=new HashMap<String,Object>();
modelMap.put("id", 1);
modelMap.put("type", 1);
FreeMarkerTextTemplateUtils.processTextTemplateToString(
url, modelMap); 输出入下:/user/edit?id=1&&type=1