• public class PageRender implements ResponseRender


    package cn.ubibi.jettyboot.demotest.controller.render;

    import cn.ubibi.jettyboot.framework.commons.StringUtils;
    import cn.ubibi.jettyboot.framework.commons.SystemUtils;
    import cn.ubibi.jettyboot.framework.rest.ifs.ResponseRender;
    import org.thymeleaf.TemplateEngine;
    import org.thymeleaf.context.WebContext;
    import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
    import org.thymeleaf.templateresolver.FileTemplateResolver;
    import org.thymeleaf.templateresolver.ITemplateResolver;

    import javax.servlet.ServletContext;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.File;
    import java.io.IOException;
    import java.lang.management.ManagementFactory;
    import java.lang.reflect.Field;
    import java.net.URL;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;

    public class PageRender implements ResponseRender {

    private static TemplateEngine templateEngine = null;

    static {
    init();
    }


    public static void init() {
    if (templateEngine == null) {
    templateEngine = new TemplateEngine();
    templateEngine.setTemplateResolver(getTemplateResolver());
    }
    }


    private static ITemplateResolver getTemplateResolver() {
    if (SystemUtils.isMavenDevMode()) {
    return getFileTemplateResolver();
    }
    return getClassLoaderTemplateResolver();
    }


    private static FileTemplateResolver getFileTemplateResolver() {

    URL classLoaderResource = PageRender.class.getClassLoader().getResource("");
    String classPath = StringUtils.appendIfNotEnd(classLoaderResource.getPath(), File.separator);
    String srcResPath = classPath + ("../../src/main/resources".replace("/", File.separator));


    FileTemplateResolver templateResolver = new FileTemplateResolver();

    templateResolver.setSuffix(".html");
    templateResolver.setPrefix(srcResPath + "/templates/");
    templateResolver.setCacheTTLMs(3600000L);
    templateResolver.setCacheable(false);
    return templateResolver;
    }


    private static ClassLoaderTemplateResolver getClassLoaderTemplateResolver() {

    ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver(PageRender.class.getClassLoader());

    templateResolver.setSuffix(".html");
    templateResolver.setPrefix("/templates/");
    templateResolver.setCacheTTLMs(3600000L);
    templateResolver.setCacheable(true);

    return templateResolver;
    }





    private static Map<String, Object> objectToMap(Object obj) {
    if (obj == null) {
    return null;
    }

    if (obj instanceof Map) {
    return (Map<String, Object>) obj;
    }

    Map<String, Object> map = new HashMap<String, Object>();

    Field[] declaredFields = obj.getClass().getDeclaredFields();
    for (Field field : declaredFields) {
    field.setAccessible(true);
    try {
    map.put(field.getName(), field.get(obj));
    } catch (IllegalAccessException e) {
    e.printStackTrace();
    }
    }

    return map;
    }








    private String template;
    private Object pageData;

    public PageRender(String template, Object pageData) {
    this.pageData = pageData;
    this.template = template;
    }

    @Override
    public void doRender(HttpServletRequest request, HttpServletResponse response) throws IOException {

    ServletContext servletContext = request.getServletContext();
    WebContext ctx = new WebContext(request, response, servletContext, request.getLocale());

    Map<String, Object> variables = objectToMap(this.pageData);
    ctx.setVariables(variables);

    response.setCharacterEncoding("utf-8");
    templateEngine.process(this.template, ctx, response.getWriter());
    }

    }
  • 相关阅读:
    100 道 Linux 常见面试题
    借助Redis锁,完美解决高并发秒杀问题
    'cnpm'安装install
    Git常用命令及方法大全
    idea controller service impl mapper xml切换跳转快捷键
    idea创建springboot项目用阿里云镜像
    mybatis.type-aliases-package的作用和用法
    MyBatis Generator
    https://antdv.com/components/layout-cn/
    https://mvnrepository.com/search?q=mysql-connector-java //maven
  • 原文地址:https://www.cnblogs.com/lhp2012/p/8808264.html
Copyright © 2020-2023  润新知