(这是学习笔记)
1.网页静态化
关于网页静态化我使用的是VelocityHelper实现的。
关键的部分如下
- public static String velocity(String templatePath,String templateName, Map<String, Object> map) {
try {
VelocityEngine ve = new VelocityEngine();
// 配置velocity引擎 也可通过配置文件和Properties来配置
// ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
// ve.setProperty("classpath.resource.loader.class",
// ClasspathResourceLoader.class.getName());
// 设置编码
ve.setProperty(Velocity.ENCODING_DEFAULT, "UTF-8");
ve.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
ve.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");
ve.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, templatePath);
Object s = ve.getProperty(Velocity.FILE_RESOURCE_LOADER_PATH);
// 初始化
ve.init();
// 模板地址
Template t = ve.getTemplate(templateName);
// 构造VelocityContext,将数值传入Context
VelocityContext ctx = new VelocityContext(map);
// 输出字符串
StringWriter sw = new StringWriter();
// 像模板串字符串,并得字符
t.merge(ctx, sw);
// System.out.println(sw.toString());
return sw.toString();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e.toString());
}
} -
其中templatePath模板目录 (相对路径) templateName是模板名称(一般是 ×××.vm)
在模板里,把要替换的值只能是map集合,在×××.vm中,你把变动的值用(假设是一个对象)用$(key).(属性名)() eg: $b.name()这样就能将值传入模板中
假设传入的值是list集合要循环
#foreach($p in $list)
$p.(属性名)()
#end
这样来遍历
2.base64加密解密
- byte[] encodeBase64 = Base64.encodeBase64(“asv”.getBytes());
- byte[] decodeBase64 = Base64.decodeBase64(encodeBase64);