1、添加自定義Freemarker標籤html
/** * @Description: 權限自定義標籤 * @author */ public class PermissionDirective implements TemplateDirectiveModel { @SuppressWarnings({ "rawtypes" }) @Override public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException { if (params.containsKey("userId")) { String userId = params.get("userId").toString(); //當前用戶 User user = User.dao.findById(userId); if (user != null) { body.render(env.getOut()); } } } }
2、JFinal config中配置自定義標籤java
@Override public void configConstant(Constants me) { //自定義Freemarker標籤 Map<String, Object> map = new HashMap<String, Object>(); map.put(DirectiveCode.permission.toString(), new PermissionDirective()); //多個繼續增長 try { FreeMarkerRender.getConfiguration().setSharedVaribles(map); } catch (TemplateModelException e) { e.printStackTrace(); } }
3、Freemarker頁面中使用自定義標籤ide
<@permission userId="002"> <a href="/user/list">用戶管理</a>
總結:經過以上三步配置便可在頁面上使用Freemarker自定義標籤。oop