How to use the FreeMarker tag
This article introduces the relevant knowledge of "how to use FreeMarker tag". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
Use the FreeMarker tag in the foreground template to get data.
tag class package com.jspxcms.plug.web.directive;/** * FreeMarker tag class needs to implement TemplateDirectiveModel interface */public class ResumeListDirective implements TemplateDirectiveModel { public static final String SITE_ID = "siteId"; public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException { //When using tags, the return variable must exist, such as [@ResumeList; result]... Result after semicolon in [/@ResumeList]. if (loopVars.length < 1) { throw new TemplateModelException("Loop variable is required. "); } //Tag body must exist, i.e.[@ResumeList; list]... [/@ResumeList] in the middle. if (body == null) { throw new RuntimeException("missing body"); } //Get tag parameters, such as [@ResumeList siteId ='123 '; list]... 123 in [/@ResumeList]. Integer[] siteId = Freemarkers.getIntegers(params, SITE_ID); if (siteId == null && params.get(SITE_ID) == null) { //If siteId is not passed, get the ID of the current site. siteId = new Integer[]{ForeContext.getSiteId(env)}; } Sort defSort = new Sort(Direction.DESC, "creationDate", "id"); Limitable limitable = Freemarkers.getLimitable(params, defSort); List list = service.findList(siteId, limitable); //Put the retrieved data into the return variable. loopVars[0] = env.getObjectWrapper().wrap(list); //Executes the tag body. body.render(env.getOut()); } @Autowired private ResumeService;} configuration file
FreeMarker tag classes need to be declared in the/src/main/resources/conf/plugin/plug/context.xml file
Then add freemarkerVariables.ResumeList=PlugResumeList to the file/src/main/resources/conf/plugin/plug/conf.properties. The name of the tag called in the template is ResumeList, not PlugResumeList.
tag at the use
After defining the tag, you can use this tag in any foreground template, such as:
[@ResumeList; result][#list result as bean] ${bean.name}, ${bean.mobile}[/#list][/@ResumeList]"How to use FreeMarker tags" content is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!