Springboot freemark自定义标签

来源:互联网 发布:好听的改编网络歌曲 编辑:程序博客网 时间:2024/06/01 23:21

spring-boot里使用freemarker自定义标签

spring-boot开发网站使用freemarker里的自定义标签方法

创建类实现 TemplateDirectiveModel 类

@Componentpublic class UserTopicDirective implements TemplateDirectiveModel {  @Autowired  private UserService userService;  @Autowired  private TopicService topicService;  @Override  public void execute(Environment environment, Map map, TemplateModel[] templateModels,                      TemplateDirectiveBody templateDirectiveBody) throws TemplateException, IOException {    Page<Topic> page = new PageImpl<>(new ArrayList<>());    if (map.containsKey("username") && map.get("username") != null) {      String username = map.get("username").toString();      if (map.containsKey("p")) {        int p = map.get("p") == null ? 1 : Integer.parseInt(map.get("p").toString());        int limit = Integer.parseInt(map.get("limit").toString());        User currentUser = userService.findByUsername(username);        if (currentUser != null) {          page = topicService.findByUser(p, limit, currentUser);        }      }    }    DefaultObjectWrapperBuilder builder = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_25);    environment.setVariable("page", builder.build().wrap(page));    templateDirectiveBody.render(environment.getOut());  }}

创建配置类

@Componentpublic class FreemarkerConfig {  @Autowired  private Configuration configuration;  @Autowired  private UserTopicDirective userTopicDirective;  @PostConstruct  public void setSharedVariable() throws TemplateModelException {    configuration.setSharedVariable("user_topics_tag", userTopicDirective);  }}

用法

跟自定义macro用法一样,直接使用 <@xx></@xx> 来使用即可,值就直接在 user_topics_tag 标签里传就可以了

<@user_topics_tag username='tomoya' p=1 limit=10>  <#list page.getContent() as topic>    <p>${topic.title!}</p>  </#list></@user_topics_tag>

扩展

FreemarkerConfig类不止可以加入自定义的标签,还可以加入系统自定义的变量等,下面举例说明

spring-boot里的配置文件

# application.ymlsite:  baseUrl: http://localhost:8080/

对应的类是 SiteConfig.java 要取里面的值,使用方法如下

@Autowiredprivate SiteConfig siteConfig;//...siteConfig.getBaseUrl();

如果把siteConfig加入到freemarker的configuration里就可以直接在freemarker页面上使用变量了

@PostConstructpublic void setSharedVariable() throws TemplateModelException {  configuration.setSharedVariable("site", siteConfig);  configuration.setSharedVariable("user_topics_tag", userTopicDirective);}

页面里就可以这样来取值

<a href="${site.bashUrl}">首页</a>

是不是很方便

参考

原创粉丝点击