How do you do it in the jar saw in the html file?



  • Writing in. pom.xml

            <resources>
                <resource>
                    <directory>${project.basedir}</directory>
                    <includes>
                        <include>templates/*.html</include>
                    </includes>
                </resource>
            </resources>
    

    The folder of the templates and her page.html file also falls into jar.

    That's how it's done in PageGenerator:

    private static final String HTML_DIR = "templates";
    Template template = cfg.getTemplate(HTML_DIR + File.separator + filename);
    

    where cfg Class object freemarker.template.ConfigurationHowever, then the inspection system issues:

    FileNotFoundException: Template "templates\\page.html" not found
    

    What is it?

    Thank you!

    Classes in which the exception arises:

    public class MirrorRequestServlet extends HttpServlet {
    
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response) throws ServletException, IOException {
    
        Map&lt;String, Object&gt; pageVariables = createPageVariablesMap(request);
    
        response.getWriter().println(PageGenerator.instance().getPage("page.html", pageVariables)); // падает здесь
    
        response.setContentType("text/html;charset=utf-8");
        response.setStatus(HttpServletResponse.SC_OK);
    
    }
    
    
    private static Map&lt;String, Object&gt; createPageVariablesMap(HttpServletRequest request) {
        Map&lt;String, Object&gt; pageVariables = new HashMap&lt;&gt;();
        pageVariables.put("key", request.getParameter("key"));
    
        return pageVariables;
    }
    

    }

    public class PageGenerator {

    private static final String HTML_DIR = "templates";
    private static PageGenerator pageGenerator;
    private final Configuration cfg;
    
    private PageGenerator() {
        cfg = new Configuration();
    }
    
    public static PageGenerator instance() {
        if (pageGenerator == null)
            pageGenerator = new PageGenerator();
        return pageGenerator;
    }
    
    public String getPage(String filename, Map&lt;String, Object&gt; data) {
        Writer stream = new StringWriter();
        try {
    
            Template template = cfg.getTemplate("classpath:" + HTML_DIR + File.separator + filename); // падает здесь
    
            template.process(data, stream);
        } catch (IOException | TemplateException e) {
            e.printStackTrace();
        }
        return stream.toString();
    }
    

    }

    All pom.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    &lt;groupId&gt;L1.1&lt;/groupId&gt;
    &lt;artifactId&gt;L1.1&lt;/artifactId&gt;
    &lt;version&gt;1.0&lt;/version&gt;
    
    &lt;properties&gt;
        &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
        &lt;maven.compiler.source&gt;1.8&lt;/maven.compiler.source&gt;
        &lt;maven.compiler.target&gt;1.8&lt;/maven.compiler.target&gt;
    &lt;/properties&gt;
    
    &lt;build&gt;
        &lt;plugins&gt;
    
            &lt;!-- Maven JAR Plugin --&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                &lt;artifactId&gt;maven-jar-plugin&lt;/artifactId&gt;
                &lt;version&gt;2.4&lt;/version&gt;
                &lt;configuration&gt;
                    &lt;archive&gt;
                        &lt;manifest&gt;
                            &lt;addClasspath&gt;true&lt;/addClasspath&gt;
                            &lt;!--&lt;classpathPrefix&gt;lib&lt;/classpathPrefix&gt;--&gt;
                            &lt;mainClass&gt;main.Main&lt;/mainClass&gt;
                        &lt;/manifest&gt;
                    &lt;/archive&gt;
                &lt;/configuration&gt;
            &lt;/plugin&gt;
    
    
            &lt;plugin&gt;
                &lt;artifactId&gt;maven-assembly-plugin&lt;/artifactId&gt;
                &lt;configuration&gt;
                    &lt;descriptorRefs&gt;
                        &lt;descriptorRef&gt;jar-with-dependencies&lt;/descriptorRef&gt;
                    &lt;/descriptorRefs&gt;
                    &lt;archive&gt;
                        &lt;manifest&gt;
                            &lt;mainClass&gt;main.Main&lt;/mainClass&gt;
                        &lt;/manifest&gt;
                    &lt;/archive&gt;
                &lt;/configuration&gt;
            &lt;/plugin&gt;
    
        &lt;/plugins&gt;
    
    
            &lt;resources&gt;
                &lt;resource&gt;
                    &lt;directory&gt;${project.basedir}&lt;/directory&gt;
                    &lt;includes&gt;
                        &lt;include&gt;templates/*.html&lt;/include&gt;
                    &lt;/includes&gt;
                &lt;/resource&gt;
            &lt;/resources&gt;
    
    
    &lt;/build&gt;
    
    
    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.freemarker&lt;/groupId&gt;
            &lt;artifactId&gt;freemarker&lt;/artifactId&gt;
            &lt;version&gt;2.3.20&lt;/version&gt;
        &lt;/dependency&gt;
    
        &lt;dependency&gt;
            &lt;groupId&gt;org.eclipse.jetty&lt;/groupId&gt;
            &lt;artifactId&gt;jetty-server&lt;/artifactId&gt;
            &lt;version&gt;9.3.0.M0&lt;/version&gt;
        &lt;/dependency&gt;
    
        &lt;dependency&gt;
            &lt;groupId&gt;org.eclipse.jetty&lt;/groupId&gt;
            &lt;artifactId&gt;jetty-webapp&lt;/artifactId&gt;
            &lt;version&gt;9.3.0.M0&lt;/version&gt;
        &lt;/dependency&gt;
    
    &lt;/dependencies&gt;
    

    </project>

    Main.class:

    public class Main {
    public static void main(String[] args) throws Exception {
    MirrorRequestServlet mirrorRequestServlet = new MirrorRequestServlet();

        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.addServlet(new ServletHolder(mirrorRequestServlet), "/mirror");
    
        Server server = new Server(8080);
        server.setHandler(context);
    
        server.start();
        System.out.println("Server started");
    
        server.join();
    }
    

    }



  • Use it.

     Template template = cfg.getTemplate("classpath:"+HTML_DIR + File.separator + filename);
    

    Or when you're created, you can configurate yours. cfg by method http://freemarker.incubator.apache.org/docs/api/freemarker/template/Configuration.html#setClassForTemplateLoading-java.lang.Class-java.lang.String-




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2