All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.haoxuer.discover.article.freemarker.ArticleDirective Maven / Gradle / Ivy

There is a newer version: 3.3.18-20230117
Show newest version
package com.haoxuer.discover.article.freemarker;

import com.haoxuer.discover.article.data.entity.Article;
import com.haoxuer.discover.article.data.service.ArticleService;
import com.haoxuer.discover.common.utils.DirectiveUtils;
import com.haoxuer.discover.data.page.Filter;
import com.haoxuer.discover.data.page.Order;
import com.haoxuer.discover.data.page.Page;
import com.haoxuer.discover.data.page.Pageable;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static freemarker.template.ObjectWrapper.DEFAULT_WRAPPER;

public class ArticleDirective implements TemplateDirectiveModel {

    @Override
    public void execute(Environment env, Map params, TemplateModel[] loopVars,
                        TemplateDirectiveBody body) throws TemplateException, IOException {


        Integer size = DirectiveUtils.getInt("size", params);
        if (size == null) {
            size = 10;
        }
        Integer id = DirectiveUtils.getInt("id", params);

        Pageable pager = new Pageable();
        pager.setPageSize(size);
        pager.getFilters().add(Filter.eq("catalog.id", id));
        pager.getOrders().add(Order.desc("id"));
        Page
page = articleService.page(pager); Map paramWrap = new HashMap(params); paramWrap.put("list", DEFAULT_WRAPPER.wrap(page.getContent())); Map origMap = DirectiveUtils .addParamsToVariable(env, paramWrap); if (body != null) { body.render(env.getOut()); } DirectiveUtils.removeParamsFromVariable(env, paramWrap, origMap); } @Autowired ArticleService articleService; }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy